0

我试图通过使用默认 httpClient 的 Web 服务调用获取 xml 响应,但由于 response.getEntity().getContent() 中出现错误消息而无法这样做。通过我的响应代码 200 comming,line String veraible 总是会出现。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
4

2 回答 2

0

Your code seems to be working fine. The problem lies with your POST request, you may want to check the parameters.

于 2012-10-13T11:54:45.447 回答
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);
                     }
于 2012-10-13T11:45:03.490 回答