81,092
社区成员
发帖
与我相关
我的任务
分享
private static final int TIMEOUT = 10000;// 10秒
/**
* 公用的调用后台的服务
*
* @param url
* @param params
* @return
*/
public static String callService(String url, List<NameValuePair> params) {
HttpParams httpParameters = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpParameters, TIMEOUT);
HttpConnectionParams.setSoTimeout(httpParameters, TIMEOUT);
HttpClient client = new DefaultHttpClient(httpParameters);
// 提拱默认的HttpClient实现
HttpPost request;
String returnString = "error";
try {
request = new HttpPost(new URI(url));
// 设置HTTP POST请求参数
request.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
HttpResponse response = client.execute(request);
// 判断请求是否成功
if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { // 200表示请求成功
HttpEntity entity = response.getEntity();
if (entity != null) {
returnString = EntityUtils.toString(entity, "UTF-8");
}
} else {
return "10" + response.getStatusLine().getStatusCode();
}
// 链接失败
} catch (HttpHostConnectException e) {
e.printStackTrace();
return "10061";
// Socket链接超时
} catch (SocketTimeoutException e) {
e.printStackTrace();
return "10060";
// Http链接超时
} catch (ConnectTimeoutException e) {
e.printStackTrace();
return "10059";
// 其他异常
} catch (Exception e) {
e.printStackTrace();
return returnString;
}
return returnString;
}
public List<RepairStationEntity> getStationDatas(RepairStationEntity entity)
throws JSONException {
List<RepairStationEntity> entities = new ArrayList<RepairStationEntity>();
String url = MyConstants.URL + MyConstants.ACTION_GETDATAS;
String msg = entity.getStationLatitude() + ","
+ entity.getStationLongitude();
List<NameValuePair> params = new ArrayList<NameValuePair>();
NameValuePair mNameValuePair = new BasicNameValuePair("padParam", msg);
params.add(mNameValuePair);
String result = NetTool.callService(url, params);
Log.e("----->", result);
if ("\"404\"".equals(result)) {
throw new BusinessException("周围2公里内没有查询到维修点数据,\n请滑动地图切换区域后再试!");
} else if ("\"403\"".equals(result)) {
throw new BusinessException("服务端未知错误!");
} else if (result.substring(0, 2).equals("10")) {
throw new HttpStatusException("HTTP链接异常,错误代码:" + result);
}
// 获得JSONTokener对象
JSONTokener jsonTokener = new JSONTokener(result);
// 获得其中的JSONObject对象
JSONObject jsonObject = (JSONObject) jsonTokener.nextValue();
JSONArray jsonArray = jsonObject.getJSONArray("nwxdata");
for (int i = 0; i < jsonArray.length(); i++) {
RepairStationEntity mEntity = new RepairStationEntity();
JSONObject object = jsonArray.getJSONObject(i);
mEntity.setStationId(object.getInt("nwxstationid"));
mEntity.setStationName(object.getString("nwxstationname"));
mEntity.setStationAddStr(object.getString("nwxstationaddress"));
mEntity.setStationImgSrc(object.getString("nwxstationimgsrc"));
mEntity.setStationLittleImgSrc(object
.getString("nwxstationlittleimgsrc"));
mEntity.setStationTel(object.getString("nwxstationtel"));
mEntity.setStationLatitude(object.getDouble("nwxstationx"));
mEntity.setStationLongitude(object.getDouble("nwxstationy"));
mEntity.setStationSayNum(object.getInt("saynum"));
entities.add(mEntity);
}
return entities;
}