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

问题描述 投票:0回答:2

我试图通过使用默认的httpClient来通过Web服务调用获取xml响应,但是不能将其作为响应.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
android apache-httpclient-4.x
2个回答
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);
                     }

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.