2012-02-25 11 views
2

ウェブサイトから1行のテキストを読み込もうとすると、エラー(NetworkOnMainThreadException)が発生します。私はいくつか試しましたが、これまでのところ何もできません。誰でも手伝ってもらえれば、ここにコードがあります。マニフェストではインターネットにアクセス権があるので問題はないはずです。NetworkOnMainThreadExceptionウェブから読み取るとき

import java.io.BufferedReader; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 

import org.apache.http.HttpEntity; 
import org.apache.http.HttpResponse; 
import org.apache.http.client.ClientProtocolException; 
import org.apache.http.client.HttpClient; 
import org.apache.http.client.methods.HttpGet; 
import org.apache.http.impl.client.DefaultHttpClient; 

import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.TextView; 

public class Weather extends Activity { 

    Button button; 
    TextView t; 
    String result; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     setContentView(R.layout.weather); 

     t = (TextView)findViewById(R.id.textView1); 

    } 

    public void myButtonClickHandler (View view) throws ClientProtocolException, IOException { 
     result = getContentFromUrl("http://url.com"); 
     t.setText(result); 
    } 

    public static String getContentFromUrl(String url) throws ClientProtocolException, IOException { 

     HttpClient httpClient = new DefaultHttpClient(); 
     HttpGet httpGet = new HttpGet(url); 
     HttpResponse response; 

     response = httpClient.execute(httpGet); 
     HttpEntity entity = response.getEntity(); 

     if(entity != null) { 

      InputStream inStream = entity.getContent(); 

      String result = Weather.convertStreamToString(inStream); 
      inStream.close(); 

      return result; 
     } 

     return null; 

    } 

    private static String convertStreamToString(InputStream is) { 
     BufferedReader reader = new BufferedReader(new InputStreamReader(is)); 
     StringBuilder sb = new StringBuilder(); 

     String line = null; 

     try { 
      while ((line = reader.readLine()) != null) { 
       sb.append(line + "\n"); 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       is.close(); 
      } catch (IOException e) { 
       e.printStackTrace(); 
      } 
     } 
     return sb.toString(); 
    } 
} 

答えて

7

メインスレッドでネットワークアクティビティを使用しないでください。この例外は、Honeycomb SDK以上をターゲットとするアプリケーションでのみスローされます。すべてのネットワーク関連のタスクを別のスレッドで実行します(ハンドラlooperまたはrunOnUiThreadのどちらかを使用します)。あなたの問題は解決されます。あなたはAPIのAsyncTask、http://developer.android.com/reference/android/os/AsyncTask.html

またはあなたの場合を使用して、すべてのダウンロードを行いたいかもしれません

+0

ハム、私にこの例を教えていただけますか? – HyperX

+1

例ではhttp://www.vogella.de/articles/AndroidPerformance/article.html、http://www.xoriant.com/blog/mobile-application-development/android-async-task.html、およびhttp://developer.android.com/resources/articles/painless-threading.html –

4

あなたは、バックグラウンドスレッドに移動することができますかあなただけにしたい場合はどちらかちょうど()あなたのonCreateでこれを追加し、このエラーを解消:

StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build(); 

    StrictMode.setThreadPolicy(policy); 

はマニフェストに、インターネットのアクセス許可を追加することを忘れないでください:

<uses-permission android:name="android.permission.INTERNET"/> 
関連する問題