public static String httpGet(String url, String token) {
HttpClient hc = new DefaultHttpClient();
String body = null;
try {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("token", token);
HttpResponse response = hc.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity, "utf-8");
}
} catch (ParseException e) {
System.out.println(e.getMessage());
body = "1";
} catch (UnsupportedEncodingException e) {
System.out.println(e.getMessage());
body = "2";
} catch (ClientProtocolException e) {
System.out.println(e.getMessage());
body = "3";
} catch (IOException e) {
System.out.println(e.getMessage());
body = "4";
}
return body;
}String url = "http://www.xx.com";
String param = "{\"appId\":\"11111\",\"secrectKey\":\"11111\",\"userName\":\"3333\"}";
String token="asdasf";如何传递json参数,求大神指点
HttpClient hc = new DefaultHttpClient();
String body = null;
try {
HttpGet httpGet = new HttpGet(url);
httpGet.setHeader("token", token);
HttpResponse response = hc.execute(httpGet);
HttpEntity entity = response.getEntity();
if (entity != null) {
body = EntityUtils.toString(entity, "utf-8");
}
} catch (ParseException e) {
System.out.println(e.getMessage());
body = "1";
} catch (UnsupportedEncodingException e) {
System.out.println(e.getMessage());
body = "2";
} catch (ClientProtocolException e) {
System.out.println(e.getMessage());
body = "3";
} catch (IOException e) {
System.out.println(e.getMessage());
body = "4";
}
return body;
}String url = "http://www.xx.com";
String param = "{\"appId\":\"11111\",\"secrectKey\":\"11111\",\"userName\":\"3333\"}";
String token="asdasf";如何传递json参数,求大神指点
解决方案 »
- 整合SSH2框架时出现的问题
- 怎么用过滤器过滤特定目录下的jsp文件啊
- 个位师兄,jsp中怎么做图形报表?
- 一个很奇怪的问题(在线等,解决立刻结单)
- oracle中long字段中检索问题???<在线等待>
- javabean <jsp:setProperty name="demand" property="*" /> 方式出现的问题, bean里面有一个Date属性,表单传递过去的是字符串,出错,
- JSP连接数据库,TOMCAT为什么会死掉?
- 关于在jsp中包含文件的问题
- 请问怎么样在JBULIDER7下配置服务器!
- 怎样才能去掉端口号!
- 如何查询出订单的信息和订单中包含的商品详细信息(一个订单有多个商品)该怎么写SQL语句
- 关于shirorememberMe登录 session失效的问题
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;
}