0

Androidでhttpgetを介してURLを実行する際に問題が発生しました。

try 
    {   
        DefaultHttpClient client = new DefaultHttpClient();
        HttpGet getURL = new HttpGet("http://www.example.com/index.php?text=x");
        client.execute(getURL);
        Toast.makeText(this.getBaseContext(), "I'm here", Toast.LENGTH_LONG).show();
    } catch (Exception e) {
        Toast.makeText(this.getBaseContext(), "Code Failed:" + e.getLocalizedMessage(), Toast.LENGTH_LONG).show();
        }

いくつかの理由で、私のアプリは「コードが失敗しました:Null」というメッセージとともにToeastを表示します。URLを実行したいだけです。URLが呼び出されると、私のphpファイルはデータベースのレコードを更新します。mysqlデータベースのデータベースを更新/挿入したいだけです。したがって、json / xml/other応答を解析する必要はありません。

logcatに例外を表示すると、次のように表示されます。

11-14 13:56:39.255: E/MYAPP(19638): exception
11-14 13:56:39.255: E/MYAPP(19638): android.os.NetworkOnMainThreadException
11-14 13:56:39.255: E/MYAPP(19638):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-14 13:56:39.255: E/MYAPP(19638):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-14 13:56:39.255: E/MYAPP(19638):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-14 13:56:39.255: E/MYAPP(19638):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-14 13:56:39.255: E/MYAPP(19638):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-14 13:56:39.255: E/MYAPP(19638):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-14 13:56:39.255: E/MYAPP(19638):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-14 13:56:39.255: E/MYAPP(19638):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-14 13:56:39.255: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
11-14 13:56:39.255: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
11-14 13:56:39.255: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-14 13:56:39.255: E/MYAPP(19638):     at com.thepl.shopapp.MainActivity.onCreate(MainActivity.java:156)
11-14 13:56:39.255: E/MYAPP(19638):     at android.app.Activity.performCreate(Activity.java:5184)
11-14 13:56:39.255: E/MYAPP(19638):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-14 13:56:39.255: E/MYAPP(19638):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-14 13:56:39.255: E/MYAPP(19638):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-14 13:56:39.255: E/MYAPP(19638):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-14 13:56:39.255: E/MYAPP(19638):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-14 13:56:39.255: E/MYAPP(19638):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 13:56:39.255: E/MYAPP(19638):     at android.os.Looper.loop(Looper.java:137)
11-14 13:56:39.255: E/MYAPP(19638):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-14 13:56:39.255: E/MYAPP(19638):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 13:56:39.255: E/MYAPP(19638):     at java.lang.reflect.Method.invoke(Method.java:511)
11-14 13:56:39.255: E/MYAPP(19638):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-14 13:56:39.255: E/MYAPP(19638):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-14 13:56:39.255: E/MYAPP(19638):     at dalvik.system.NativeStart.main(Native Method)
11-14 13:56:58.205: E/MYAPP(19638): exception
11-14 13:56:58.205: E/MYAPP(19638): android.os.NetworkOnMainThreadException
11-14 13:56:58.205: E/MYAPP(19638):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-14 13:56:58.205: E/MYAPP(19638):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-14 13:56:58.205: E/MYAPP(19638):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-14 13:56:58.205: E/MYAPP(19638):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-14 13:56:58.205: E/MYAPP(19638):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-14 13:56:58.205: E/MYAPP(19638):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-14 13:56:58.205: E/MYAPP(19638):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-14 13:56:58.205: E/MYAPP(19638):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-14 13:56:58.205: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
11-14 13:56:58.205: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
11-14 13:56:58.205: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-14 13:56:58.205: E/MYAPP(19638):     at com.thepl.shopapp.MainActivity.onCreate(MainActivity.java:156)
11-14 13:56:58.205: E/MYAPP(19638):     at android.app.Activity.performCreate(Activity.java:5184)
11-14 13:56:58.205: E/MYAPP(19638):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-14 13:56:58.205: E/MYAPP(19638):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-14 13:56:58.205: E/MYAPP(19638):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-14 13:56:58.205: E/MYAPP(19638):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-14 13:56:58.205: E/MYAPP(19638):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-14 13:56:58.205: E/MYAPP(19638):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 13:56:58.205: E/MYAPP(19638):     at android.os.Looper.loop(Looper.java:137)
11-14 13:56:58.205: E/MYAPP(19638):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-14 13:56:58.205: E/MYAPP(19638):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 13:56:58.205: E/MYAPP(19638):     at java.lang.reflect.Method.invoke(Method.java:511)
11-14 13:56:58.205: E/MYAPP(19638):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-14 13:56:58.205: E/MYAPP(19638):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-14 13:56:58.205: E/MYAPP(19638):     at dalvik.system.NativeStart.main(Native Method)
11-14 13:56:59.100: E/MYAPP(19638): exception
11-14 13:56:59.100: E/MYAPP(19638): android.os.NetworkOnMainThreadException
11-14 13:56:59.100: E/MYAPP(19638):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-14 13:56:59.100: E/MYAPP(19638):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-14 13:56:59.100: E/MYAPP(19638):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-14 13:56:59.100: E/MYAPP(19638):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-14 13:56:59.100: E/MYAPP(19638):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-14 13:56:59.100: E/MYAPP(19638):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-14 13:56:59.100: E/MYAPP(19638):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-14 13:56:59.100: E/MYAPP(19638):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-14 13:56:59.100: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
11-14 13:56:59.100: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
11-14 13:56:59.100: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-14 13:56:59.100: E/MYAPP(19638):     at com.thepl.shopapp.MainActivity.onCreate(MainActivity.java:156)
11-14 13:56:59.100: E/MYAPP(19638):     at android.app.Activity.performCreate(Activity.java:5184)
11-14 13:56:59.100: E/MYAPP(19638):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-14 13:56:59.100: E/MYAPP(19638):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-14 13:56:59.100: E/MYAPP(19638):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-14 13:56:59.100: E/MYAPP(19638):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-14 13:56:59.100: E/MYAPP(19638):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-14 13:56:59.100: E/MYAPP(19638):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 13:56:59.100: E/MYAPP(19638):     at android.os.Looper.loop(Looper.java:137)
11-14 13:56:59.100: E/MYAPP(19638):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-14 13:56:59.100: E/MYAPP(19638):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 13:56:59.100: E/MYAPP(19638):     at java.lang.reflect.Method.invoke(Method.java:511)
11-14 13:56:59.100: E/MYAPP(19638):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-14 13:56:59.100: E/MYAPP(19638):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-14 13:56:59.100: E/MYAPP(19638):     at dalvik.system.NativeStart.main(Native Method)
11-14 13:57:21.185: E/MYAPP(19638): exception
11-14 13:57:21.185: E/MYAPP(19638): android.os.NetworkOnMainThreadException
11-14 13:57:21.185: E/MYAPP(19638):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1118)
11-14 13:57:21.185: E/MYAPP(19638):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-14 13:57:21.185: E/MYAPP(19638):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-14 13:57:21.185: E/MYAPP(19638):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-14 13:57:21.185: E/MYAPP(19638):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-14 13:57:21.185: E/MYAPP(19638):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-14 13:57:21.185: E/MYAPP(19638):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-14 13:57:21.185: E/MYAPP(19638):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-14 13:57:21.185: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:670)
11-14 13:57:21.185: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:509)
11-14 13:57:21.185: E/MYAPP(19638):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
11-14 13:57:21.185: E/MYAPP(19638):     at com.thepl.shopapp.MainActivity.onCreate(MainActivity.java:156)
11-14 13:57:21.185: E/MYAPP(19638):     at android.app.Activity.performCreate(Activity.java:5184)
11-14 13:57:21.185: E/MYAPP(19638):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
11-14 13:57:21.185: E/MYAPP(19638):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
11-14 13:57:21.185: E/MYAPP(19638):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
11-14 13:57:21.185: E/MYAPP(19638):     at android.app.ActivityThread.access$600(ActivityThread.java:140)
11-14 13:57:21.185: E/MYAPP(19638):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
11-14 13:57:21.185: E/MYAPP(19638):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-14 13:57:21.185: E/MYAPP(19638):     at android.os.Looper.loop(Looper.java:137)
11-14 13:57:21.185: E/MYAPP(19638):     at android.app.ActivityThread.main(ActivityThread.java:4898)
11-14 13:57:21.185: E/MYAPP(19638):     at java.lang.reflect.Method.invokeNative(Native Method)
11-14 13:57:21.185: E/MYAPP(19638):     at java.lang.reflect.Method.invoke(Method.java:511)
11-14 13:57:21.185: E/MYAPP(19638):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
11-14 13:57:21.185: E/MYAPP(19638):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
11-14 13:57:21.185: E/MYAPP(19638):     at dalvik.system.NativeStart.main(Native Method)
4

2 に答える 2

1

メインUIスレッドでネットワーク呼び出しを行う必要があります。例外の定義は次のとおりです。

アプリケーションがメインスレッドでネットワーク操作を実行しようとしたときにスローされる例外。

これは、HoneycombSDK以降を対象とするアプリケーションに対してのみスローされます。以前のSDKバージョンを対象とするアプリケーションは、メインのイベントループスレッドでネットワークを構築できますが、お勧めできません。

解決策については、次の投稿で私の答えを確認してください:

java.util.zip.GZIPInputStream.closeはandroid.os.NetworkOnMainThreadExceptionを引き起こす可能性があります

于 2012-11-14T20:21:20.357 に答える
1

スタックトレースはかなり便利なようです。Strictmodeがオンになっていると、メイン(UI)スレッドで長時間実行される作業を実行しようとすると例外がスローされます。

Androidスレッドモデルには2つのルールがあります。

  1. メインスレッドで長い作業をしないでください
  2. UIスレッドの外部からUIを更新しないでください

これらの最初のルールに違反しました。

于 2012-11-14T20:21:42.470 に答える