0

Android で簡単なリクエストをしようとしてhttpいますが、実行時にアプリケーションがクラッシュします。エラーは次の場所で発生します。

HttpResponse rp= hc.execute(post);

インターネットのアクセス許可が不足している可能性があると思いましたが、次を追加しました。

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

動作しませんでした。

これは私が使用したコードです:

public class Http_requestActivity extends Activity {
    /** Called when the activity is first created. */

    private String getPage() {
        String str = "***";

        try
        {
            HttpClient hc = new DefaultHttpClient();
            HttpPost post = new HttpPost("http://www.yahoo.com");

            HttpResponse rp = hc.execute(post);

            if (rp.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
            {
                str = EntityUtils.toString(rp.getEntity());
            }
        } catch(IOException e) {
            e.printStackTrace();
        }  

        return str;
    }


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        TextView txt = (TextView)findViewById(R.id.textView1);
        txt.setText(this.getPage());

    }
}

ログファイル:

06-19 09:04:47.277: E/AndroidRuntime(2213): FATAL EXCEPTION: main
06-19 09:04:47.277: E/AndroidRuntime(2213): android.os.NetworkOnMainThreadException
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.net.InetAddress.getAllByName(InetAddress.java:220)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.exatech.httprequest.Http_requestActivity.getPage(Http_requestActivity.java:99)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.exatech.httprequest.Http_requestActivity.access$0(Http_requestActivity.java:91)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.exatech.httprequest.Http_requestActivity$1.onClick(Http_requestActivity.java:123)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.view.View.performClick(View.java:3511)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.view.View$PerformClick.run(View.java:14105)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.os.Handler.handleCallback(Handler.java:605)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.os.Looper.loop(Looper.java:137)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.lang.reflect.Method.invokeNative(Native Method)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at java.lang.reflect.Method.invoke(Method.java:511)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-19 09:04:47.277: E/AndroidRuntime(2213):     at dalvik.system.NativeStart.main(Native Method)
4

0 に答える 0