Ich versuche, eine Textdatei auf einem Server zu lesen. Aber ich habe den Test nicht zurückgegeben. Scheint, dass ich einen Ausnahmefehler habe.Android-Fehler beim Lesen von Textdatei auf Remote-Server
Wie kann man den Fehler in Eclipse sehen oder den Fehler anzeigen?
Wenn ich versuche, den Erreor zu protokollieren, wird die App geschlossen. (Log.e ("Ausnahme --->", e.getMessage());)
Der Code hier nach der Arbeit.
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;
}
Irgendeine Idee?
Danke,
Und wieder LogCat, Luke! –
Hier ist, was ich in LogCat habe: 04-15 22: 16: 31.016: W/System.err (26558): android.os.NetworkOnMainThreadException 04-15 22: 16: 31.036: W/System.err (26558): \t bei android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork (StrictMode.java:1178) 04-15 22: 16: 31.036: W/System.err (26558): \t bei java.net.InetAddress.lookupHostByName (InetAddress.java:394) 04-15 22: 16: 31.036: W/System.err (26558): \t bei java.net.InetAddress.getAllByNameImpl (InetAddress.java:245) 04-15 22: 16: 31.036 : W/System.err (26558): \t bei java.net.InetAddress.getAllByName (InetAddress.java:220) ... –
Bitte verwenden Sie einen StringBuffer um die Zeichenfolge zu erstellen. Tue nicht text_file = Textdatei + str; wie es schlecht ist, und es ist ineffizient !! – Raunak