2012-01-17 21 views
1

ページからデータを受け取る必要があるAndroidアプリケーションを作成します。http://example.com/get.php?id=1このために私は次のメソッドを使用します。 アン:Webページ(Android)から情報を取得するにはどうすればいいですか?

private String getData() 
    { 
     HttpClient httpClient = new DefaultHttpClient(); 
     HttpContext localContext = new BasicHttpContext(); // !!! application stops here 
     HttpGet httpGet = new HttpGet("http://example.com/get.php?id=1"); 
     HttpResponse response = null; 
      try { 
       response = httpClient.execute(httpGet, localContext); 
      } catch (ClientProtocolException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     String result = ""; 

     BufferedReader reader = null; 
      try { 
       reader = new BufferedReader(
        new InputStreamReader(
         response.getEntity().getContent() 
        ) 
       ); 
      } catch (IllegalStateException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     String line = null; 
     try { 
       while ((line = reader.readLine()) != null){ 
        result += line + "\n"; 
       } 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 

     // Now you have the whole HTML loaded on the result variable 
     return result; 
    } 

を私はまた、AndroidManifest.xmlを

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.android.repetitor_ent_free" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-permission android:name="android.permission.INTERNET" /> 

問題は、私は、デバイス上でアプリケーションを実行すると、私はエラーを取得するということであるに権限を追加予期しないアプリケーションMyApp(プロセスcom.android.myapp)を停止します。もう一度お試しください。これを使用する

+0

ログインしてみてください。ネットワークがメインスレッドで使用されている場合は、エラーが表示されます(sdk api> 5)。 – m039

+0

Logcat出力をポストしてデバッグを容易にします。 – Thommy

+0

logcatを確認してください。あなたは例外を見つけて、ここに投稿します。 –

答えて

2

new Thread(){ 
    @Owerride 
    public void run(){ 
     URL url = new URL(urlString); 
     URLConnection conn = url.openConnection(); 
     InputStreamReader streamReader = new InputStreamReader(conn.getInputStream()); 
     BufferedReader br = new BufferedReader(streamReader); 
     StringBuilder sb = new StringBuilder(); 
     String line = null; 
     while((line = br.readLine()) != null) { 
      sb.append(line);sb.append("\n"); 
     } 
    } 
}.start(); 
1

試してください:あなたはより良いソリューションがAsyncTaskを使用しようとする希望の場合は、android.os.NetworkOnMainThreadExceptionを得た

HttpURLConnection conn = null; 

try { 

    URL url = new URL("http://example.com/get.php?id=1"); 
    conn = (HttpURLConnection) url.openConnection(); 

    conn.setRequestMethod("GET"); 
    conn.setRequestProperty("Content-length", "0"); 
    conn.setUseCaches(false); 
    conn.setAllowUserInteraction(false); 
    conn.connect(); 

    InputStreamReader streamReader = new InputStreamReader(conn.getInputStream()); 

    //Do what you need with the data here 

    } catch (Exception e) { 
     // Handle your exceptions 
    } finally { 
     if (conn != null) { 
      conn.disconnect(); 
      conn = null; 
     } 
    } 
} 
+0

私は同じエラーが発生します:http://pastebin.com/0ZgbRNVS – abg

+0

他のスレッドで起動しようとします。私は投稿に追加します –

+0

明らかにこのメソッドは機能しますが、テキスト出力の最後にアプリケーションが終了することを追加します。 TextView questionTextView =(TextView)findViewById(R.id.myTextView); myTextView.setText(sb.toString()); – abg

1

次のコードを試みることができます。スレッドの詳細については、 'processes and threads'をご覧ください(今後、この問題を避けるためにこのガイドをよくお読みください)。ローマの答えを使用してください)

1と、このようなものを追加します:

たとえば、あなたはこれらのソリューションのいずれかを試すことができ

final StringBuilder sb = ... ; 
final TextView tv = ... ; 

tv.post(new Runnable() { 
     public void run() { 
      tv.setText(sb.toString()); 
     } 
    }); 

または使用の活動方法:

runOnUiThread(new Runnable() { 
    public void run() { 
     final StringBuilder sb = ... ; 
     final TextView tv = ... ; 

     tv.setText(sb.toString()); 
    } 
}); 

2 )AsyncTaskを使用:

new AsyncTask<Void, Void, Void>() { 
    protected Void doInBackground(Void... none) { 
     // 
     // your code 
     // 

     return null; 
    } 
    protected void onProgressUpdate(Void... none) {} 
    protected void onPostExecute(Void none) { 
     // use this to set the text 

     final StringBuilder sb = ... ; 
     final TextView tv = ... ; 

     tv.setText(sb.toString());    
    } 
}.execute(); 
+0

同じ問題ですが、例外が発生します。 – abg

+0

ああ、ペーストビンのリンクを見つけられませんでした。メインのアクティビティでこのネットワークコードを実行していますか? @ m039で示唆されているように、ブロックまたはAysyncタスクを回避するには別のスレッドに存在する必要があります –

関連する問題