我在通过 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();
}
出于某种原因,我的应用程序将显示一个带有消息的 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)