首页 > Okhttp get 请求

Okhttp get 请求

Okhttp 的普通的Get请求如下:

OkHttpClient client = new OkHttpClient();                //新建客户端
            Request request = new Request.Builder()                    //新建请求
                    .get()                                                    //get请求
                    .url("http://publicobject.com/helloworld.txt")            //URL
                    .build();                                                
            Response response = client.newCall(request).execute();            //返回对象
            if (response.isSuccessful()) {                                    //阻塞线程。
                Log.e("code",":"+response.code());
                Log.e("body",response.body().string());
            }
            else {
                Log.e("---","不成功");
            }
   这是同步的。
   要是我相传入Get请求的参数怎么做?好像找不到这个API,还是说,直接手动链接到请求的URL中嘛?
   还有,我找不到官方的Okhttp的API文档,有哪位大神方便提供提供吗?     

找了半天好像也没有。。。
网上的大神都是自己弄一个工具:

/**
 * Created by Chestnut on 2016/6/24.
 */
public class OkhttpUtils {


    /**
     * 为HttpGet 的 url 方便的添加多个name value 参数。
     * @param url
     * @param params
     * @return
     */
    public static String attachHttpGetParams(String url, LinkedHashMap<String,String> params){

        Iterator<String> keys = params.keySet().iterator();
        Iterator<String> values = params.values().iterator();
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("?");

        for (int i=0;i<params.size();i++ ) {
            stringBuffer.append(keys.next()+"="+values.next());
            if (i!=params.size()-1) {
                stringBuffer.append("&");
            }
        }

        return url + stringBuffer.toString();
    }


    /**
     * 为HttpGet 的 url 方便的添加1个name value 参数。
     * @param url
     * @param name
     * @param value
     * @return
     */
    public static String attachHttpGetParam(String url, String name, String value){
        return url + "?" + name + "=" + value;
    }


}

这是我写的,欢迎吐槽。。。


其实就是自己拼接url+querystring


用HttpUrl.Builder

Request.Builder reqBuild = new Request.Builder();
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
urlBuilder.addQueryParameter(key, value);
reqBuild.url(urlBuilder.build());

get方式在Android上常用的方式是url后面加参数,
看了OKHttp的源码,他是默认get方式没有RequestBody参数.一下摘自部分OKHttp源码

public Builder get() {
      return method("GET", null);
    }

调用了一个method方法

public Builder method(String method, RequestBody body) {
      if (method == null || method.length() == 0) {
        throw new IllegalArgumentException("method == null || method.length() == 0");
      }
      if (body != null && !HttpMethod.permitsRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must not have a request body.");
      }
      if (body == null && HttpMethod.requiresRequestBody(method)) {
        throw new IllegalArgumentException("method " + method + " must have a request body.");
      }
      this.method = method;
      this.body = body;
      return this;
    }

您可以尝试修改源码,自行加一个get_demo(String method, RequestBody body)方法,里面return method("get",requestbody) 测试看看能否成功

对了,如果非必要get方法,我更推荐post方法.

【热门文章】
【热门文章】