Android Http 요청 프레임 워 크 2: xUtils 프레임 워 크 네트워크 요청

8251 단어 android
Http 에 대해 모 르 는 것 은 보 세 요.
Android Http 요청 프레임 워 크 1: 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; } }

좋은 웹페이지 즐겨찾기