0

My android application sends a web service request and gets the response from web service in xml, with the embedded data in JSON. I am saving this in a string. Now I do not know how to get the JSON data from this string.

    System.setProperty("http.keepAlive", "false");
            // request parameters
            HttpParams params = httpClient.getParams();
            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 15000);
            // set parameter
            HttpProtocolParams.setUseExpectContinue(httpClient.getParams(), true);

            // POST the envelope
            HttpPost httppost = new HttpPost(url);
            // add headers
            httppost.setHeader("SOAPAction", soapAction);
            httppost.setHeader("Content-Type", "text/xml; charset=utf-8");
    //      httppost.setHeader("Content-Length",
    //              String.valueOf(requestEnvelope.length()));
            httppost.setHeader("SOAPAction", "http://tempuri.org/"
                    + methodName);



    //      String responseString = "";
            try {

                // the entity holds the request
                HttpEntity entity = new StringEntity(requestEnvelope);
                httppost.setEntity(entity);
HttpResponse response = httpClient.execute(httppost);
            String result = EntityUtils.toString(response.getEntity());

This is the response from the server, which I get in the result string.

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ValidatePassCodeResponse xmlns="http://tempuri.org/">
<ValidatePassCodeResult>
[{"ID":1929,"Headline":"Test News","Detail":"","SubmitDate":"1/17/2012 12:08:04 PM"}]
</ValidatePassCodeResult>
</ValidatePassCodeResponse>
</soap:Body>
</soap:Envelope>

Thanks in advance.

aiRbornE
  • 139
  • 1
  • 11

4 Answers4

2

This is how I removed the tags of xml string and obtained the json string from that, then I parsed the string to json array and obtained the result.

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
            factory.setNamespaceAware(true);
            XmlPullParser xpp = factory.newPullParser();

            xpp.setInput(new StringReader(result));
            int eventType = xpp.getEventType();
            while (eventType != XmlPullParser.END_DOCUMENT) {
                if (eventType == XmlPullParser.START_DOCUMENT) {
                    System.out.println("Start document");
                } else if (eventType == XmlPullParser.START_TAG) {
                    System.out.println("Start tag " + xpp.getName());
                } else if (eventType == XmlPullParser.END_TAG) {
                    System.out.println("End tag " + xpp.getName());
                } else if (eventType == XmlPullParser.TEXT) {
                    responseString = xpp.getText();
                    System.out.println("Text " + xpp.getText());
                }
                eventType = xpp.next();
            }
            System.out.println("End document");
aiRbornE
  • 139
  • 1
  • 11
0

Please see my question and put your response string in json viewer.

How to use Json Parsing?

Community
  • 1
  • 1
Dipak Keshariya
  • 22,193
  • 18
  • 76
  • 128
0

First retrieve JSON using SAXParser.
See this sample.
Then you parse the JSON. See this question.

Community
  • 1
  • 1
Shadow
  • 6,161
  • 3
  • 20
  • 14
0

I got the solution using XMLPullParser. Thanks for the help though.

DaveShaw
  • 52,123
  • 16
  • 112
  • 141
aiRbornE
  • 139
  • 1
  • 11
  • 5
    ... and what was the answer? The next reader of this question could benefit if you write down what you found out. – Jim DeLaHunt Jan 19 '12 at 13:18