当前位置:网站首页 > 更多 > 编程开发 > 正文

[安卓] Android OKHttp发送get网络请求实例

作者:CC下载站 日期:2020-09-23 00:00:00 浏览:56 分类:编程开发

今天总算把安卓的网络请求弄了一下了。

获取的是我自己做的接口:https://api.565.ink/one/

随机一句英语,不得不说换一门语言,写法上真的有点不适应。

MainActivify.java

packageink.cik.firsthttpapp;

importandroid.os.Bundle;
importandroid.util.Log;
importandroid.view.View;
importandroid.widget.Button;
importandroid.widget.TextView;

importandroidx.appcompat.app.AppCompatActivity;

importjava.io.IOException;

importokhttp3.OkHttpClient;
importokhttp3.Request;
importokhttp3.Response;

publicclassMainActivityextendsAppCompatActivity{
TextViewresponseText;

@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Buttonbutton=(Button)findViewById(R.id.btnSyncTest);
responseText=findViewById(R.id.response_text);
button.setOnClickListener(newView.OnClickListener(){
@Override
publicvoidonClick(Viewv){
Log.d("点击事件","点击发送请求按钮");
sendRequest();
}
});
}

privatevoidsendRequest(){
//开启线程发送请求
newThread(newRunnable(){
@Override
publicvoidrun(){
try{
OkHttpClientclient=newOkHttpClient();
Requestrequest=newRequest.Builder().url("https://api.565.ink/one/").build();
Responseresponse=client.newCall(request).execute();
StringresponseData=response.body().string();
showResponse(responseData);
}catch(IOExceptione){
e.printStackTrace();
}
}
}).start();
}

privatevoidshowResponse(finalStringresponse){
runOnUiThread(newRunnable(){
@Override
publicvoidrun(){
responseText.setText(response);
}
});

}
}

AndroidMainifest.xml

<?xmlversion="1.0"encoding="utf-8"?>
<manifestxmlns:android="http://schemas.android.com/apk/res/android"
package="ink.cik.firsthttpapp">

<uses-permissionandroid:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activityandroid:name=".MainActivity">
<intent-filter>
<actionandroid:name="android.intent.action.MAIN"/>

<categoryandroid:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>

</manifest>

activity_main.xml

<?xmlversion="1.0"encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<Button
android:id="@+id/btnSyncTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="同步测试"/>

<Button
android:id="@+id/btnAsyncTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="异步测试"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<EditText
android:id="@+id/etId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="6"
android:hint="输入用户编号"
android:inputType="number"/>

<Button
android:id="@+id/btnParamTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="doParamTest"
android:text="传参测试"/>

</LinearLayout>

<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/response_text"
android:layout_width="match_parent"
android:layout_height="match_parent"/>

</ScrollView>
</LinearLayout>

build.gradle

applyplugin:'com.android.application'

android{
compileSdkVersion30
buildToolsVersion"30.0.2"
defaultConfig{
applicationId"ink.cik.firsthttpapp"
minSdkVersion30
targetSdkVersion30
versionCode1
versionName"1.0"
testInstrumentationRunner"androidx.test.runner.AndroidJUnitRunner"
}
buildTypes{
release{
minifyEnabledfalse
proguardFilesgetDefaultProguardFile('proguard-android-optimize.txt'),'proguard-rules.pro'
}
}
}

dependencies{
implementationfileTree(dir:'libs',include:['*.jar'])
implementation'androidx.appcompat:appcompat:1.2.0'
implementation'androidx.constraintlayout:constraintlayout:2.0.1'
testImplementation'junit:junit:4.12'
androidTestImplementation'androidx.test.ext:junit:1.1.2'
androidTestImplementation'androidx.test.espresso:espresso-core:3.3.0'
implementation'com.squareup.okhttp3:okhttp:4.9.0'
implementation'com.squareup.okio:okio:2.8.0'
}

总结一下踩得一些坑。

坑一:

导入okhttp包导错了地方。

书上说,要在build.gradle中导入okhttp和okio

于是乎。。这两个里面果断的选择了第一个,因为名字是自己取的,按照python来说,默认的都是系统的。

结果出错了,其实是Module:app这个

坑二:本来我是本地搭建的环境,然后电脑端自己修改的hosts,将lan指向了127.0.0.1于是乎。

坑三:原本用的http,结果不行,百度发现得https

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯