0
try {
            HttpClient client = new DefaultHttpClient();
            HttpGet request = new HttpGet("http://www.google.com");
            client.execute(request);//it fails at this line
            Log.e("yo", "yo");
        } catch (Exception e) {}

有没有人解决这个问题,因为我遇到了同样的问题。我的设备连接到同一个网络,在浏览器中粘贴 URL 有效,但使用 HTTP 无效。

4

3 回答 3

1

试试 HttpPost 方法

在 manfiest 文件中声明INTERNET 许可

httpclient=new DefaultHttpClient();
     HttpPost httppost=new HttpPost(URL);

     HttpResponse res = null;
        try {
            res = httpclient.execute(httppost);
            System.out.println("asa "+res);

        } catch (ClientProtocolException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        } 
于 2012-09-09T07:23:25.383 回答
0

您是否忘记添加

<uses-permission android:name="android.permission.INTERNET" />

到您的 AndroidManifest.xml?

于 2012-09-04T21:13:25.803 回答
0

您是否在与 UI 相同的线程上运行它?您必须为任何网络连接使用单独的线程以防止 UI 阻塞。在这种情况下,您需要使用 AsyncTask;显然,需要 android Internet 权限。

于 2012-09-09T07:42:01.737 回答