Maison > Questions et réponses > le corps du texte
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文档,有哪位大神方便提供提供吗?
天蓬老师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());
大家讲道理2017-04-17 17:34:13
Après avoir longtemps cherché, je n’arrive pas à le trouver. . .
Les grands maîtres d'Internet fabriquent tous leurs propres outils :
/**
* 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;
}
}
C'est ce que j'ai écrit. Les commentaires sont les bienvenus. . .
ringa_lee2017-04-17 17:34:13
La méthode get couramment utilisée sur Android consiste à ajouter des paramètres après l'url.
Après avoir regardé le code source de OKHttp, c'est la méthode get par défaut sans le paramètre RequestBody. Code source OKHttp
public Builder get() {
return method("GET", null);
}
Appeler une méthode
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;
}
Vous pouvez essayer de modifier le code source, ajouter une méthode get_demo(String method, RequestBody body) et un test inside return method("get", requestbody) pour voir s'il réussit
D'ailleurs, si la méthode get n'est pas nécessaire, je recommande la méthode post.
大家讲道理2017-04-17 17:34:13
En fait, vous assemblez simplement vous-même l'url la chaîne de requête