okhttp同步/异步
来源:互联网 发布:mysql 5.7 for linux 编辑:程序博客网 时间:2024/05/20 15:59
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; }}}
阅读全文
0 0
- okhttp同步/异步
- 使用OkHttp进行网络同步异步操作
- okhttp 同步请求和异步请求
- okhttp异步请求和同步请求
- OkHttp的同步请求和异步请求
- OkHttp的get/post同步异步请求
- OKhttp get post 同步 异步 header设置
- okhttp 一 概述及同步和异步请求的实现
- OkHttp的同步和异步请求的实现
- Okhttp之同步和异步请求简单分析
- OKHttp异步get,post请求和同步请求
- Android OKHttp同步get请求,同步post请求,异步get请求,异步post请求
- okhttp 第三方 网络数据请求 异步get请求 和异步post请求 同步请求
- OKhttp:Get同步请求丶Get异步请求丶Post异步请求丶上传文件
- OKhttp:Get同步请求丶Get异步请求丶Post异步请求丶上传文件
- OkHttp 的基本数据请求 步骤 模板 (同时包裹异步请求和同步请求)
- 关于okhttp的同步消息和异步消息总结(java中)
- okhttp 异步调用
- Java二分查找、折半查找
- Eclipse快捷键设置
- Nodemcu上使用Mongoose OS的坑(1)
- MODBUS协议
- 调试android源码之打堆栈加重写控件
- okhttp同步/异步
- 【Spring Security实战系列】Spring Security实战(五)
- arxiv | 技术概述深度学习:详解前馈、卷积和循环神经网络
- unity3d 任务头上的血条
- bzoj1726 [Usaco2006 Nov]Roadblocks第二短路(spfa)
- 模型评估-留出法
- 91免费▪分享:什么是共享经济?共享经济共享的都是什么?
- Android 通知(使用NotificationCompat.Builder )
- Eclipse快捷键的设置和使用