search

Home  >  Q&A  >  body text

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

reply all(4)I'll reply

  • 天蓬老师

    天蓬老师2017-04-17 17:34:13

    用HttpUrl.Builder

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

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:34:13

    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. . .

    reply
    0
  • ringa_lee

    ringa_lee2017-04-17 17:34:13

    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.

    reply
    0
  • 大家讲道理

    大家讲道理2017-04-17 17:34:13

    In fact, you just splice the url+querystring yourself

    reply
    0
  • Cancelreply