开发者社区> 问答> 正文

JSON数据请求乱码问题? 400 报错

JSON数据请求乱码问题? 400 报错

try {
StringBuffer buffer = new StringBuffer();
HttpPost   httpRequest=new HttpPost("http://192.168.1.100:8080/users/userinfos.html");
/*httpRequest.setHeader("Content-Type", "application/x-www-form-urlencoded; charset=utf-8");*/
/*httpRequest.addHeader("charset", HTTP.UTF_8); */
HttpClient httpClient = new DefaultHttpClient();
HttpParams params1 = httpClient.getParams();
HttpConnectionParams.setConnectionTimeout(params1, 1000);
HttpConnectionParams.setSoTimeout(params1, 5000);
Log.i("infor", "timeout");
HttpResponse response = httpClient.execute(httpRequest);
HttpEntity entity = response.getEntity();
if(response.getStatusLine().getStatusCode()==200){
if (entity != null) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
entity.getContent()));
String line = null;
while ((line = reader.readLine()) != null) {
buffer.append(line);
}
}
}
//Log.e("shuchu",buffer.toString());
       return buffer.toString();
} catch (Exception e) {
Log.e("msg", "网络连接失败");
return result;
}

获取数据乱码,求大神解答


展开
收起
爱吃鱼的程序员 2020-06-05 12:39:05 581 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    传回来的响应,进行utf-8转码一下######请求返回的内容编码 格式理论上是未知的,应该结合Content-Type响应头去处理返回数据。

    ######可是不会用######浏览器里返回正常不?首先确保不是服务器端的问题。其次参照浏览器里面的参数进行设定
    ######使用repsCharset = response.getCharacterEncoding()获得返回数值的编码格式,再Log.e("shuchu",new String(buffer.toString(), repsCharset))解码,试试看还乱码么;
    2020-06-05 12:39:23
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载