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文档,有哪位大神方便提供提供吗?
用HttpUrl.Builder
After looking for a long time, I can’t seem to find it. . .
The great masters on the Internet all make their own tools:
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
Call a method
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