**
* OKHttp3
* 如何使用:
* 1:配置网络权限
* 2:导入依赖
* 3:写一些代码
* 同步和异步请求网络
* 1:同步是耗时操作必须要放在子线程里 ,异步不需要
*/
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button Send_Get_Tong;
private Button Send_Post_Tong;
private Button Send_Get_Async;
private Button Send_POST_Async;
private String mUrl = "http://112.124.22.238:8081/course_api/cniaoplay/featured2?p={%27page%27:0}";
private String mPostUrl = "http://172.16.45.10:8080/FuWebDemo/AA";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
}
private void initView() {
Send_Get_Tong = (Button) findViewById(R.id.Send_Get_Tong);
Send_Post_Tong = (Button) findViewById(R.id.Send_Post_Tong);
Send_Get_Async = (Button) findViewById(R.id.Send_Get_Async);
Send_POST_Async = (Button) findViewById(R.id.Send_POST_Async);
Send_Get_Tong.setOnClickListener(this);
Send_Post_Tong.setOnClickListener(this);
Send_Get_Async.setOnClickListener(this);
Send_POST_Async.setOnClickListener(this);
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.Send_Get_Tong:
getInfo();
break;
case R.id.Send_Post_Tong:
getPostInfo();
break;
case R.id.Send_Get_Async:
asyncGetInfo();
break;
case R.id.Send_POST_Async:
asyncPostInfo();
break;
}
}
//同步GET请求
private void getInfo() {
new Thread(new Runnable() {
@Override
public void run() {
try {
//1:需要一个OKHTTPClient对象
OkHttpClient okHttpClient = new OkHttpClient();
//2:封装请求数据
Request request = new Request.Builder().url(mUrl).build();
//3:发送请求并且生成一个相应
Response response = okHttpClient.newCall(request).execute();
//3:得到相应体里面数据
String string = response.body().string();
Log.e("看看Json串", string);
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//同步请求POST发送方式
private void getPostInfo() {
new Thread(new Runnable() {
@Override
public void run() {
try {
OkHttpClient ok = new OkHttpClient();
//请求体参数
FormBody formBody = new FormBody.Builder().add("userName", "lxx").add("passWord", "123").build();
//设置请求方式
Request request = new Request.Builder().url(mPostUrl).post(formBody).build();
Response response = ok.newCall(request).execute();
Log.e("post请求", response.body().string());
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}
//异步GET请求
private void asyncGetInfo() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(mUrl).build();
okHttpClient.newCall(request).enqueue(new Callback() {
//这个接口访问失败时做的操作
@Override
public void onFailure(Call call, IOException e) {
}
//一个成功时做的操作
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("异步get请求", response.body().string());
}
});
}
//异步POST请求
private void asyncPostInfo() {
OkHttpClient okHttpClient = new OkHttpClient();
FormBody formBody = new FormBody.Builder().add("userName", "lxs").add("passWord", "123").build();
Request request = new Request.Builder().url(mPostUrl).post(formBody).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Log.e("异步Post请求", response.body().string());
}
});
}
}
//布局
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/Send_Get_Tong" android:text="同步Get请求"/>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/Send_Post_Tong" android:text="同步Post请求"/>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/Send_Get_Async" android:text="异步Get请求"/>
<Button android:layout_height="wrap_content" android:layout_width="match_parent" android:id="@+id/Send_POST_Async" android:text="异步POST请求"/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content" android:id="@+id/get_Content" android:text="memeda"/>
//导依赖
compile 'com.squareup.okhttp3:okhttp:3.6.0'