package com.lbs.okhttp1; import android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.util.Log; import android.view.View; import java.io.IOException; import okhttp3.Call; import okhttp3.Callback; import okhttp3.FormBody; import okhttp3.OkHttpClient; import okhttp3.Request; import okhttp3.Response; import okhttp3.ResponseBody; public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void doGetClick(View view) { new Thread(new Runnable() { @Override public void run() { try { //实例化OKhttpclick OkHttpClient okhttpclick = new OkHttpClient(); Request request = new Request.Builder().url("http://apicloud.mob.com/v1/weather/type?key=22ecf6c32440e").build(); //通过newCall方法获取到Call对象 Call call = okhttpclick.newCall(request); //通过call去进行网络请求并返回响应结果 Response execute = call.execute(); if (execute.isSuccessful()) { //获取到响应体 ResponseBody body = execute.body(); //根据相应体获取到相应内容 String jsonStr = body.string(); Log.i("t", "result=" + jsonStr); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } public void doGetClickYi(View view){ OkHttpClient okHttpClient = new OkHttpClient(); Request request = new Request.Builder().url("http://apicloud.mob.com/v1/weather/type?key=22ecf6c32440e").build(); Call call = okHttpClient.newCall(request); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { ResponseBody body = response.body(); String string = body.string(); Log.i("tt",""+string); } } }); } public void doPostClick(View view){ new Thread(new Runnable() { @Override public void run() { try { OkHttpClient okHttpClient3 = new OkHttpClient(); FormBody.Builder add = new FormBody.Builder() .add("mobile", "17611200379") .add("password", "123456"); Request buildd = new Request.Builder().post(add.build()).url("http://120.27.23.105/user/login").build(); Call call = okHttpClient3.newCall(buildd); Response execute = call.execute(); if (execute.isSuccessful()) { ResponseBody body = execute.body(); String string = body.string(); Log.i("ttt",""+string); } } catch (IOException e) { e.printStackTrace(); } } }).start(); } public void doPostClickYi(View view){ new Thread(new Runnable() { @Override public void run() { OkHttpClient okHttpClient = new OkHttpClient(); FormBody.Builder add = new FormBody.Builder() .add("mobile", "17611200379") .add("password", "123456"); Request build = new Request.Builder().post(add.build()).url("http://120.27.23.105/user/login").build(); Call call = okHttpClient.newCall(build); call.enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ ResponseBody body = response.body(); String string = body.string(); Log.i("tttt",""+string); } } }); } }).start(); } }
<?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="com.lbs.okhttp1.MainActivity"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="okhttp的get同步请求" android:onClick="doGetClick" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="okhttp的get异步请求" android:onClick="doGetClickYi" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="okhttp的post同步请求" android:onClick="doPostClick" /> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="okhttp的post异步请求" android:onClick="doPostClickYi" /> </LinearLayout>
/**
* OKHttp的Post网络请求(同步)*
* @param view
*/
public void doPostRequest_bak(View view) {
new Thread(new Runnable() {
@Override
public void run() {
try {
//实例化OkHttpClient
OkHttpClient okHttpClient = new OkHttpClient();
//区别----------------------------------------
//通过FormBody的builder方法获取到buildr对象
//FormBody.Builder builder = new FormBody.Builder();
//通过builder设置表单请求数据
//builder.add("", "");
FormBody.Builder builder = new FormBody.Builder()
.add("mobile", "17611200379")
.add("password", "123456");
//需要设置编码 中文编码时用到
//builder.addEncoded("","");
//通过builder对象的build方法获取到FormBody对象
//FormBody extends RequestBody
//FormBody body = builder.build();
//----------------------------------------
//通过Request的builder对象设置post请求方式并设置请求地址并返回请求对象request
//第一种
//Request request = new Request.Builder().post(body).url("").build();
// 第二种
Request request = new Request.Builder().post(builder.build()).url("http://120.27.23.105/user/login").build();
//通过调用newCall方法获取到Call对象
Call call = okHttpClient.newCall(request);
//通过call请求网络获取到响应对象Response
Response response = call.execute();
//判断响应结果是否正确 200
if (response.isSuccessful()) {
String josnStr = response.body().string();
Log.i("t", "result=" + josnStr);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();
}