OkHttpGET+Post+异步GET+异步POST请求数据
来源:互联网 发布:网络科学导论 课件 编辑:程序博客网 时间:2024/06/16 04:38
APP
public class App extends Application { private static OkHttpClient okHttpClient; @Override public void onCreate() { super.onCreate(); okHttpClient = new OkHttpClient(); okHttpClient = okHttpClient.newBuilder() .connectTimeout(10, TimeUnit.SECONDS) .readTimeout(10,TimeUnit.SECONDS) .addInterceptor(new MyLogInterceptor()) .build(); } public static OkHttpClient okHttpClient() { return okHttpClient; } public 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; } }}Activity
public class MainActivity extends AppCompatActivity implements View.OnClickListener { private OkHttpClient okHttpClient; private Response response; private Request request; private Button getbutton; private Button postbutton; private Button getyubu; private Button postyibu; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); getbutton = (Button) findViewById(R.id.getbutton); postbutton = (Button) findViewById(R.id.postbutton); getyubu = (Button) findViewById(R.id.getyibubutton); postyibu = (Button) findViewById(R.id.postyibubutton); getbutton.setOnClickListener(this); postbutton.setOnClickListener(this); getyubu.setOnClickListener(this); postyibu.setOnClickListener(this); okHttpClient = App.okHttpClient(); } @Override public void onClick(View v) { switch (v.getId()){ //同步 case R.id.getbutton: request = new Request.Builder() .url("http://www.baidu.com") .build(); new Thread(new Runnable() { @Override public void run() { try { response = okHttpClient.newCall(request).execute(); if (response.isSuccessful()){// String json = response.body().string();// InputStream inputStream = response.body().byteStream(); 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(); break;// case R.id.postbutton:// break; case R.id.getyibubutton: //异步get 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 { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "异步请求成功get", Toast.LENGTH_SHORT).show(); } }); } }); break; case R.id.postyibubutton: //异步post FormBody body = new FormBody.Builder() .add("type", "yuantong") .add("postid", "11111111111") .build(); request = new Request.Builder() .url("http://www.kuaidi100.com/query") .post(body) .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, "post请求失败", Toast.LENGTH_SHORT).show(); } }); } @Override public void onResponse(Call call, Response response) throws IOException { if (response.isSuccessful()) { runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(MainActivity.this, "post请求成功", Toast.LENGTH_SHORT).show(); } }); } } }); break; } }}
阅读全文
0 0
- OkHttpGET+Post+异步GET+异步POST请求数据
- Okhttp 异步get post 请求
- OkHttpGET和POST请求
- 网络请求 同步get 同步post 异步get 异步post
- iOS 数据请求 同步get 同步post 异步get 异步post
- okhttp 第三方 网络数据请求 异步get请求 和异步post请求 同步请求
- UI 14 GET & POST 数据请求 & block异步
- javascript异步请求,get和post
- iOS同步 异步 get post 请求
- GET--POST的同步/异步请求
- iOS网络请求GET&POST,同步&异步
- $.get()/$.post()/$ajax() 异步请求的例子
- iOS原生Http请求,get post 异步
- PHP异步请求 POST和GET方式
- Ajax异步请求-GET和POST
- AngularJS发送异步Get/Post请求
- OkHttp的get/post同步异步请求
- Android OKHttp同步get请求,同步post请求,异步get请求,异步post请求
- Windows 下的删除含有众多文件夹的代码目录操作
- $(window).width()与window.innerWidth的区别
- Centos里的tomcat开启https接口
- asp.net mvc下使用FluentScheduler 后台任务定时器
- 搭建自己的ES6转ES5环境
- OkHttpGET+Post+异步GET+异步POST请求数据
- DeepLearning tutorial 易用的深度学习框架Keras简介
- 两个程序模拟交互,出现No route to host异常
- Android AIDL使用详解
- 备战秋招复习之HashMap
- eclipse配置虚拟tomcat运行环境,不同项目配置不同tomcat端口号
- PowerDesigner 正向工程 和 逆向工程 说明
- 工作感言:任务分配及管理
- Spring各个jar包作用