我嘗試讀取服務器上的文本文件。但我沒有返回測試。似乎我有一個異常錯誤。閱讀遠程服務器上的文本文件時出現Android錯誤
如何在eclipse中查看錯誤或顯示錯誤?
如果我嘗試記錄錯誤,應用程序將關閉。 (Log.e(「Exception --->」,e.getMessage());)
doent工作後的代碼。
public static String getText(String urlAdress) {
String text_file = "";
BufferedReader in = null;
try {
// create a url object
URL url = new URL(urlAdress);
// create a urlconnection object
URLConnection urlConnection = url.openConnection();
// Read all the text returned by the server
in = new BufferedReader(new InputStreamReader(
urlConnection.getInputStream()));
String str;
while ((str = in.readLine()) != null) {
// str is one line of text; readLine() strips the newline
// character(s)
text_file = text_file + str;
}
in.close();
} catch (MalformedURLException e) {
text_file = "error";
Log.e("MalformedURLException --->", e.getMessage());
} catch (IOException e) {
text_file = "error";
Log.e("IOException --->", e.getMessage());
} catch (Exception e) {
text_file = "error";
// Log.e("Exception --->",e.getMessage());
e.printStackTrace();
} finally {
if (in != null) {
try {
in.close();
} catch (IOException e) {
Log.e("finally IOException --->",
"Exception trying to close BufferedReader");
}
}
}
return text_file;
}
任何想法?
感謝,
再次使用LogCat,Luke! – 2012-04-15 19:43:51
這是我在LogCat中的: 04-15 22:16:31.016:W/System.err(26558):android.os.NetworkOnMainThreadException 04-15 22:16:31.036:W/System.err( 26558):\t at android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178) 04-15 22:16:31.036:W/System.err(26558):\t at java.net.InetAddress.lookupHostByName( InetAddress.java:394) 04-15 22:16:31.036:W/System.err(26558):\t at java.net.InetAddress.getAllByNameImpl(InetAddress.java:245) 04-15 22:16:31.036 :W/System.err(26558):\t at java.net.InetAddress.getAllByName(InetAddress.java:220) ... – 2012-04-15 20:18:58
請使用StringBuffer創建字符串。不要做text_file = text_file + str;因爲這是不好的做法,而且效率低下! – Raunak 2012-04-16 01:01:51