Android post请求问题

小0 发布于 2011/08/24 11:26
阅读 3K+
收藏 0
    String url = "http://172.16.108.8:8080/SunShine/PinglunInfo.action?name=济南市第五人民医院& amp;address=山东省济南市槐荫区经十路447号, China&page=0"
    HttpClient client = new DefaultHttpClient();
        HttpPost request = new HttpPost(url);
        System.out.println(request.toString() + "request.tostring");
        HttpResponse response = client.execute(request);
        HttpEntity entity = response.getEntity();
        String out = EntityUtils.toString(entity);

经过我测试,我的这个url有问题,url包含逗号,导致发送不到服务端,我换了一个没有逗号的,就可以发送过去了,,纠结。。这个问题该怎么解决。。。。。求解决,,,,

加载中
0
红薯
红薯

将中文参数直接放在URL中,需要做 URLEncode 处理的:URLEncoder.encode(...),可以使用 UrlEncodedFormEntity来添加参数,使用方法请看:

http://www.oschina.net/code/snippet_12_5909

小0
小0
O(∩_∩)O哈哈~,谢谢站长、、
0
小0
小0

引用来自“红薯”的答案

将中文参数直接放在URL中,需要做 URLEncode 处理的:URLEncoder.encode(...),可以使用 UrlEncodedFormEntity来添加参数,使用方法请看:

http://www.oschina.net/code/snippet_12_5909

我的服务端代码

		try {
			String storeName = URLDecoder.decode(request.getParameter("name"), "utf-8");
			String storeAddress =  URLDecoder.decode(request.getParameter("address"),"utf-8");
			String currentPage = request.getParameter("page");
			String sql =.......;
			CoordService coordService = new CoordService();
			try {
				String xmlAddress = coordService.getAll(sql);
				System.out.println(xmlAddress
						+ "------------------------------------------------");
				this.response.getWriter().write(xmlAddress);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
我的客户端代码
  public static String getCritisimUrl(String url,
			List<BasicNameValuePair> params) throws Exception {
		DefaultHttpClient mHttpClient = new DefaultHttpClient();
		System.out.println(url + "url------------");
		HttpPost httpRequest = new HttpPost(new URI(url));
		httpRequest.setEntity(new UrlEncodedFormEntity(params,"utf-8"));
		HttpResponse response = mHttpClient.execute(httpRequest);
		HttpEntity entity = response.getEntity();
		String out = EntityUtils.toString(entity);
		return out;
	}
  String url = "http://172.16.108.8:8080/SunShine/PinglunInfo.action";
 List<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>();
		params.add(new BasicNameValuePair("name",URLEncoder.encode(title.getText().toString())));
		params.add(new BasicNameValuePair("address", URLEncoder.encode(address.getText().toString().substring(4))));
		params.add(new BasicNameValuePair("page",URLEncoder.encode(String.valueOf(currentPage))));
		String out = getCritisimUrl(url, params);
返回的out是?? ,(乱码)。。。但是服务端我打印的那个xmlAddress 是中文。。怎么到了手机上就是乱码了呢
我用new String(result.getBytes("utf-8"), "gbk");也不行。几乎常见的编码都试了。。都试了。还是不行。
求解决、、、、、、!!!
返回顶部
顶部