码迷,mamicode.com
首页 > Web开发 > 详细

OKHttp简单使用

时间:2016-05-20 11:13:45      阅读:196      评论:0      收藏:0      [点我收藏+]

标签:

官网:http://square.github.io/okhttp/

参考地址:http://www.jcodecraeer.com/a/anzhuokaifa/androidkaifa/2015/0106/2275.html

封装:http://blog.csdn.net/lmj623565791/article/details/47911083

Http Get 同步方式

This program downloads a URL and print its contents as a string.

URL访问,放回结果以String形式显示。

OkHttpClient client = new OkHttpClient();

String run(String url) throws IOException {
  Request request = new Request.Builder()
      .url(url)
      .build();

  Response response = client.newCall(request).execute();
  return response.body().string();
}

Http POST 同步方式

This program posts data to a service. 

提交数据到服务器,放回结果以String形式显示,以下例子没有数据提交。

public static final MediaType JSON
    = MediaType.parse("application/json; charset=utf-8");

OkHttpClient client = new OkHttpClient();

String post(String url, String json) throws IOException {
  RequestBody body = RequestBody.create(JSON, json);
  Request request = new Request.Builder()
      .url(url)
      .post(body)
      .build();
  Response response = client.newCall(request).execute();
  return response.body().string();
}
返回结果:
1.以String形式,默认以UTF-8形式解析
public final String string() throws IOException
Returns the response as a string decoded with the charset of the Content-Type header. If that header is either absent or lacks a charset,
this will attempt to decode the response body as UTF-8.Throws:
IOException

2.以流的形式

public final InputStream byteStream()

Http POST 同步方式

提交数据到服务器,放回结果以String形式显示,以下例子有数据提交。

OkHttpClient client = new OkHttpClient();
String post(String url, String json) throws IOException {
 
     RequestBody formBody = new FormEncodingBuilder()
                               .add("platform", "android")
                               .add("name", "bug")
                               .add("subject", "XXXXXXXXXXXXXXX")
                               .build();
 
      Request request = new Request.Builder()
                           .url(url)
                           .post(body)
                           .build();
 
      Response response = client.newCall(request).execute();
    if (response.isSuccessful()) {
        return response.body().string();
    } else {
        throw new IOException("Unexpected code " + response);
    }
}

使用步骤:

第一步:创建OkHttpClient

第二步:创建Request

第三步:发送请求,等待返回结果:Response response = client.newCall(request).execute();

Andoird不推荐用同步方式去请求数据,Android本身不允许,要么就用要另开线程。用框架的异步方式去请求。

注意:

  • OkHttp官方文档并不建议我们创建多个OkHttpClient,因此全局使用一个。 如果有需要,可以使用clone方法,再进行自定义。

  • enqueue为OkHttp提供的异步方法,入门教程中并没有提到。

异步方式

异步方式和同步方式相同,第三步的execute()更为enqueue()就行,重写你们回调方法即可,这里是另开线程,所以不能更新UI。

        // 异步执行网络请求
        mOkHttpClient.newCall(request).enqueue(new Callback() {

            @Override
            public void onFailure(Request arg0, IOException arg1) {
                Log.d("h_bl", "onFailure");
            }

            @Override
            public void onResponse(Response response) throws IOException {
                String result = response.body().string();
                Log.d("h_bl", result);
            }
        });

 

OKHttp简单使用

标签:

原文地址:http://www.cnblogs.com/H-BolinBlog/p/5511065.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!