Java的HttpClient的改变内容类型? [英] Java HttpClient changing content-type?
问题描述
我建立一个小的应用程序为我的Android手机短信转发到使用一个很基本的REST接口的网络服务器。
I am building a small app for my Android phone to forward text messages to a webserver using a very basic REST interface.
我使用的是Android 4.0.3 SDK。我公司开发使用Django和Django的restframework包中的Web服务的蟒蛇。该设置是完全开箱。这里基本上是一个端点接收一个JSON对象持有的邮件信息(发送者,身体,日期)的POST。我已经测试使用卷曲用下面的命令服务:
I am using the android 4.0.3 SDK. I developed the webservice in python using the Django and the Django restframework package. The setup is completely out of the box. There is basically one endpoint that receives a POST of a JSON object holding the message info (sender, body, date). I have tested the service using cURL with the following command:
卷曲-X POST -H内容类型:应用程序/ JSON--data{发件人:+ XXXXXXXX,体:测试,send_date: 2011-03-20 16点32分02秒}'的http:// [...] /messages.json
这一切工作正常,我也得到了预期的响应:
This all works fine and I get the expected response:
{身:测试,send_date:2011-03-20T16:32:02,ID:25,发件人:+ XXXXXXXXXX}
现在我成立了Android应用程序。它是包括一个私人的AsyncTask类的简单BroadcastReceiver的子类:
Now I set up the android app. It is a simple BroadcastReceiver child class that includes a private AsyncTask class:
private class httpPost extends AsyncTask<String, Void, JSONObject> {
protected JSONObject doInBackground(String... args) {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpParams myParams = new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(myParams, 10000);
HttpConnectionParams.setSoTimeout(myParams, 10000);
String url = args[0];
String json=args[1];
JSONObject JSONResponse = null;
InputStream contentStream = null;
String resultString = "";
try {
HttpPost httppost = new HttpPost(url);
httppost.setHeader("Content-Type", "application/json");
httppost.setHeader("Accept", "application/json");
StringEntity se = new StringEntity(json);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
for (int i=0;i<httppost.getAllHeaders().length;i++) {
Log.v("set header", httppost.getAllHeaders()[i].getValue());
}
HttpResponse response = httpclient.execute(httppost);
// Do some checks to make sure that the request was processed properly
Header[] headers = response.getAllHeaders();
HttpEntity entity = response.getEntity();
contentStream = entity.getContent();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(contentStream,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
contentStream.close();
resultString = sb.toString();
}catch(Exception e){
e.printStackTrace();
}
Log.v("log", resultString);
return new JSONObject();
}
}
正如你所看到的,我刚开始熟悉Java和Android SDK的,所以请大家多多包涵。这样的设置实际上是在另一个应用程序,我建到JSON字符串发送到Web服务的Neo4j正常工作。
As you can see I am just starting to get familiar with Java and the android SDK so please bear with me. This setup actually worked fine in another app that I build to send JSON strings to a Neo4J webservice.
现在的问题是,当我通过张贴Android的消息给我的web服务,在某些时候的内容类型会从应用/ JSON的到应用/ JSON,应用/ JSON的改变。在头部循环的日志条目依然输出正确的价值观为每个标题,然而,Web服务将返回此错误消息:
The problem is that when I post the message via Android to my webservice, at some point the content-type gets changed from 'application/json' to 'application/json, application/json'. The log entry in the headers loop still outputs the correct values for each header, however, the webservice returns this error message:
{错误:不支持的媒体类型的请求应用/ JSON,应用/ JSON}
我百思不得其解,任何帮助是值得欢迎的。
I am puzzled, any help is welcome.
推荐答案
更改此:
StringEntity se = new StringEntity(json);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json"));
httppost.setEntity(se);
要这只是:
httppost.setEntity(new StringEntity(json.toString(), HTTP.UTF_8));
这篇关于Java的HttpClient的改变内容类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!