Android HttpGet请求不发送

时间:2013-04-10 12:43:13

标签: java android http

我正在尝试向http://www.google.com/发送HTTP请求,并希望在我的Android应用程序中保存所有响应。我尝试了很多方法,但每次我遇到错误或logcat错误或我的模拟器停止。

请指出我的代码有什么问题并给我一些配置提示?我希望能够同时使用GETPOST

 public void executeHttpGet() throws Exception {
     BufferedReader in = null;
        try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet();
            request.setURI(new URI("http://www.google.com/"));
            HttpResponse response = client.execute(request);
            in = new BufferedReader
            (new InputStreamReader(response.getEntity().getContent()));
            StringBuffer sb = new StringBuffer("");
            String line = "";
            String NL = System.getProperty("line.separator");
            while ((line = in.readLine()) != null) {
                sb.append(line + NL);
            }
            result.setText(sb.toString());

            in.close();
            String page = sb.toString();
            System.out.println(page);
            Debug.out(sb.toString());

            } finally {
            if (in != null) {
                try {
                    in.close();
                    } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }   

logcat的:

04-09 20:56:07.730: E/AndroidRuntime(9128): FATAL EXCEPTION: main
04-09 20:56:07.730: E/AndroidRuntime(9128): java.lang.RuntimeException: Unable to start activity ComponentInfo{mainpackage.rest_client/mainpackage.rest_client.GetResponse}: android.os.NetworkOnMainThreadException
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.app.ActivityThread.access$600(ActivityThread.java:141)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.os.Looper.loop(Looper.java:137)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.app.ActivityThread.main(ActivityThread.java:5041)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at java.lang.reflect.Method.invoke(Method.java:511)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at dalvik.system.NativeStart.main(Native Method)
04-09 20:56:07.730: E/AndroidRuntime(9128): Caused by: android.os.NetworkOnMainThreadException
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at java.net.InetAddress.getAllByName(InetAddress.java:214)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at mainpackage.rest_client.GetResponse.QueryGooglePlus(GetResponse.java:75)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at mainpackage.rest_client.GetResponse.onCreate(GetResponse.java:61)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.app.Activity.performCreate(Activity.java:5104)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
04-09 20:56:07.730: E/AndroidRuntime(9128):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
04-09 20:56:07.730: E/AndroidRuntime(9128):     ... 11 more
04-09 20:56:08.330: D/dalvikvm(9128): GC_CONCURRENT freed 204K, 11% free 2889K/3216K, paused 6ms+44ms, total 417ms
04-09 20:56:11.640: I/Process(9128): Sending signal. PID: 9128 SIG: 9

4 个答案:

答案 0 :(得分:2)

我认为文档描述足以理解异常NetworkOnMainThreadException

  

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

     

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

尝试此链接,告诉您如何处理网络操作http://developer.android.com/training/articles/perf-anr.html

答案 1 :(得分:1)

你有NetworkOnMainThreadException。在AsyncTask

中进行联网

答案 2 :(得分:0)

请检查您是否可以在模拟器中访问互联网。

如果您无法访问互联网,请尝试以下解决方案,它对我有用:)

Issue in accessing internet through android applications.

答案 3 :(得分:0)

当你面对像这样的微不足道的问题时,你的学习总是更好地通过谷歌搜索来查找exception description,然后通过查看像these这样的解释和链接来解决问题。它几乎会以指数方式缩短您的开发时间。 只是一个诚实的建议,我不是专家:)

而且,AsyncTask应该可以解决问题。