0

我正在尝试向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();
                }
            }
        }
    }   

日志猫:

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

4 回答 4

2

我认为文档描述足够丰富,可以理解异常NetworkOnMainThreadException

当应用程序尝试在其主线程上执行网络操作时引发的异常。

这仅针对面向 Honeycomb SDK 或更高版本的应用程序抛出。允许以早期 SDK 版本为目标的应用程序在其主事件循环线程上进行网络连接,但非常不鼓励这样做。见文件

试试这个让你知道如何处理网络操作的链接http://developer.android.com/training/articles/perf-anr.html

于 2013-04-10T13:21:26.380 回答
1

你有一个NetworkOnMainThreadException. 在AsyncTask.

于 2013-04-10T12:54:30.870 回答
0

当您遇到此类琐碎问题时,最好先通过谷歌搜索查找异常描述,然后通过查看此类给出的解释和链接将问题归零。它几乎成倍地减少了您的开发时间。只是一个诚实的建议,我不是专家:)

是的,AsyncTask 应该可以解决问题。

于 2013-04-10T13:29:24.170 回答
0

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

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

通过安卓应用程序访问互联网的问题。

于 2013-04-10T12:54:21.567 回答