android - Okhttp get 请求
PHP中文网
PHP中文网 2017-04-17 17:32:13
0
4
643

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文档,有哪位大神方便提供提供吗?     
PHP中文网
PHP中文网

认证0级讲师

reply all(4)
Peter_Zhu

用HttpUrl.Builder

Request.Builder reqBuild = new Request.Builder();
HttpUrl.Builder urlBuilder = HttpUrl.parse(url).newBuilder();
urlBuilder.addQueryParameter(key, value);
reqBuild.url(urlBuilder.build());
大家讲道理

After looking for a long time, I can’t seem to find it. . .
The great masters on the Internet all make their own tools:

/**
 * 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;
    }


}

This is what I wrote. Comments are welcome. . .

左手右手慢动作

The commonly used method of get method on Android is to add parameters after the url.
Looking at the source code of OKHttp, it is the default get method without the RequestBody parameter. It is excerpted from part of the OKHttp source code

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

Call a 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;
    }

You can try to modify the source code, add a get_demo(String method, RequestBody body) method, and inside return method("get", requestbody) test to see if it succeeds

By the way, if the get method is not necessary, I prefer the post method.

大家讲道理

In fact, you just splice the url+querystring yourself

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template