Android Http請求框架二:xUtils 框架網路請求

趙彥軍發表於2015-05-04

一:對Http不瞭解的請看

Android Http請求框架一:Get 和 Post 請求

 

二、正文

1、xUtils 下載地址

    github 下載地址  : https://github.com/wyouflf/xUtils

 

2、關於網路請求的方法

package com.jike.shanglv.NetAndJson;

import java.io.File;

import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseInfo;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.http.callback.RequestCallBack;
import com.lidroid.xutils.http.client.HttpRequest;
import com.lidroid.xutils.util.LogUtils;

public class HttpUtil {

	String result = "" ;

	/**
	 * Get請求  非同步的
	 * @param url  伺服器地址
	 * @param userkey 
	 * @param str
	 * @param sign 簽名
	 * @return
	 */
	public String xutilsGet( String url , String userkey , String str , String sign  ){
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("userkey", userkey );
		params.addQueryStringParameter("str", str );
		params.addQueryStringParameter("sign", sign );
		HttpUtils http = new HttpUtils();
		http.configCurrentHttpCacheExpiry(1000 * 10); //設定超時時間   10s
		http.send(HttpRequest.HttpMethod.GET,
				url ,
				new RequestCallBack<String>(){
			@Override
			public void onLoading(long total, long current, boolean isUploading) {

			}

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

			@Override
			public void onStart() {
			}

			@Override
			public void onFailure(HttpException error, String msg) {
			}
		});

		return result ;
	}

	/**
	 * Post請求 非同步的
	 * @param url
	 * @param userkey
	 * @param str
	 * @param sign
	 * @return
	 */
	public String xutilsPost( String url , String userkey , String str , String sign ){
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("userkey", userkey );
		params.addQueryStringParameter("str", str );
		params.addQueryStringParameter("sign", sign );

		// 只包含字串引數時預設使用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));

		HttpUtils http = new HttpUtils();
		http.configCurrentHttpCacheExpiry(1000 * 10); //設定超時時間   10s  
		http.send(HttpRequest.HttpMethod.POST ,
				url ,
				params,
				new RequestCallBack<String>() {

			@Override
			public void onStart() {
			}

			@Override
			public void onLoading(long total, long current, boolean isUploading) {
			}

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

			@Override
			public void onFailure(HttpException error, String msg) {

			}
		});

		return result ;
	}

	/**
	 * 帶上傳檔案的 Post請求   非同步的
	 * @param url
	 * @param userkey
	 * @param str
	 * @param sign
	 * @param picString  檔案的地址
	 * @return
	 */
	public String xutilsFilePost( String url , String userkey , String str , String sign , String picString ){
		RequestParams params = new RequestParams();
		params.addQueryStringParameter("userkey", userkey );
		params.addQueryStringParameter("str", str );
		params.addQueryStringParameter("sign", sign );

		// 只包含字串引數時預設使用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("picture", new File( picString )) ;

		com.lidroid.xutils.HttpUtils http = new com.lidroid.xutils.HttpUtils();
		http.send(HttpRequest.HttpMethod.POST ,
				url ,
				params,
				new RequestCallBack<String>() {

			@Override
			public void onStart() {
			}

			@Override
			public void onLoading(long total, long current, boolean isUploading) {
			}

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

			@Override
			public void onFailure(HttpException error, String msg) {
			}
		});

		return result ;
	}

	//-------------------以上的程式碼 是 非同步請求的, 以下的程式碼是同步請求的-------------------------//
/** * Get同步請求 必須在非同步塊兒中執行 * @param url * @param userkey * @param str * @param sign * @return */ private String xutilsGetSync(String url , String userkey , String str , String sign ) { RequestParams params = new RequestParams(); params.addQueryStringParameter("userkey", userkey ); params.addQueryStringParameter("str", str ); params.addQueryStringParameter("sign", sign ); HttpUtils http = new HttpUtils() ; http.configCurrentHttpCacheExpiry(1000 * 10); //設定超時時間 try { ResponseStream responseStream = http.sendSync(HttpRequest.HttpMethod.GET, url , params ) ; //int statusCode = responseStream.getStatusCode(); //Header[] headers = responseStream.getBaseResponse().getAllHeaders(); return responseStream.readString(); } catch (Exception e) { LogUtils.e(e.getMessage(), e); } return null; } /** * Post同步請求 必須在非同步塊兒中執行 * @param url * @param userkey * @param str * @param sign * @return */ private String xutilsPostSync(String url , String userkey , String str , String sign ) { RequestParams params = new RequestParams(); params.addQueryStringParameter("userkey", userkey ); params.addQueryStringParameter("str", str ); params.addQueryStringParameter("sign", sign ); HttpUtils http = new HttpUtils() ; http.configCurrentHttpCacheExpiry(1000 * 10); //設定超時時間 try { ResponseStream responseStream = http.sendSync(HttpRequest.HttpMethod.POST , url , params ) ; //int statusCode = responseStream.getStatusCode(); //Header[] headers = responseStream.getBaseResponse().getAllHeaders(); return responseStream.readString(); } catch (Exception e) { LogUtils.e(e.getMessage(), e); } return null; } }

 

相關文章