2016-01-21 128 views
1

我想发送一些数据到Web服务使用网络.svc网络服务。我使用HttpPost和BasicNameValuePair发送一些数据到Web服务,但我得到错误的响应字符串。谢谢!如何将请求数据发布到Android中的Web服务?

这里是日志信息

Editable Post﹕ = <?xml version="1.0" encoding="utf-8"?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Request Error</title> <style>BODY { color: #000000; background-color: white; font-family: Verdana; margin-left: 0px; margin-top: 0px; } #content { margin-left: 30px; font-size: .70em; padding-bottom: 2em; } A:link { color: #336699; font-weight: bold; text-decoration: underline; } A:visited { color: #6699cc; font-weight: bold; text-decoration: underline; } A:active { color: #336699; font-weight: bold; text-decoration: underline; } .heading1 { background-color: #003366; border-bottom: #336699 6px solid; color: #ffffff; font-family: Tahoma; font-size: 26px; font-weight: normal;margin: 0em 0em 10px -20px; padding-bottom: 8px; padding-left: 30px;padding-top: 16px;} pre { font-size:small; background-color: #e5e5cc; padding: 5px; font-family: Courier New; margin-top: 0px; border: 1px #f0f0e0 solid; white-space: pre-wrap; white-space: -pre-wrap; word-wrap: break-word; } table { border-collapse: collapse; border-spacing: 0px; font-family: Verdana;} table th { border-right: 2px white solid; border-bottom: 2px white solid; font-weight: bold; background-color: #cecf9c;} table td { border-right: 2px white solid; border-bottom: 2px white solid; background-color: #e5e5cc;}</style> </head> <body> <div id="content">  <p class="heading1">Request Error</p>  <p xmlns="">The server encountered an error processing the request. Please see the <a rel="help-page" href="http://103.24.4.60/classnk1/MobileService.svc/help">service help page</a> for constructing valid requests to the service.</p> </div> </body></html> 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk D/@@@@ Http Post :﹕ Response [email protected] 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ org.json.JSONException: Value <?xml of type java.lang.String cannot be converted to JSONObject 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at org.json.JSON.typeMismatch(JSON.java:111) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:160) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at org.json.JSONObject.<init>(JSONObject.java:173) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at com.example.tazeen.classnkk.Edit_Post$9.doInBackground(Edit_Post.java:972) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at com.example.tazeen.classnkk.Edit_Post$9.doInBackground(Edit_Post.java:911) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at android.os.AsyncTask$2.call(AsyncTask.java:292) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at java.util.concurrent.FutureTask.run(FutureTask.java:237) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 
01-21 16:18:49.132 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) 
01-21 16:18:49.133 16744-17063/com.example.tazeen.classnkk W/System.err﹕ at java.lang.Thread.run(Thread.java:818) 

下面是编写的Web服务代码。

public void ActivityUpload(final String strCurrentDateTime, final String strTitle, final String replaceDescChar, final String editedHashTag) 
    { 

     new AsyncTask<String, Void, String>() 
     { 
      @Override 
      protected String doInBackground(String... arg0) 
      { 
       HttpClient httpClient1 = new DefaultHttpClient(); 
       String strupdate_Upload = namespace + "/Upd_Post_Activity"; 
       HttpPost httpPost = new HttpPost(strupdate_Upload); 


       List<NameValuePair> nameValuePair = new ArrayList<NameValuePair>(11); 
       nameValuePair.add(new BasicNameValuePair("ActionDate", strCurrentDateTime)); 
       nameValuePair.add(new BasicNameValuePair("ActivityId", strActivityId)); 
       nameValuePair.add(new BasicNameValuePair("UserId", str_UserId)); 
       nameValuePair.add(new BasicNameValuePair("ObjectId", strVessId)); 
       nameValuePair.add(new BasicNameValuePair("Name", strTitle)); 
       nameValuePair.add(new BasicNameValuePair("Remark", replaceDescChar)); 
       nameValuePair.add(new BasicNameValuePair("Status", "PENDING")); 
       nameValuePair.add(new BasicNameValuePair("Type", strType)); 
       nameValuePair.add(new BasicNameValuePair("starflag", "0")); 
       nameValuePair.add(new BasicNameValuePair("HashTag", editedHashTag)); 
       nameValuePair.add(new BasicNameValuePair("Authentication_Token", str_Authentication_Token)); 


       //Encoding POST data 
       try 
       { 

        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePair)); 
       } catch (UnsupportedEncodingException e) 
       { 
        // log exception 
        e.printStackTrace(); 
       } 

       //making POST request. 
       try 
       { 

        HttpResponse response = httpClient1.execute(httpPost); 
        InputStream inputStream = response .getEntity().getContent(); 
        if (inputStream != null) 
         strEditableResult = Constant.convertInputStreamToString(inputStream); 
        else 
         strEditableResult = "Did not work!"; 
        Log.e("Editable Post", " = " + strEditableResult); 
        // write response to log 
        Log.d(" @@@@ Http Post :", " Response " + response.toString()); 
       } catch (ClientProtocolException e) 
       { 
        // Log exception 
        e.printStackTrace(); 
       } catch (IOException e) { 
        // Log exception 
        e.printStackTrace(); 
       } 


       if (strEditableResult != null) 
       { 
        try 
        { 
         JSONObject jsonObj = new JSONObject(strEditableResult); 
         String jsonResult = jsonObj.toString().trim(); 
         Log.e("jsonResult ", " = " + jsonResult); 
         catch (JSONException je) 
         { 
          je.printStackTrace(); 
         } 

         } 
       return result; 

      } 
     }.execute(); 


     } 

回答

0

问题出现在您的解析响应JSON中,因为它是用XML标记覆盖的。 请参考this的答案。

相关问题