2012-09-03 36 views
0

正試圖從URL中獲取XML文件,但沒有得到響應,並且代碼稍後停止,因爲字符串xml爲null,你能告訴我最新的問題嗎?XML解析使用Java沒有得到任何迴應

public String getXmlFromUrl(String url) { 
String xml = null; 

    try { 


     DefaultHttpClient httpClient = new DefaultHttpClient(); 
     HttpPost httpPost = new HttpPost(url); 
     HttpResponse httpResponse = httpClient.execute(httpPost); 
        // I printed the response here but I got nothing ! 
     HttpEntity httpEntity = httpResponse.getEntity(); 
     xml = EntityUtils.toString(httpEntity); 
        return xml; 


    } catch (Exception e) { 
     e.printStackTrace(); 
    } 

請具體說明在你的答案我感謝你的幫助

+0

您是否在瀏覽器中檢查了URL,如果它返回一些數據? –

+0

您是否嘗試打印'xml'? xml = EntityUtils.toString(httpEntity); – rahul

+0

@DmytroDanylyk是的,我做了,它的工作正常 –

回答

1

爲什麼你正在使用HTTPPost?你甚至沒有發送任何數據。嘗試使用HttpGet。

試試這個:

public String getXmlFromUrl(String url) throws Exception { 
    return new AsyncTask<String, Void, String>() { 
     @Override 
     protected String doInBackground(String... params) { 
      String xml = null; 
      try { 
       DefaultHttpClient httpClient = new DefaultHttpClient(); 
       HttpGet httpPost = new HttpGet(params[0]); 
       HttpResponse httpResponse = httpClient.execute(httpPost); 
       // I printed the response here but I got nothing ! 
       HttpEntity httpEntity = httpResponse.getEntity(); 
       xml = EntityUtils.toString(httpEntity); 
       Log.i("DEMO", xml); 

      } catch (Exception e) { 
       e.printStackTrace(); 
      } 
      return xml; 
     } 
    }.execute(url).get(); 

} 
+0

但這個「Document doc = parser.getDomElement(xml); //獲取DOM元素」不再工作 –

+0

這是一件有趣的事情,創建一個asynctask並用相同的方法等待答案。我不明白你爲什麼要那樣做? – njzk2

+0

我剛剛糾正了他的答案... –

0

嘗試例如啓動從單獨的線程代碼

new Thread(new Runnable() 
{ 

    @Override 
    public void run() 
    { 
     // TODO your code here 

    } 
    }).start(); 
0

試試這個:

try { 

    items = new ArrayList<String>(); 

    XmlPullParserFactory factory = XmlPullParserFactory.newInstance(); 
    factory.setNamespaceAware(true); 
    XmlPullParser xpp = factory.newPullParser(); 
    xpp.setInput(new InputStreamReader(
      getUrlData(" url"))); 

    while (xpp.getEventType() != XmlPullParser.END_DOCUMENT) { 
     Log.i(TAG, "doc started"); 
     if (xpp.getEventType() == XmlPullParser.START_TAG) { 
      if (xpp.getName().equals("entry")) { 
       items.add(xpp.getAttributeValue(0)); 
      } 
     } 
     xpp.next(); 

    } 
} catch (Throwable t) { 
    Toast.makeText(this, "Request failed: " + t.toString(), 
      Toast.LENGTH_LONG).show(); 
} 
0

爲geturldata()

public InputStream getUrlData(String url) throws URISyntaxException, 
    ClientProtocolException, IOException { 

DefaultHttpClient client = new DefaultHttpClient(); 
HttpGet method = new HttpGet(new URI(url)); 
HttpResponse res = client.execute(method); 
return res.getEntity().getContent(); 

}

0

你應該明白例外。 xml的原因是null是因爲'某些事情'出了問題。這扔了一個Exception,可能很好地描述了哪裏出了問題。當發生這種情況時,Exception被拋出,直到有人處理它。

Exception的每個子類都有不同的「風味」,並在特定情況下拋出。這使您能夠對錯誤做出「反應」。例如,您可以告訴用戶出了什麼問題,或者爲了調試而記錄某​​些內容。

就你而言,你在一個地方「捕捉」所有異常,當發生異常時,執行catch (Exception e)之後的代碼。你在這裏什麼都不做,只是打印出一些東西(這在LogCat中會顯示爲橙色)。然後你繼續,好像什麼都沒發生。但是xml將爲null,這對您的程序不利,而且您顯然沒有注意到LogCat項,因爲您的程序在稍後崩潰。

這一次,Eldhose M Babu解決了您的問題。下一次,當其他事情出錯(並且很多可能在htttp請求中出錯)時,您的程序將顯示相同的行爲。嘗試閱讀例外情況,並在處理它們過於沉默時小心。

+0

我處理了每個例外dw thx for ur help –