Connection →Keep-Alive
Content-Length →227
Content-Type →text/html
Date →Wed, 05 Sep 2018 03:41:58 GMT
Etag →"5b7b7f40-e3"
Last-Modified →Tue, 21 Aug 2018 02:56:00 GMT
Pragma →no-cache
Server →BWS/1.1
Set-Cookie →BD_NOT_HTTPS=1; path=/; Max-Age=300
Strict-Transport-Security →max-age=0
X-Ua-Compatible →IE=Edge,chrome=1
- 如果失败的话,那么就会在onFailure中把异常反馈给你!!!
给你贴下整体代码吧!
/1.创建OkHttpClient对象/
OkHttpClient httpClient = new OkHttpClient();
/2.创建请求Request内容/
Request request = new Request.Builder()
.method(“GET”, null)
.url(“https://www.baidu.com/”)
.build();
/3.发送请求/
Call call = httpClient.newCall(request);
/4.创建请求的回调/
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Log.e(TAG, “请求失败的原因:” + e);
}
@Override
public void onResponse(Call call, final Response response) throws IOException {
Headers headers = response.headers();
Set names = headers.names();
for (String name : names) {
Log.e(TAG, “请求的header” + name);
String value = headers.get(name);
Log.e(TAG, "值为: " + value + “\n----------------------------------”);
}
final String date = response.body().string();
mHandler.post(new Runnable() {
@Override
public void run() {
mTvShow.setText(date);
}
});
}
});
以上步骤就能正常请求相应的数据了,如果还没有数据的话,好好看看代码!
1.2 简单的POST请求
关于POST请求的话,基本上就是比GET请求多一步设置表单的方法,也就是一个FormBody对象的设置,以key、value的方式设置表单而已,所以这里教你怎么写,然后我贴一下代码就那么滴了,谁让我那么懒呢!!!
表单的写法是这样的:
FormBody formBody = new FormBody.Builder()
.add(“key”, “value”)
.build();
其实add方法可以被调用多次,添加相应的key和value;
整体的代码是这样的!!!
/1.创建OkHttpClient对象/
OkHttpClient httpClient = new OkHttpClient();
/2.创建相应的表单内容/
FormBody formBody = new FormBody.Builder()
.add(“key”, “value”)
.build();
/3.创建请求Request内容/
Request request = new Request.Builder()
.url(“https://www.baidu.com/”)
.post(formBody)
.build();
/4.发送请求/
Call call = httpClient.newCall(request);
/5.创建请求的回调/
call.enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
Lo