2017-10-21 51 views
0
public String getMetaData() { 
    String errors = ""; 
    try { 
     URL url = new URL("http://in2streaming.com:9999/stats?sid=1.xml"); 
     URLConnection conn = url.openConnection(); 

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     // Error Here: 
     Document doc = db.parse(conn.getInputStream().toString()); 

     // get the root node 
     NodeList nodeList = doc.getElementsByTagName("SHOUTCASTSERVER"); 
     Node node=nodeList.item(0); 
     // the node has three child nodes 
     for (int i = 0; i < node.getChildNodes().getLength(); i++) { 
      Node temp=node.getChildNodes().item(i); 
      if(temp.getNodeName().equalsIgnoreCase("SONGTITLE")){ 
       return temp.getTextContent(); 
      } 
     } 
     return "Couldn't reach XML"; 
    } 
    catch (Exception e) { 
     return "Exception "; 
    } 
} 

通過調用Runnable接口,拿到了異常這一功能android.os.NetworkOnMainThreadException 我可能鏈接更改爲http://in2streaming.com:9999/7.html和使用HTML解析器,而不是錯誤中獲取XML和解析它

// Refresh meta data 
private final Runnable refresh_meta = new Runnable() { 
    @Override 
    public void run() { 
     Toast.makeText(m_context, getMetaData(), Toast.LENGTH_SHORT).show(); 
     m_handler.postDelayed(this, 5000); 
    } 
}; 
+0

有什麼錯誤?發佈logcat – pleft

+0

java.lang.NullPointerException:試圖調用虛擬方法'void android.app.NotificationManager.createNotificationChannel(android.app.NotificationChannel)'在空對象引用 at cloud.SmoothRadio.nagare.MainJava.showNotification(MainJava .java:60) at cloud.SmoothRadio.nagare.MainJava.onClick(MainJava.java:120) –

+0

此錯誤不屬於發佈在問題中的源代碼。 – pleft

回答

2

對於NetworkOnMainThreadException(你也可以使用的AsyncTask):

Executors.newSingleThreadExecutor().submit(new Runnable() { 
      @Override 
      public void run() { 
       Toast.makeText(MainActivity.this, getMetaData(), Toast.LENGTH_SHORT).show(); 
      } 
     }); 

如果要計劃每5秒。

您可以使用ScheduledExecutorService的

ScheduledExecutorService worker = Executors.newSingleThreadScheduledExecutor(); 

     worker.scheduleAtFixedRate(refresh_meta, 
       1, //initial delay 
       5, //run every 5 seconds 
       TimeUnit.SECONDS); 

和更新您的Runnable作爲

private Runnable refresh_meta = new Runnable() { 
     @Override 
     public void run() { 
      final String text = getMetaData(); 

      runOnUiThread(new Runnable() { 
           @Override 
           public void run() { 
            Toast.makeText(m_context, text, Toast.LENGTH_SHORT).show(); 
           } 
          } 
      ); 


     } 
    }; 

此外,

變化Document doc = db.parse(conn.getInputStream().toString());

Document doc = db.parse(conn.getInputStream()); 
+0

我沒有提到我通過Runnable調用這個函數 –

+1

@mazenelzoor你是否也可以添加該代碼來提問posted –

+0

@mazenelzoor我用ScheduledExecutorService代替了Handler.Update你的Runnable refresh_meta;並使用上面的代碼嘗試。 –

1

首先,一些言論:

一)不喜歡你在這裏做裹住例外:

catch (Exception e) { 
    return "Exception "; 
} 

這樣你永遠不會知道拋出的異常是什麼。這是更好地記錄/打印異常的堆棧跟蹤,例如:

catch (Exception e) { 
    Log.e("TAG", "Error", e); 
    return "Exception"; 
} 

B)conn.getInputStream().toString()不這樣做,你想它做什麼(轉換InputStreamString)。 DocumentBuilderparse方法需要InputStream作爲參數,無需將其轉換爲String

銘記這裏上面是你的方法:

public String getMetaData() { 
    String errors = ""; 
    try { 
     URL url = new URL("http://in2streaming.com:9999/stats?sid=1.xml"); 
     URLConnection conn = url.openConnection(); 

     DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 
     DocumentBuilder db = dbf.newDocumentBuilder(); 
     // Error Here: 
     Document doc = db.parse(conn.getInputStream()); 

     // get the root node 
     NodeList nodeList = doc.getElementsByTagName("SHOUTCASTSERVER"); 
     Node node=nodeList.item(0); 
     // the node has three child nodes 
     for (int i = 0; i < node.getChildNodes().getLength(); i++) { 
      Node temp=node.getChildNodes().item(i); 
      if(temp.getNodeName().equalsIgnoreCase("SONGTITLE")){ 
       return temp.getTextContent(); 
      } 
     } 
     return "Couldn't reach XML"; 
    } 
    catch (Exception e) { 
     Log.e("TAG", "Error in getMetaData()", e); 
     return "Exception "; 
    } 
} 

嘗試再次運行應用程序,如果出現錯誤,從這個方法產生,將與在的getMetaData消息「錯誤的logcat的打印() 」。相應地更新您的問題,讓其他成員幫助您。