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

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级讲师

全部回覆(4)
Peter_Zhu

用HttpUrl.Builder

雷雷
大家讲道理

找了半天好像也沒有。 。 。
網路上的大神都是自己弄一個工具:

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


}

這是我寫的,歡迎吐槽。 。 。

左手右手慢动作

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

大家讲道理

其實就是自己拼接url+querystring

熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板