0

我尝试读取服务器上的文本文件。但我没有返回测试。似乎我有一个异常错误。

如何在eclipse中查看错误,或显示错误?

如果我尝试记录错误,应用程序将关闭。(Log.e("Exception --->",e.getMessage());)

这里的代码在没有工作之后。

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;
}

任何的想法 ?

谢谢,

4

2 回答 2

1

所以,看看这个。这意味着您需要将所有代码从主线程移动到另一个线程。

于 2012-04-15T20:23:20.233 回答
0

尝试设置字符集

 connection.setRequestProperty("Accept-Charset", charset);

并摆脱BufferedReader。Http 不保证发送换行符。

也许使用 Apache Streams

http://commons.apache.org/fileupload/apidocs/org/apache/commons/fileupload/util/Streams.html

text_file = Streams.asString (urlConnection.getInputStream());
于 2012-04-15T19:46:37.567 回答