本文共 768 字,大约阅读时间需要 2 分钟。
今天做项目的时候,需要向服务器接口 传递 json参数,使用 apache 的 httpclient,当时没有对参数做特殊处理直接提交了,结果运行时报错了,如下:
HTTP 400 Bad Request
后来在网上查资料找到原因了:HttpGet或 HttpPost都不能传包含 ” 、“{“、”}”这样的参数,需要对特殊字符进行转义,把 ” 转成%22,把 { 转成%7b,把 } 转成%7d
MsgResult msgResult = new MsgResult(1,"成功","我是正文内容"); Gson gson = new Gson(); String gsonStr = gson.toJson(msgResult); //特殊字符进行转义 gsonStr = gsonStr.replace("\"", "%22") .replace("{", "%7b").replace("}", "%7d"); Observable observable = webService_tomcat.insertJsonString(MethodName_MainHome,gsonStr); observable.subscribeOn(Schedulers.io()) .unsubscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) .subscribe(observer);
注意:参数里面如果有 空格的话,也需要转义,否则会有问题。
转载地址:http://rcxfb.baihongyu.com/