0

i have made an activity in android,in that i have made a multipart entity request using HttpPost,Now i am getting successfull respose also.but thing is i dont know how to get those data from response.i have tried number of links for parsing xml but with no luck.Please help me for this.how to get data from my xml respose.My code is as below:

login

    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......

Response

 <?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>
jigar
  • 11
  • 5
  • Please change the subject of your post to `how to parse xml data from xml response`. You can remove all your code. Instead post the xml response. – greenapps Nov 21 '14 at 11:04
  • @greenapps-you should see the code..and obvious i got a number of links of parsing...but reason of posting is my question is quite different..please go through code.its multipart request..not like..xmlparse.execute(url)..!!! – jigar Nov 21 '14 at 11:14
  • The way you do the request is irrelevant. You have a response. You want to parse the response. So show the response. – greenapps Nov 21 '14 at 11:17
  • @greenapps-please see my edit..i have posted my response..but i have just prinoted it as a string using system.out.println as you see in code..i dont know how to get data from it – jigar Nov 21 '14 at 11:20
  • 1
    You can use JSOUP for that. Or another xml parser. And because the document is build up very simple you can write your own xml parser in ten lines of code. – greenapps Nov 21 '14 at 11:22
  • @greenapps-can you send me any links of that please? – jigar Nov 21 '14 at 11:25
  • Please google for JSOUP. You also need to download a jar file and put it in the libs directory. – greenapps Nov 21 '14 at 11:28
  • @greenapps-if i am going to use JSOUP..still have same question..at a line 'Document document = Jsoup.connect(url).get();' as i am making multipart request so no full url i am having ,you got my question? – jigar Nov 21 '14 at 11:28
  • That is the wrong way as you tried to let jsoup retrieve the response. But you have already the response in `String responseBody`. So take a different jsoup function to parse responseBody. – greenapps Nov 21 '14 at 11:35
  • @greenapps-ok..i am finding if any..but can you tell me how to parse data from document..as i have converted my response string to[ document.. – jigar Nov 21 '14 at 11:38
  • What do you mean by that? Please show the code you tried (in a new code block). – greenapps Nov 21 '14 at 11:41
  • please wait i am posting my code. – jigar Nov 21 '14 at 11:57
  • @greenapps-i have posted my code.i have tried..please. – jigar Nov 21 '14 at 12:01
  • And.. what is the problem? Does it compile? Any errors? I think you do not need a document builder or factory as you have already a document. What you want to parse is the basic of basic of xml parsing. I suggest you press the `xml-parsing` button on this page and read a lot of threads about xml parsing. I bet within ten minutes you find what you need. – greenapps Nov 21 '14 at 12:55
  • @greenapps-my friend even i got so many links for parsing,but i dontk know how to parse the xml in myl case.If you have any links as like me,Please refer me,thanks – jigar Nov 24 '14 at 03:59
  • You are not answering my questions. Why? `Document doc = Jsoup.parse(responseBody);`. I asked you to read posts under `xml-parsing` tag. Sorry. Better try the `jsoup` tag. – greenapps Nov 24 '14 at 09:04

2 Answers2

0

I got my answer by my way.Hello all for your responses but i got myy question solved by my way ,Actually all are telling me about different parsing but i want to know about the parsing from HTTP RESPONSE,So i have got my solution,here it is: http://www.java2s.com/Code/Android/Development/getCharacterDataFromElement.htm

Thanks for this useful post.

jigar
  • 11
  • 5
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);
Shaktisinh Jadeja
  • 1,427
  • 1
  • 17
  • 22