Xml data is converted to json data in android

I need to parse the XML data from a web service. But, when I get data from a web service, the data comes in JSON format (in the browser I see the data as XML). So, please tell me how to get the XML data in my application.

I am using the following.

In my main activity:

static final String URL = "http://nclex.testshell.net/api/resources"; XMLParser parser = new XMLParser(); String xml = parser.getXmlFromUrl(URL); // getting XML Log.e("Response is...",xml); 

My xmlParser class

 public class XMLParser { // constructor public XMLParser() { } /** * Getting XML from URL making HTTP request * @param url string * */ public String getXmlFromUrl(String url) { String xml = null; try { // defaultHttpClient DefaultHttpClient httpClient = new DefaultHttpClient(); HttpGet httpGet = new HttpGet(url); HttpResponse httpResponse = httpClient.execute(httpGet); HttpEntity httpEntity = httpResponse.getEntity(); xml = EntityUtils.toString(httpEntity); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (ClientProtocolException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // return XML return xml; } } 

exit response

[{"ResourceId": 1, "Title": "GRE revised general test", "Description": "Verbal reasoning section edited by GRE Test", "Link": " http://www.ets.org/gre/ revised_general / about / content / verbal_reasoning "}, {" ResourceId ": 2," Title ":" GRE Power Pre "," Description ":" GRE Power Pre "," Link ":" http: //www.number2. com / exams / gre / companion / index.cfm? s = 0 "}, {" ResourceId ": 3," Title ":" GRE Analytical Writing "," Description ":" GRE Analytical "Link": " http: / /www.mygretutor.com/tests.aspx "}, {" ResourceId ": 4," Title ":" GATE "Architecture and Planning", "Description": "GATE Architecture & Planning", "Link": " http: //www.onestopgate.com/gate-sample-papers/architecture-planning/ "}, {" ResourceId ": 5," Title ":" TarGATE "," Description ":" GATE to achieve target "," Link ": " http://gateforum.com/Testseries-Venue .php "}, {" ResourceId ": 6," Title ":" TOEFL iBT "," Description ":" Test sample TOEFL iBT Questions "," Link ":" https://www.ets.org/toefl/ ibt / prepare / sample_questions "}]

+4
source share
1 answer

The type or format of data received from the server using the web service depends entirely on the language in which the web service is running and the response code that the web service selects to format the data. .

Older web services or platforms mostly support the XML format. And new web services use the Json format for its light weight.

In your case, your web service supports both types of formats (XML, Json), and it selects the format according to the platform with which it receives the request (Mobile, Desktop). (Maybe it acts like a general web service).

One way to test the webservice response is to request it from the platform browser. Use your platform’s browser (desktop or mobile) to see the answer for your platform.

Finally, answer your question, Change the code on the server side and make sure that it returns the XML data as an answer for the platform for mobile devices (you do not need to worry about the code from the side of your application, all the changes that need to be made to the server).

However, you can also convert Json to xml as,

 JSONObject Jobj = new JSONObject(jsonString); 

Then you can get it in XML format using an XML class, for example:

 String xml = org.json.XML.toString(Jobj); 

but it is not recommended, because it takes extra processor time to convert Json to xml, and this can be a problem if your response data is huge.

+2
source

Source: https://habr.com/ru/post/1487814/


All Articles