使用Okhttp3访问网络 Okhttp学习笔记(一)
关于OKHttp
详细可以看GitHup的项目主页地址:https://github.com/square/okhttp 。
运行结果图:
第一步,引用Okhttp
首先在项目中添加一行引用:
implementation 'com.squareup.okhttp3:okhttp:3.14.0'
添加完成后,Android会自动下载OKHttp库与OKi库。
第二步,修改MainActivity.java
package com.example.a13115.tobeyfirstoriginproject;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import java.io.IOException;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
public class MainActivity extends AppCompatActivity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.button_request);
textView = (TextView) findViewById(R.id.text_response);
}
@Override
protected void onStart() {
super.onStart();
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
sendRequestWithOkHttp();
}
});
}
private void sendRequestWithOkHttp() {
new Thread(new Runnable() {
@Override
public void run() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder()
.url("https://github.com")
.build();
try {
Response response = okHttpClient.newCall(request).execute();
String responseData = response.body().string();
showResponse(responseData);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
private void showResponse(final String responseData) {
runOnUiThread(new Runnable() {
@Override
public void run() {
//不能在子线程更新,不然会崩溃,所以使用这个匿名类参数方法操作
//使用textview显示获取的html数据
textView.setText(responseData);
}
});
}
}
第三步,修改activity_main.xml布局文件
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="@+id/button_request"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="send"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/text_response"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textColor="#00f"
/>
</ScrollView>
</LinearLayout>
最后一步,添加网络权限
在AndroidManifest.xml中添加:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a13115.tobeyfirstoriginproject">
//添加网络权限
<uses-permission android: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">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
好了,本篇到此结束,项目地址:https://github.com/Tobey-r1/RepositoryTest 。
欢迎一起讨论!