Android httpget无法正常运行

时间:2012-11-14 19:57:07

标签: android http url get execute

我在使用android中的httpget执行网址时遇到问题。

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,并显示以下消息:“Code Failed:Null”。 我只是想执行一个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)

2 个答案:

答案 0 :(得分:1)

你应该在主UI线程上进行网络调用。以下是例外的定义:

  

应用程序尝试执行时抛出的异常   在其主线程上进行网络操作。

     

这仅适用于针对Honeycomb SDK或。的应用程序   更高。允许使用早期SDK版本的应用程序   他们的主要事件循环线程上的网络,但它是很重要的   气馁。

在以下帖子中查看我的答案:

Can java.util.zip.GZIPInputStream.close cause android.os.NetworkOnMainThreadException

答案 1 :(得分:1)

似乎堆栈跟踪非常有用。如果您尝试在主(UI)线程上进行长时间运行,则Strictmode会抛出异常。

Android线程模型有两个规则:

  1. 不要在主线程上做长时间的工作
  2. 不要从UI线程外部更新UI
  3. 您违反了第一条规则。