/**
* 物管界面的信息
*
* @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;
}
}