android 网络通信 okHttp

okhttp 是一个 Java 的 HTTP+SPDY 客户端开发包,同时也支持 Android。需要Android 2.3以上。

特点
  • OKHttp是Android版Http客户端。非常高效,支持SPDY、连接池、GZIP和 HTTP 缓存。
  • 默认情况下,OKHttp会自动处理常见的网络问题,像二次连接、SSL的握手问题。
  • 如果你的应用程序中集成了OKHttp,Retrofit默认会使用OKHttp处理其他网络层请求。
  • 从Android4.4开始HttpURLConnection的底层实现采用的是okHttp.
  • 同步:发送http请求→获取返回结果→分析结果→跳转下一页
    异步:发送http请求→跳转下一页(不需要等待请求结果,对结果的处理在另一个线程中)
    同步是用主线程去访问;异步是用子线程去访问,主线程继续工作 
    若是的同步的话会卡死ui界面,
    Android4.1以后不允许使用同步请求了
用法
  1. 新建一个OkHttpClient对象
  2. 通过Request.Builder对象新建一个Request对象
  3. 返回执行结果

    • GET
private String get(String url) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

 

  • POST

POST需要使用RequestBody对象,之后再构建Request对象时调用post函数将其传入即可

private String post(String url) {
        OkHttpClient client = new OkHttpClient();

        RequestBody formBody = new FormEncodingBuilder()
                .add("user", "Jurassic Park")
                .add("pass", "asasa")
                .add("time", "12132")
                .build();
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            return response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

 

此外,post的使用方法还支持文件等操作,具体使用方法有兴趣的可以自行查阅

  • 对Gson的支持

okHttp还自带了对Gson的支持

  private Person gson(String url){
        OkHttpClient client = new OkHttpClient();
        Gson gson = new Gson();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = null;
        try {
            response = client.newCall(request).execute();
            Person person = gson.fromJson(response.body().charStream(), Person.class);
            return person;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

 

  • 异步操作

以上的两个例子必须在子线程中完成,同时okHttp还提供了异步的方法调用,通过使用回调来进行异步调用,然后okHttp的回调依然不在主线程中,因此该回调中不能操作UI

private void getAsync(String url) {
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .url(url)
                .build();
        Response response = null;

        client.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Request request, IOException e) {

            }

            @Override
            public void onResponse(Response response) throws IOException {
                String result = response.body().string();
                Toast.makeText(getApplicationContext(),result,Toast.LENGTH_SHORT).show();
                //不能操作ui,回调依然在子线程
                Log.d("TAG", result);
            }
        });


    }

 http://www.open-open.com/lib/view/open1435381866122.html

转载于:https://www.cnblogs.com/pbq-dream/p/5371127.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值