0

サーバー上のテキスト ファイルを読み込もうとしています。しかし、私はテストを返していません。例外エラーがあるようです。

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

何か案が ?

ありがとう、

4

2 に答える 2

1

だから、これを見てください。これは、すべてのコードをメイン スレッドから別のスレッドに移動する必要があることを意味します。

于 2012-04-15T20:23:20.233 に答える
0

文字セットを設定してみてください

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

そして、 BufferedReaderを取り除きます。Httpは改行を送信することを保証しません。

たぶんApacheStreamsを使用してください

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 に答える