OkHttp 同步get

/**
	 * 下载一个文件,打印他的响应头,以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):                                                      @@:




  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值