【问题标题】:how to parse xml data from HTTPResponse in android如何在android中解析来自HTTPResponse的xml数据
【发布时间】:2014-11-21 10:58:34
【问题描述】:

我在 android 中进行了一项活动,因为我使用 HttpPost 进行了多部分实体请求,现在我也成功地响应了。但问题是我不知道如何从响应中获取这些数据。我尝试了解析xml的链接,但没有运气。请帮我解决这个问题。如何从我的xml响应中获取数据。我的代码如下:

登录

    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>

【问题讨论】:

  • 请将您的帖子主题更改为how to parse xml data from xml response。您可以删除所有代码。而是发布 xml 响应。
  • @greenapps-你应该看到代码..很明显我有很多解析链接......但是发布的原因是我的问题完全不同..请通过代码。它的多部分请求..不喜欢..xmlparse.execute(url)..!!!
  • 您提出请求的方式无关紧要。你有回应。您想解析响应。所以显示响应。
  • @greenapps-请查看我的编辑..我已经发布了我的回复..但我刚刚使用 system.out.println 将其打印为字符串,正如您在代码中看到的那样..我不知道如何从中获取数据
  • 您可以使用 JSOUP。或者另一个 xml 解析器。而且由于文档的构建非常简单,您可以用十行代码编写自己的 xml 解析器。

标签: android xml xml-parsing multipartentity


【解决方案1】:

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

感谢这篇有用的帖子。

【讨论】:

  • 请说明您是如何使用它来提取所需数据的。
  • @greenapps-去链接..它展示了如何从字符串解析数据。
【解决方案2】:
            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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-06-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-08-21
    相关资源
    最近更新 更多