1. 程式人生 > >Xutils和Xutils3的基本使用

Xutils和Xutils3的基本使用

  /**普通get方法**/
	HttpUtils http = new HttpUtils();
	http.send(HttpRequest.HttpMethod.GET,
		"http://www.lidroid.com",
		new RequestCallBack<String>(){
			@Override
			public void onLoading(long total, long current, boolean isUploading) {
				testTextView.setText(current + "/" + total);
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				textView.setText(responseInfo.result);
			}

			@Override
			public void onStart() {
			}

			@Override
			public void onFailure(HttpException error, String msg) {
			}
	});
	
	/**使用HttpUtils上傳檔案 或者 提交資料 到伺服器(post方法)**/
	RequestParams params = new RequestParams();
	params.addHeader("name", "value");
	params.addQueryStringParameter("name", "value");

	// 只包含字串引數時預設使用BodyParamsEntity,
	// 類似於UrlEncodedFormEntity("application/x-www-form-urlencoded")。
	params.addBodyParameter("name", "value");

	// 加入檔案引數後預設使用MultipartEntity("multipart/form-data"),
	// 如需"multipart/related",xUtils中提供的MultipartEntity支援設定subType為"related"。
	// 使用params.setBodyEntity(httpEntity)可設定更多型別的HttpEntity(如:
	// MultipartEntity,BodyParamsEntity,FileUploadEntity,InputStreamUploadEntity,StringEntity)。
	// 例如傳送json引數:params.setBodyEntity(new StringEntity(jsonStr,charset));
	params.addBodyParameter("file", new File("path"));

	HttpUtils http = new HttpUtils();
	http.send(HttpRequest.HttpMethod.POST,
		"uploadUrl....",
		params,
		new RequestCallBack<String>() {

			@Override
			public void onStart() {
				testTextView.setText("conn...");
			}

			@Override
			public void onLoading(long total, long current, boolean isUploading) {
				if (isUploading) {
					testTextView.setText("upload: " + current + "/" + total);
				} else {
					testTextView.setText("reply: " + current + "/" + total);
				}
			}

			@Override
			public void onSuccess(ResponseInfo<String> responseInfo) {
				testTextView.setText("reply: " + responseInfo.result);
			}

			@Override
			public void onFailure(HttpException error, String msg) {
				testTextView.setText(error.getExceptionCode() + ":" + msg);
			}
	});														
	/**使用HttpUtils下載檔案:支援斷點續傳,隨時停止下載任務,開始任務**/
	HttpUtils http = new HttpUtils();
	HttpHandler handler = http.download("http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip",
		"/sdcard/httpcomponents-client-4.2.5-src.zip",
		true, // 如果目標檔案存在,接著未完成的部分繼續下載。伺服器不支援RANGE時將從新下載。
		true, // 如果從請求返回資訊中獲取到檔名,下載完成後自動重新命名。
		new RequestCallBack<File>() {

			@Override
			public void onStart() {
				testTextView.setText("conn...");
			}

			@Override
			public void onLoading(long total, long current, boolean isUploading) {
				testTextView.setText(current + "/" + total);
			}

			@Override
			public void onSuccess(ResponseInfo<File> responseInfo) {
				testTextView.setText("downloaded:" + responseInfo.result.getPath());
			}


			@Override
			public void onFailure(HttpException error, String msg) {
				testTextView.setText(msg);
			}
	});
	//呼叫cancel()方法停止下載
	handler.cancel();