我需要解析來自Web服務的XML數據。但是,當我從Web服務獲取數據時,數據採用JSON格式(在瀏覽器中,我將數據視爲XML)。所以,請指導我如何將xml數據導入到我的應用程序中。Xml數據被轉換成Json數據android
我正在使用以下內容。
以我主要活動:
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);
我XMLPARSER類
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;
}
}
輸出響應是
[{ 「RESOURCEID」:1,」標題「:」GRE revi sed通用測試「,」說明「:」 GRE修訂後的通用 測試「,」鏈接「:」http://www.ets.org/gre/revised_general/about/content/verbal_reasoning「},{」ResourceId「:2,」標題「:」GRE Power Pre「 「Description」:「GRE Power Pre」,「Link」:「http://www.number2.com/exams/gre/companion/index.cfm?s=0」},{「ResourceId」:3,「Title」:「GRE分析寫作」,「說明」:「GRE分析 書寫」 「:」http://www.mygretutor.com/tests.aspx「},{」ResourceId「:4,」Title「:」GATE Architecture & Planning「,」Description「:」GATE Architecture & Planning「,」Link「:」http://www.onestopgate.com/gate-sample-papers/architecture-planning/「},{」ResourceId 「:5,」標題「:」TarGATE「,」描述「:」GATE 獲得您的 目標「,」鏈接「:」http://gateforum.com/Testseries-Venue.php「},{」ResourceId「:6,」標題「:」TOEFL 網考「‘說明’:‘託福網考測試樣品 問題’,‘鏈接’:」 https://www.ets.org/toefl/ibt/prepare/sample_questions「}]
這是通過網絡服務完成的。修改你的web服務代碼... –
謝謝你的快速響應,那麼如何改變我的web服務代碼 – koti
查看你的web服務代碼。將會有一個代碼檢查傳入的響應,並查看標誌或標題,告訴該服務「如果桌面應用程序請求或移動應用程序請求」。 –