2012-04-15 5 views
0

サーバー上のテキストファイルを読み取ろうとしています。しかし、私はテストを返す必要はありません。私は例外エラーがあるようだ。リモートサーバーのテキストファイルを読む際にAndroidエラーが発生する

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

おかげで、文字セット

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

BufferedReaderのを取り除くを設定する

+1

また、LogCat、Luke!を使用してください。 –

+0

これは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 android.os.StrictMode $ AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1178) 04-15 22:16:31.036:W/System.err(26558):\t java.net.InetAddress.lookupHostByName (InetAddress.java:234) 04-15 22:16:31.036:W/System.err(26558):\t java.net.InetAddress.getAllByNameImpl :W/System.err(26558):\t java.net.InetAddress.getAllByName(InetAddress.java:220) ... –

+0

StringBuffer Stringを作成します。しないでくださいtext_file = text_file + str;それは悪い習慣であり、それは非効率的です! – Raunak

答えて

1

を使用しています。つまり、すべてのコードをメインスレッドから別のスレッドに移動する必要があります。

+0

ありがとう、 私はlogcatをよく読んで、最初の行に書かれています(上記のようにコピーしました)。 チェックするだけで、私はStrictmodeポリシーを無効にしました。 このプロセスをスクリプトの他の部分に書き直す必要があります。 André。 –

関連する問題