我有以下代码,我正在尝试将数据发布到websevice。但我得到的反应是{“消息”:“错误已经发生”}。我不知道我哪里错了。请仔细阅读代码并告诉我。
@Override
protected String doInBackground(String... urls) {
HttpClient httpClient= new DefaultHttpClient();
HttpContext context=new BasicHttpContext();
HttpPost httpPost=new HttpPost(url);
JSONObject json=new JSONObject();
httpPost.setHeader("Content-type",
"application/json; charset=UTF-8");
try {
json.put("EmailID","test@yahoo.com");
json.put("ProjID","78");
json.put("Uid","1");
StringEntity stringEntity = new StringEntity(json.toString());
InputStream stream= new ByteArrayInputStream(json.toString().getBytes("UTF-8"));
httpPost.setEntity(new StringEntity(json.toString()));
HttpResponse httpResponse= httpClient.execute(httpPost,context);
BufferedReader reader= new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));
res= reader.readLine();
resp=res.toString();
Log.e("RESPONSE OF WEBSER:", resp);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return resp;
这是jsonobject。
jsonObjects = {
"ProjID": "78",
"Uid": "1",
"EmailID": "test@yahoo.com",
"ProjectInviterFQAnswer": [{
"slno": "1",
"Answer": "a1",
"order": "1",
"flag": "F"
}, {
"slno": "2",
"Answer": "a1",
"order": "2",
"flag": "F"
}, {
"slno": "1",
"Answer": "a1",
"order": "2",
"flag": "Q"
}
]
};
现在我想将我的值放在“答案”键中。但我得到的反应是{“消息”:“错误已经发生”}
等待正确的解决方案。谢谢。
答案 0 :(得分:0)
试试这种方式
HttpClient httpClient= new DefaultHttpClient();
HttpContext context=new BasicHttpContext();
HttpPost httpPost=new HttpPost(url);
JSONObject json=new JSONObject();
try {
json.put("EmailID","test@yahoo.com");
json.put("ProjID","78");
json.put("Uid","1");
StringBody data = new StringBody(json.toString(),
Charset.forName(HTTP.UTF_8));
MultiPartEntity entity = new MultiPartEntity(
HttpMultipartMode.BROWSER_COMPATIBLE);
entity.addPart("Answer", data);
httpPost.setEntity(entity);
HttpResponse httpResponse= httpClient.execute(httpPost,context);
BufferedReader reader= new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(),"UTF-8"));
res= reader.readLine();
resp=res.toString();
Log.e("RESPONSE OF WEBSER:", resp);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
希望这会对你有所帮助。
答案 1 :(得分:0)
试试这个:
DefaultHttpClient httpclient = new DefaultHttpClient();
String statusResponse;
HttpPost httpPost = new HttpPost(url);
StringEntity entity;
JSONObject jsonLocation = new JSONObject();
try {
jsonLocation.put("param0", params[0]);
jsonLocation.put("param1", params[1]);
httpPost.setHeader("Content-Type", "application/json");
entity = new StringEntity(jsonLocation.toString());
httpPost.setEntity(entity);
HttpResponse httpResponse = httpclient.execute(httpPost);
InputStream is = httpResponse.getEntity().getContent();
statusResponse = convertStreamToString(is);
if (httpResponse.getStatusLine().getStatusCode() == 200
&& statusResponse.length() > 0) {
// Do your code here
}
else
{
// Error Code
}
这适合我。