HttpClient get 方法传递json参数的问题

ruanjian123456123 2016-10-12 01:41:02
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参数,求大神指点
...全文
2113 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
lemerence 2019-07-07
  • 打赏
  • 举报
回复
哥们你把NetTool类也贴出来啊
小灯光环 2016-10-12
  • 打赏
  • 举报
回复
把json字符串封装到List<NameValuePair>传给服务端就可以了,给你看下我以前写的:
	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;
	}

81,092

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧