我必须从远程服务器获取一些JSON对象,为此我使用这个功能,这是工作伟大的,除了有时一些奇怪的数据被获取,我相信是因为它是使用ASCII字符集解码。 / p>
请在下面找到我使用
的方法 public HttpResponse call (String serviceURL,String serviceHost,String namespace,String methodName,String payloadKey,String payloadValue)throws ClientProtocolException,IOException,JSONException { HttpResponse response = null; HttpContext HTTP_CONTEXT = new BasicHttpContext(); HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT,Mozilla / 5.0); HttpPost httppost = new HttpPost(serviceURL); httppost.setHeader(User-Agent,Constants.USER_AGENT_BROWSER_FIREFOX); httppost.setHeader(Accept,application / json,text / javascript,* / *); httppost.setHeader(Accept-Language,en-US,en; q = 0.8); httppost.setHeader(Content-Encoding,foo-1.0); httppost.setHeader(Content-Type,application / json; charset = UTF-8); httppost.setHeader(X-Requested-With,XMLHttpRequest); httppost.setHeader(Host,serviceHost); httppost.setHeader(X-Foo-Target,String.format(%s。%s,namespace,methodName)); / * Making Payload * / JSONObject objectForPayload = new JSONObject(); objectForPayload.put(payloadKey,payloadValue); StringEntity stringentity = new StringEntity(objectForPayload.toString()); httppost.setEntity(stringentity); response = client.execute(httppost); return response; }我传递的所有标头正确,我已经验证了通过inspect元素在Google chrome或Firebug插件,如果你熟悉Mozilla的相同。
现在的问题是,大多数时间,我得到可读的数据,但有时我得到不可读的数据。
我使用eclipse调试,注意到wrappedEntity下的字符集显示为US-ASCII。我附加了一个jpg作为参考
有人可以告诉我怎么能改变字符集从ASCII到UTF-8的响应之前,我 response = client.execute(httppost); 。 PS:正如你已经注意到,我传递charset = utf-8在标题,我已经验证使用firebug和谷歌chrome,我传递确切的标头。
请放大以更清楚地查看图片
提前感谢
方案我能够解决这个问题,只是提到它可能面临类似问题的人。 在获得响应之后,首先通过使用 HttpEntity entity = response.getEntity(); 获取实体, json对象将实体转换为字符串,但使用UTF-8这样的东西 responseJsonObject = new JSONObject(EntityUtils.toString(entity,UTF-8));
以前我只是在做 responseJsonObject = new JSONObject(EntityUtils.toString(entity));
I have to fetch some JSON object from a remote server and for that i am using this function which is working great except that for sometime some weird data is getting fetched which i believe is because it is using ASCII charset to decode.
Please find below thw method that i am using
public HttpResponse call(String serviceURL,String serviceHost,String namespace,String methodName,String payloadKey, String payloadValue) throws ClientProtocolException,IOException,JSONException { HttpResponse response = null; HttpContext HTTP_CONTEXT = new BasicHttpContext(); HTTP_CONTEXT.setAttribute(CoreProtocolPNames.USER_AGENT, "Mozilla/5.0"); HttpPost httppost = new HttpPost(serviceURL); httppost.setHeader("User-Agent",Constants.USER_AGENT_BROWSER_FIREFOX); httppost.setHeader("Accept", "application/json, text/javascript, */*"); httppost.setHeader("Accept-Language","en-US,en;q=0.8"); httppost.setHeader("Content-Encoding", "foo-1.0"); httppost.setHeader("Content-Type", "application/json; charset=UTF-8"); httppost.setHeader("X-Requested-With","XMLHttpRequest"); httppost.setHeader("Host",serviceHost); httppost.setHeader("X-Foo-Target", String.format("%s.%s", namespace,methodName)); /*Making Payload*/ JSONObject objectForPayload = new JSONObject(); objectForPayload.put(payloadKey, payloadValue); StringEntity stringentity = new StringEntity(objectForPayload.toString()); httppost.setEntity(stringentity); response = client.execute(httppost); return response; }All these headers that i am passing are correct and i have verified the same via inspect element in Google chrome or Firebug plugin if you are familiar with Mozilla.
Now the problem is that most of the time i am getting the readable data but sometimes i do get unreadable data.
I debugged using eclipse and noticed that the charset under wrappedEntity is showing as "US-ASCII". I am attaching a jpg for reference
Can someone please tell me how can i change the charset from ASCII to UTF-8 of the response before i do response = client.execute(httppost); . PS:As you have noticed that i am passing charset=utf-8 in the header and that i have already verified using firebug and google chrome that i am passing the exact headers .
Please zoom in to see the image more clearly
Thanks in advance
解决方案i was able to resolve the issue just mentioning it for people that may face similar issue. after getting the response first get the entity by using HttpEntity entity = response.getEntity(); and since my response was a json object convert entity to string but using "UTF-8" something like this responseJsonObject = new JSONObject(EntityUtils.toString(entity,"UTF-8"));
previously i was just doing responseJsonObject = new JSONObject(EntityUtils.toString(entity));
更多推荐
如何在Java中的HTTP响应中更改字符集编码
发布评论