HttpClient向服务器接口 传递 json参数,参数要做特殊的处理,否则报错。
错误原因是:HttpGet或 HttpPost都不能传包含 " 、“{"、"}"这样的参数,需要对特殊字符进行转义,把 " 转成%22,把 { 转成%7b,把 } 转成%7d
干脆直接自己 replaceAll 算了,两次:
“{” => %7B
“}” => %7D
对了,URLEncode从?之后的参数开始,不要把域名和路径也放进去了。
String x = IOUtility.read2String("d:/pushlog.txt");
x = URLEncoder.encode(x,"UTF-8");
x = x.replace(" ", "").replace("\"", "%22");
x = x.replace("{", "%B").replace("}", "%7D");
String uri = "http://10.131.80.148:8080/receive/log?data=" + x;
HttpGet get = new HttpGet(uri);
System.out.println(get);
HttpResponse res = client.execute(get);;
[java] view plain copy
- RequestVo reqVo = new RequestVo();
- reqVo.context = getApplicationContext();
- StringBuffer sb = new StringBuffer();
- sb.append("?info={\"type\":\"1\",");
- sb.append("\"shopid\":\"" + shopId + "\",");
- sb.append("\"fields\":{\"name\":true,");
- sb.append("\"address\":true,");
- sb.append("\"tel\":true,");
- sb.append("\"scores\":true,");
- sb.append("\"intro\":true}}");
- String param = sb.toString().replace("\"", "%22")
- .replace("{", "%7b").replace("}", "%7d"); //特殊字符进行转义
- reqVo.requestUrl = getString(R.string.detail_api).concat(param);
- reqVo.jsonParser = new ShopParser();
- BaseTask task = new BaseTask(getApplicationContext(), reqVo,
- handler);
- ThreadPoolManager.getInstance().addTask(task);
注意:参数里面如果有 空格的话,也需要转义,否则会有问题。