您的位置:首页 > 理论基础 > 计算机网络

OkHttp 同步get

2016-02-25 11:57 375 查看
/**
* 下载一个文件,打印他的响应头,以string形式打印响应体。 响应体的 string()
* 方法对于小文档来说十分方便、高效。但是如果响应体太大(超过1MB),应避免适应 string()方法 ,因为他会将把整个文档加载到内存中。
* 对于超过1MB的响应body,应使用流的方式来处理body。<br>
* 方 法 名:SynchronousGetrun <br>
* 创 建 人: <br>
* 创建时间:2016-2-25 上午11:48:53 <br>
* 修 改 人: <br>
* 修改日期: <br>
* @throws Exception void
*/
public void SynchronousGetrun() throws Exception {
Request request = new Request.Builder().url("https://publicobject.com/helloworld.txt").build();
Response response = client.newCall(request).execute();
if (!response.isSuccessful())
throw new IOException("Unexpected code " + response);

Headers responseHeaders = response.headers();
for (int i = 0; i < responseHeaders.size(); i++) {
Log.i("TAG4", responseHeaders.name(i) + ": " + responseHeaders.value(i));
}
Log.i("TAG4", response.body().string());
}
按钮监听:
public void SynchronousGetRun(View v) throws Exception {
getExecuteAsyncTask asyncTask2 = new getExecuteAsyncTask();
asyncTask2.execute(3);
}
private class getExecuteAsyncTask extends AsyncTask<Integer, Integer, Integer> {

@Override
protected Integer doInBackground(Integer... param) {
switch (param[0]) {
case 1:
try {
getExecute();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 2:
try {
run();
} catch (Exception e) {
e.printStackTrace();
}
break;
case 3:
try {
<span style="color:#ff6666;">SynchronousGetrun();</span>
} catch (Exception e) {
e.printStackTrace();
}
break;

default:
break;
}

return 1;
}

@Override
protected void onPostExecute(Integer result) {
super.onPostExecute(result);
}
}
//打印日志:
02-26 11:54:51.825: I/TAG4(12956): <span style="color:#ff6666;">Server: nginx/1.4.6 (Ubuntu)</span>
02-26 11:54:51.825: I/TAG4(12956): <span style="color:#ff6666;">Date: Thu, 25 Feb 2016 03:46:30 GMT</span>
02-26 11:54:51.825: I/TAG4(12956): <span style="color:#ff6666;">Content-Type: text/plain</span>
02-26 11:54:51.825: I/TAG4(12956): <span style="color:#ff0000;">Content-Length: 1759</span>
02-26 11:54:51.825: I/TAG4(12956): Last-Modified: Tue, 27 May 2014 02:35:47 GMT
02-26 11:54:51.825: I/TAG4(12956): Connection: keep-alive
02-26 11:54:51.825: I/TAG4(12956): ETag: "5383fa03-6df"
02-26 11:54:51.825: I/TAG4(12956): Accept-Ranges: bytes
02-26 11:54:51.825: I/TAG4(12956): OkHttp-Sent-Millis: 1456458890752
02-26 11:54:51.825: I/TAG4(12956): OkHttp-Received-Millis: 1456458891833
02-26 11:54:51.825: I/TAG4(12956):                          \\           //
02-26 11:54:51.825: I/TAG4(12956):                           \\  .ooo.  //
02-26 11:54:51.825: I/TAG4(12956):                            .@@@@@@@@@.
02-26 11:54:51.825: I/TAG4(12956):                          :@@@@@@@@@@@@@:
02-26 11:54:51.825: I/TAG4(12956):                         :@@. '@@@@@' .@@:
02-26 11:54:51.825: I/TAG4(12956):                         @@@@@@@@@@@@@@@@@
02-26 11:54:51.825: I/TAG4(12956):                         @@@@@@@@@@@@@@@@@
02-26 11:54:51.825: I/TAG4(12956):                    :@@ :@@@@@@@@@@@@@@@@@. @@:
02-26 11:54:51.825: I/TAG4(12956):                    @@@ '@@@@@@@@@@@@@@@@@, @@@
02-26 11:54:51.825: I/TAG4(12956):                    @@@ '@@@@@@@@@@@@@@@@@, @@@
02-26 11:54:51.825: I/TAG4(12956):                    @@@ '@@@@@@@@@@@@@@@@@, @@@
02-26 11:54:51.825: I/TAG4(12956):                    @@@ '@@@@@@@@@@@@@@@@@, @@@
02-26 11:54:51.825: I/TAG4(12956):                    @@@ '@@@@@@@@@@@@@@@@@, @@@
02-26 11:54:51.825: I/TAG4(12956):                    @@@ '@@@@@@@@@@@@@@@@@, @@@
02-26 11:54:51.825: I/TAG4(12956):                         @@@@@@@@@@@@@@@@@
02-26 11:54:51.825: I/TAG4(12956):                         '@@@@@@@@@@@@@@@'
02-26 11:54:51.825: I/TAG4(12956):                            @@@@   @@@@
02-26 11:54:51.825: I/TAG4(12956):                            @@@@   @@@@
02-26 11:54:51.825: I/TAG4(12956):                            @@@@   @@@@
02-26 11:54:51.825: I/TAG4(12956):                            '@@'   '@@'
02-26 11:54:51.825: I/TAG4(12956):      :@@@.
02-26 11:54:51.825: I/TAG4(12956):    .@@@@@@@:   +@@       `@@      @@`   @@     @@
02-26 11:54:51.825: I/TAG4(12956):   .@@@@'@@@@:  +@@       `@@      @@`   @@     @@
02-26 11:54:51.825: I/TAG4(12956):   @@@     @@@  +@@       `@@      @@`   @@     @@
02-26 11:54:51.825: I/TAG4(12956):  .@@       @@: +@@   @@@ `@@      @@` @@@@@@ @@@@@@  @@;@@@@@
02-26 11:54:51.825: I/TAG4(12956):  @@@       @@@ +@@  @@@  `@@      @@` @@@@@@ @@@@@@  @@@@@@@@@
02-26 11:54:51.825: I/TAG4(12956):  @@@       @@@ +@@ @@@   `@@@@@@@@@@`   @@     @@    @@@   :@@
02-26 11:54:51.825: I/TAG4(12956):  @@@       @@@ +@@@@@    `@@@@@@@@@@`   @@     @@    @@#    @@+
02-26 11:54:51.825: I/TAG4(12956):  @@@       @@@ +@@@@@+   `@@      @@`   @@     @@    @@:    @@#
02-26 11:54:51.825: I/TAG4(12956):   @@:     .@@` +@@@+@@   `@@      @@`   @@     @@    @@#    @@+
02-26 11:54:51.825: I/TAG4(12956):   @@@.   .@@@  +@@  @@@  `@@      @@`   @@     @@    @@@   ,@@
02-26 11:54:51.825: I/TAG4(12956):    @@@@@@@@@   +@@   @@@ `@@      @@`   @@@@   @@@@  @@@@#@@@@
02-26 11:54:51.825: I/TAG4(12956):     @@@@@@@    +@@   #@@ `@@      @@`   @@@@:  @@@@: @@'@@@@@
02-26 11:54:51.825: I/TAG4(12956):                                                      @@:
02-26 11:54:51.825: I/TAG4(12956):                                                      @@:
02-26 11:54:51.825: I/TAG4(12956):                                                      @@:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: