HttpResponse的getEntity()。getContent()方法只有

时间:2012-10-13 11:34:25

标签: android apache-httpclient-4.x

我试图通过使用默认httpClient通过Web服务调用获取xml响应,但是无法执行此操作                 作为错误消息来自response.getEntity()。getContent()。                 line String veraible总是通过我的响应代码200来了。         DefaultHttp客户端对象是从另一种方法创建的         这是我的代码 -

            try {
            HttpPost post = new HttpPost(tmsURL);
            post.setHeader("Content-Type", "text/xml;charset=utf-8");
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
            nameValuePairs.add(new BasicNameValuePair(METHOD_PARAM, method));
            nameValuePairs.add(new BasicNameValuePair(XML_PARAM, xml));
            UrlEncodedFormEntity entity_st=new UrlEncodedFormEntity(nameValuePairs,"UTF-8");
            post.setEntity(entity_st);


                            HttpResponse response = client.execute(post,localcontext);


                            responseCode = response.getStatusLine().getStatusCode();

                            if (responseCode == HttpStatus.SC_OK) {
                                 BufferedReader rd = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
                                  String line = "";
                                  while ((line = rd.readLine()) != null) {
                                    System.out.println(line);
                                  }
            }
} catch (Exception e) {
                e.printStackTrace();
                logger.info("Exception occured in httpClientSendRequest() : "
                        + printStackTrace(e));
                result = buildXmlErrorMessage("", e.getMessage(), "");
            } finally {
                // post.releaseConnection();
            }

    please help me if anybody have any solution to solve the issue

2 个答案:

答案 0 :(得分:0)

try 
 {  

                         HttpClient httpclient = new DefaultHttpClient();
                         HttpPost httppost = new HttpPost("url");

                         static_class.str_IpAddress=getLocalIpAddress();

                        // Add your data
                        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(3);
                        nameValuePairs.add(new BasicNameValuePair("UserName", "str_user_name"));
                        nameValuePairs.add(new BasicNameValuePair("Password", "str_password"));
                        nameValuePairs.add(new BasicNameValuePair("IpAddress", "str_IpAddress"));
                        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

                        // Execute HTTP Post Request
                        HttpResponse response = httpclient.execute(httppost);
                        HttpEntity entity = response.getEntity();
                        String responseText = EntityUtils.toString(entity);     
                      //  Log.w("response message....",""+responseText);
                        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                        DocumentBuilder db = dbf.newDocumentBuilder();
                        InputStream is = new ByteArrayInputStream(responseText.getBytes());
                        doc = db.parse(is);
                        doc.getDocumentElement().normalize();

                        NodeList nodeList = doc.getElementsByTagName("Response");
                        Node node = nodeList.item(0);                           
                        Element fstElmnt = (Element) node;


                        NodeList toList = fstElmnt.getElementsByTagName("ResponseMessage");
                        Element nameElement = (Element) toList.item(0);
                        toList = nameElement.getChildNodes();
                        String str_welcome_message=""+((Node) toList.item(0)).getNodeValue();



                     catch(Exception e)
                     {

                         Log.w("DocumentBuilderFactory",""+e);
                     }

答案 1 :(得分:0)

您的代码似乎运行正常。问题在于您的POST请求,您可能需要检查参数。