XUtils框架中发送带参数的Get请求

/**
 * 物管界面的信息
 * 
 * @author jason.zhan
 *
 */
public class LogisticsService extends BaseService {

	/**
	 * 获取物管信息
	 * @return
	 */
	public ApiResult getLogisticsInformations(String user_uuid, String auth_uuid, String neibourhood_id) {
		RequestParams params = new RequestParams();
		ApiResult apiResult = null;
		try {                       
			// 后来才知道,选择addQueryStringParameter还是addBodyParameter是根据服务器那边是否有url param来定的。
			params.addQueryStringParameter("user_uuid", user_uuid);
			params.addQueryStringParameter("auth_uuid", auth_uuid);
			params.addQueryStringParameter("neibourhood_id", neibourhood_id);
			String response = execute(HttpMethod.GET, ApiUrl.URL_LOGISTICS, params);
			apiResult = JsonHelper.convert(response, new TypeReference<ApiResult>(){});
		} catch (Exception e) {
			e.printStackTrace();
		}
		return apiResult;
	}
}
execute()方法如下:
import java.io.IOException;

import android.util.Log;

import com.lidroid.xutils.DbUtils;
import com.lidroid.xutils.HttpUtils;
import com.lidroid.xutils.exception.HttpException;
import com.lidroid.xutils.http.RequestParams;
import com.lidroid.xutils.http.ResponseStream;
import com.lidroid.xutils.http.client.HttpRequest.HttpMethod;
import com.woyoli.ApiUrl;
import com.woyoli.Constant;
import com.woyoli.JsonHelper;
import com.woyoli.WoyoliApp;
import com.woyoli.models.ApiResult;
import com.woyoli.models.ResponseStatus;

public abstract class ServiceBase{
	
	private HttpUtils httpClient;
	
	protected DbUtils getDbUtils(){
		return WoyoliApp.getDbUtils();
	}
	
	protected HttpUtils getHttpClient(){
		if(httpClient==null){
			httpClient = new HttpUtils();	
		}
		
		return httpClient;
	}
	
	protected  <T> T execute(HttpMethod method, String url, RequestParams params, Class<T> type) throws HttpException, IOException{
		String apiUrl = ApiUrl.API_DOMAIN + url;	
		ResponseStream stream = getHttpClient().sendSync(method, apiUrl, params);
		String content = stream.readString();	
		Log.d(Constant.DEBUG_TAG, content);
		ApiResult result = JsonHelper.convert(content, ApiResult.class);
		if(ResponseStatus.SUCCESS.equals(result.getStatus())){
			return JsonHelper.convert(result.getData().toString(), type);
		}else{
			
			throw new HttpException(result.getMessage());
		}
	}
		
	protected  String execute(HttpMethod method, String url, RequestParams params) throws HttpException, IOException{
		String apiUrl = ApiUrl.API_DOMAIN + url;
		ResponseStream stream = getHttpClient().sendSync(method, apiUrl, params);
		String content = stream.readString();
		Log.d(Constant.DEBUG_TAG, content);
		return content;
	}
	
	protected  <T> T executeWholeUrl(HttpMethod method, String url, RequestParams params, Class<T> type) throws HttpException, IOException{
		ResponseStream stream = getHttpClient().sendSync(method, url, params);
		String content = stream.readString();	
		Log.d(Constant.DEBUG_TAG, content);
		ApiResult result = JsonHelper.convert(content, ApiResult.class);
		if(ResponseStatus.SUCCESS.equals(result.getStatus())){
			return JsonHelper.convert(result.getData().toString(), type);
		}else{
			
			throw new HttpException(result.getMessage());
		}
	}
	
	protected  String executeWholeUrl(HttpMethod method, String url, RequestParams params) throws HttpException, IOException{
		ResponseStream stream = getHttpClient().sendSync(method, url, params);
		String content = stream.readString();
		Log.d(Constant.DEBUG_TAG, content);
		return content;
	}
	
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

ithouse

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值