public class MainActivity extends AppCompatActivity { private OkHttpClient okHttpClient; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); okHttpClient = App.okHttpClient(); } //同步的get public void get(View view) { //request 设置url final Request request= new Request.Builder() .url("http://www.baidu.com") .build(); new Thread(new Runnable() { @Override public void run() { try { //通过newCall方法将request转换成call ,如果用execute()是同步执行 Response execute = okHttpClient.newCall(request).execute(); if (execute.isSuccessful()){ runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"get成功",Toast.LENGTH_SHORT).show(); } }); }else { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"get失败",Toast.LENGTH_SHORT).show(); } }); } } catch (Exception e) { e.printStackTrace(); } } }).start(); } //异步get,所以的回调方法里面都是分线程.不能更新ui public void getAsync(View view) { final Request request = new Request.Builder() .url("http://www.baidu.com") .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"异步get失败",Toast.LENGTH_SHORT).show(); } }); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ String json = response.body().toString(); //用Gson解析 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "get成功", Toast.LENGTH_SHORT).show(); } }); }else { //用Gson解析 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "getshibai", Toast.LENGTH_SHORT).show(); } }); } } }); } //Post异步请求 public void postAsync(View view) { FormBody formBody = new FormBody.Builder() .add("type", "yuantong") .add("postid", "1111111") .build(); Request request = new Request.Builder() .url(" http://www.kuaidi100.com/query") .post(formBody) .build(); okHttpClient.newCall(request).enqueue(new Callback() { @Override public void onFailure(Call call, IOException e) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this,"异步get失败",Toast.LENGTH_SHORT).show(); } }); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()){ String json = response.body().toString(); //用Gson解析 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "post成功", Toast.LENGTH_SHORT).show(); } }); }else { //用Gson解析 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "getshibai", Toast.LENGTH_SHORT).show(); } }); } } }); } } Myapp:public class App extends Application { private static OkHttpClient okHttpClient; @Override public void onCreate() { super.onCreate(); //建议一个app只有一个OkHttpClient实例 okHttpClient = new OkHttpClient(); okHttpClient = okHttpClient.newBuilder() .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10, TimeUnit.SECONDS) .addInterceptor(new MyLogInterceptor()) .build(); } public static OkHttpClient okHttpClient() { return okHttpClient; } //拦截器,可以修改header,可以通过拦截器打印日志 private class MyLogInterceptor implements Interceptor { @Override public Response intercept(Chain chain) throws IOException { Request request = chain.request().newBuilder() .header("shenfen", "chinesse") .build(); HttpUrl url = request.url(); String httpUrl = url.url().toString(); Log.e("TAG", "============" + httpUrl); Response response = chain.proceed(request); int code = response.code(); Log.e("TAG", "============response.code() == " + code); return response; } } }
okhttp同步/异步
最新推荐文章于 2024-08-15 22:20:05 发布