获取org.apache.http.client.HttpResponseException:在android

时间:2015-09-02 05:49:10

标签: android xml wcf rest http-post

我正在尝试向Android发出 httpsost 请求 WCF Restful service 。我在android中生成 XML 并通过httppost将其传递给服务。但是我一直得到上​​面指定的例外。

我使用 POST 方法创建了一个WCF Rest服务,该方法包含一个读取收到的xml内容的函数。

此外,我已在防火墙下的WWW中启用域。如果使用网址 http://192.168.0.12/RestFul/service1.svc ,我可以浏览该服务,但是当我尝试使用来自Android的服务(例如http://192.168.0.12/RestFul/service1.svc/Push)调用网络方法时,我会收到此异常

这是我的代码fofr makinh Httppost请求:

DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
                 dbfac.setNamespaceAware(true);
                 DocumentBuilder docBuilder = null;
                 try {
                    docBuilder = dbfac.newDocumentBuilder();
                } catch (ParserConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 DOMImplementation domImpl = docBuilder.getDOMImplementation();
                  doc = domImpl.createDocument(null,"SensorData", null);
                 Log.v("MA", "File created:"+doc);
                 doc.setXmlVersion("1.0");
                 doc.setXmlStandalone(true);

                 Element trackElement = doc.getDocumentElement();

                 Element CompanyId = doc.createElement("CompanyId");
                 CompanyId.appendChild(doc.createTextNode("1"));
                 trackElement.appendChild(CompanyId);
                 Log.v("MA", "Element  added"+CompanyId);
                 Element CreatedBy = doc.createElement("CreatedBy");
                 CreatedBy.appendChild(doc.createTextNode("6"));
                 trackElement.appendChild(CreatedBy);
                 Log.v("MA", "Element  added"+CreatedBy);
                 Element DepartmentId = doc.createElement("DepartmentId");
                 DepartmentId.appendChild(doc.createTextNode("4"));
                 trackElement.appendChild(DepartmentId);
                 Log.v("MA", "Element  added"+DepartmentId);
                 Element IsBillable = doc.createElement("IsBillable");
                 IsBillable.appendChild(doc.createTextNode("1"));
                 trackElement.appendChild(IsBillable);
                 Log.v("MA", "Element  added"+IsBillable);
                 Element ProjectId = doc.createElement("ProjectId");
                 ProjectId.appendChild(doc.createTextNode("1"));
                 trackElement.appendChild(ProjectId);
                 Log.v("MA", "Element  added"+ProjectId);
                 Element StartTime = doc.createElement("StartTime");
                 StartTime.appendChild(doc.createTextNode("2015-09-27 10:44:45"));
                 trackElement.appendChild(StartTime);
                 Log.v("MA", "Element  added"+StartTime);
                 Element StopTime = doc.createElement("StopTime");
                 StopTime.appendChild(doc.createTextNode("2015-09-27 11:44:45"));
                 trackElement.appendChild(StopTime);
                 Log.v("MA", "Element  added"+StopTime);
                 Element TaskId = doc.createElement("TaskId");
                 TaskId.appendChild(doc.createTextNode("3"));
                 trackElement.appendChild(TaskId);
                 Log.v("MA", "Element  added"+TaskId);
                 Element TotalTime = doc.createElement("TotalTime");
                 TotalTime.appendChild(doc.createTextNode("1"));
                 trackElement.appendChild(TotalTime);
                 Log.v("MA", "Element  added"+TotalTime);
                 Element TrackDesc = doc.createElement("TrackDesc");
                 TrackDesc.appendChild(doc.createTextNode("dello testing"));
                 trackElement.appendChild(TrackDesc);
                 Log.v("MA", "Element  added"+TrackDesc);
                 Element TrackId = doc.createElement("TrackId");
                 TrackId.appendChild(doc.createTextNode("0"));
                 trackElement.appendChild(TrackId);
                 Log.v("MA", "Element  added"+TrackId);
                 TransformerFactory transfac = TransformerFactory.newInstance();
                 Transformer trans = null;
                try {
                    trans = transfac.newTransformer();
                } catch (TransformerConfigurationException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                 trans.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
                 trans.setOutputProperty(OutputKeys.INDENT, "yes");

                 //create string from xml tree
                 StringWriter sw = new StringWriter();
                 StreamResult result = new StreamResult(sw);
                 DOMSource source = new DOMSource(doc);
                 try {
                    trans.transform(source, result);
                } catch (TransformerException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                  xmlString = sw.toString();
            // Create a new HttpClient and Post Header

            DefaultHttpClient httpClient = new DefaultHttpClient();


              httppost = new HttpPost("http://192.168.0.12/RestFul/service1.svc/Push/"); 
              Log.v("MA", "xmlString is:"+xmlString);
             // Make sure the server knows what kind of a response we will accept
             httppost.addHeader("Accept", "text/xml");
             // Also be sure to tell the server what kind of content we are sending
             httppost.addHeader("Content-Type", "application/xml");

             try
             {
             StringEntity entity = new StringEntity(xmlString, "UTF-8");
             entity.setContentType("text/xml");
             httppost.setEntity(entity);
             Log.v("MA", "Post executed");
             // execute is a blocking call, it's best to call this code in a thread separate from the ui's
             HttpResponse response = httpClient.execute(httppost);
             responseCode = response.getStatusLine().getStatusCode();
              responseContent = EntityUtils.toString(response.getEntity());
             Log.v("MA", "HttpResponse:"+response);
            //Toast.makeText(getApplicationContext(),"Response:"+response, Toast.LENGTH_LONG);
             BasicResponseHandler responseHandler = new BasicResponseHandler();
                String strResponse = null;
                if (response != null) {
                    try {
                        Log.v("MA", "Response is not null");
                        strResponse = responseHandler.handleResponse(response);
                      //  Toast.makeText(getApplicationContext(), "Response:",Toast.LENGTH_SHORT).show();
                    } catch (HttpResponseException e) {
                        e.printStackTrace();  
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                Log.e("WCFTEST", "WCFTEST ********** Response" + strResponse);    
[![enter image description here][1]][1]

             }
             catch (Exception ex)
             {
             ex.printStackTrace();
             }

这是我的网络服务方法:

[OperationContract]
        [WebInvoke(Method = "POST", UriTemplate = "Push",
               RequestFormat = WebMessageFormat.Xml,
               ResponseFormat = WebMessageFormat.Xml,
               BodyStyle = WebMessageBodyStyle.Bare)]
        XElement DoWork(Stream xml);

public XElement DoWork(Stream xml)
        {
            StreamReader reader = null;
            XDocument xDocRequest = null;
            string strXmlRequest = string.Empty;
            reader = new StreamReader(xml);
            strXmlRequest = reader.ReadToEnd();
            xDocRequest = XDocument.Parse(strXmlRequest);
            string response = "<Result>OK</Result>";
            return XElement.Parse(response);
        }

这是我的logcat:

Here is the snapshot

任何有关此工作的建议都会非常感激。

1 个答案:

答案 0 :(得分:0)

您是否在清单中提供了互联网权限?