如何从android中的HTTPResponse解析xml数据

时间:2014-11-21 10:58:34

标签: android xml xml-parsing multipartentity

我已经在android中做了一个活动,因为我已经使用HttpPost做了一个多部分实体请求,现在我也成功地使用了。但事情是我不知道如何从response.i尝试获取这些数据用于解析xml但没有运气的链接。请帮我这个。从我的xml respose获取数据。我的代码如下:

登录

    try {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(Consts.API_HOST + "/login");

        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    2);
            nameValuePairs.add(new BasicNameValuePair("apiKey",
                    "JU7Jqt6X"));
            nameValuePairs.add(new BasicNameValuePair("type", "xml"));
            nameValuePairs.add(new BasicNameValuePair("email",
                    "yogesh@amarinfotech.com"));
            nameValuePairs.add(new BasicNameValuePair("pwd", "123"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            // xml response..!jigar...
            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            String responseBody = httpclient.execute(httppost,
                    responseHandler);


            // end of res jigar...
            System.out
                    .println("::::::::::::::::::::::::;;MY RESPONSE IN LOGIN ATIVITY::::::::::"
                            + responseBody);

            // making doc
            Document doc = null;
            DocumentBuilderFactory factory = DocumentBuilderFactory
                    .newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            StringReader sr = new StringReader(responseBody);
            InputSource is = new InputSource(sr);
            doc = builder.parse(is);



        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    } catch (Exception e) {

        System.out
                .println("::::::::::::::::::::::::::::MY exception in edit::::::::::::::::"
                        + e.getMessage());

        return null;
    }
    return null;

    // Parsing Procedure......

响应

 <?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<id>
8
</id>
<personal_title>
Mr.
</personal_title>
<first_name>
a
</first_name>
<middle_name>
b
</middle_name>
<last_name>
c
</last_name>
<email>
yogesh@amarinfotech.com
</email>
<password>
202cb962ac59075b964b07152d234b70
</password>
<mobile_number>
1234567890
</mobile_number>
<p_first_name>

</p_first_name>
<p_last_name>

</p_last_name>
<p_card_type>

</p_card_type>
<p_card_number>

</p_card_number>
<p_sec_code>

</p_sec_code>
<p_exp_month>

</p_exp_month>
<p_exp_year>

</p_exp_year>
<user_activation_key>
14164668001
</user_activation_key>
<varification>
1
</varification>
<send_mail>
0
</send_mail>
<status>
0
</status>
<register_date>
2014-11-20 23:04:14
</register_date>
<last_visit_date>
2014-11-20 23:04:14
</last_visit_date>
</root>

2 个答案:

答案 0 :(得分:0)

我按照自己的方式得到了答案。你好回答所有问题,但是我的问题解决了我的问题,实际上所有人都在告诉我不同​​的解析但是我想知道从HTTP RESPONSE解析,所以我有了我的解决方案,这里是: http://www.java2s.com/Code/Android/Development/getCharacterDataFromElement.htm

感谢这篇有用的帖子。

答案 1 :(得分:0)

            String xml="<yourxml></yourxml>"

            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(url);
            httpPost.setHeader("Content-Type", "application/xml");

            StringEntity entity = new StringEntity(xml);
            httpPost.setEntity(entity);

            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);