0

我有以下代码,我正在尝试将数据发布到网络服务。但我得到的响应是 {"Message":"An Error Has Occured"}。我不知道我错在哪里。请仔细阅读代码并告诉我。

  @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"
             }
             ]
         };

现在我想把我的价值观放在“答案”键中。但我得到的响应是 {"Message":"An Error Has Occured"}

等待适当的解决方案。谢谢你。

4

2 回答 2

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();
     }

希望这对您有所帮助。

于 2013-11-06T06:21:38.137 回答
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
}

这对我有用。

于 2013-11-06T06:29:05.423 回答