简体   繁体   English

尝试访问网页时,应用程序为FC

[英]App is FC when trying to access a web page

I have the following code from my application: 我的应用程序中包含以下代码:

DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet("http://www.pagesbyz.com");
ResponseHandler<String> resHandler = new BasicResponseHandler();
try {
    String page = httpClient.execute(httpGet, resHandler);
    Toast.makeText(getApplicationContext(), "SUCCESS", 2000).show();
} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I added the following line to my Manifest: 我在清单中添加了以下行:

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

My app FC with the following LogCat error: 我的应用程序FC出现以下LogCat错误:

11-04 11:59:14.137: E/AndroidRuntime(11052): FATAL EXCEPTION: main
11-04 11:59:14.137: E/AndroidRuntime(11052): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.testing/com.test.testing.AndroidFragmentation}: android.os.NetworkOnMainThreadException
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2306)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2356)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1244)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.os.Handler.dispatchMessage(Handler.java:99)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.os.Looper.loop(Looper.java:137)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.main(ActivityThread.java:5195)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.lang.reflect.Method.invokeNative(Native Method)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.lang.reflect.Method.invoke(Method.java:511)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at dalvik.system.NativeStart.main(Native Method)
11-04 11:59:14.137: E/AndroidRuntime(11052): Caused by: android.os.NetworkOnMainThreadException
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at java.net.InetAddress.getAllByName(InetAddress.java:214)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:137)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:360)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at com.test.testing.AndroidFragmentation.onCreate(AndroidFragmentation.java:36)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.Activity.performCreate(Activity.java:5104)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
11-04 11:59:14.137: E/AndroidRuntime(11052):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2260)

Any idea how to resolve it? 知道如何解决吗? Thanks! 谢谢!

This is explained here . 在这里解释。 You should use an AsyncTask for network access, or Thread / HandlerThread. 您应该使用AsyncTask进行网络访问,或者使用Thread / HandlerThread。 Network access on main (UI) thread is forbidden since API 11. 自API 11起禁止在主(UI)线程上进行网络访问。

you could try something like this : 您可以尝试这样的事情:

private class HttpTask extends AsyncTask<String, Void, Void>{
    @Override
    protected Void doInBackground(String... params) {
        String url = params[0];
        DefaultHttpClient httpClient = new DefaultHttpClient();
        HttpGet httpGet = new HttpGet(url);
        ResponseHandler<String> resHandler = new BasicResponseHandler();
        try {
            String page = httpClient.execute(httpGet, resHandler);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        Toast.makeText(getApplicationContext(), "SUCCESS", 2000).show();
    }
}

and from your Activity/Fragment 和您的活动/片段

HttpTask task = new HttpTask();
task.execute(url);

You must make request from nonUI thread, for example use AsyncTask or ? 您必须从nonUI线程发出请求,例如使用AsyncTask或? extends Thread 扩展线程

The problem, you are facing is that you are running your network code on the main thread, as the NetworkOnMainThreadException states. 您面临的问题是,正如NetworkOnMainThreadException所述,您正在主线程上运行网络代码。

Look at the answer to this question, for an explanation on how to use AsyncTask to avoid this situation: How to fix android.os.NetworkOnMainThreadException? 查看此问题的答案,以获取有关如何使用AsyncTask避免这种情况的说明: 如何修复android.os.NetworkOnMainThreadException?

This blog post gives an in-depth explanation of the exception: http://www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html 这篇博客文章提供了对该异常的深入说明: http : //www.androiddesignpatterns.com/2012/06/app-force-close-honeycomb-ics.html

And lastly, the official NetworkOnMainThreadException documentation can be found here: http://developer.android.com/reference/android/os/NetworkOnMainThreadException.html 最后,可以在此处找到官方的NetworkOnMainThreadException文档: http : //developer.android.com/reference/android/os/NetworkOnMainThreadException.html

You are trying to get result from server directly from activity. 您正在尝试直接从活动中获取服务器的结果。 This was allowed until 2.x. 这允许直到2.x。 From Honeycomb onwards, NetworkOnMainThreadException was introduced. 从Honeycomb开始,引入了NetworkOnMainThreadException As this operation may take long time, it will block the UI thread, and will likely show ANR . 由于此操作可能需要很长时间,因此它将阻塞UI线程,并可能显示ANR For any time taking operation, a separate thread should be used. 对于任何花费时间的操作,都应使用单独的线程。

You can use AsyncTask . 您可以使用AsyncTask It runs on separate thread from your activity. 它在与您的活动不同的线程上运行。

In doInBackground() of AsyncTask, you can perform your desired operation and pass its result to onPostExecute() of this class based on which you can show the Toast message which you want to show in your application. 在AsyncTask的doInBackground()中,您可以执行所需的操作,并将其结果传递给此类的onPostExecute() ,基于此您可以显示要在应用程序中显示的Toast消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试访问 Web 应用程序中的文件时“访问被拒绝” - “Access Denied” when trying to access file in web app 尝试访问 Web 的某些部分而不是重定向到登录页面时提供简单的 HTTP 401 - Give simple HTTP 401 when trying to access some part of the web instead of redirect to login page 当我尝试从其他计算机访问小型 Web 应用程序时,CSS 不起作用 - CSS is not working when I am trying to access a small web app from other computers 尝试抓取 web 页面时出现“无 javascript”错误 - “no javascript” error when trying to scrape web page 尝试从Android应用访问本地主机上存储的php页面时出现错误“连接被拒绝” - error “connection refused” when trying to access to a php page stored on localhost from android app Jsoup:尝试访问页面时出现错误307 - Jsoup: error 307 when trying to access a page 尝试访问地图片段时应用崩溃 - App crash when trying to access mapfragment 从网页和Android应用访问网络服务 - Access web service from web page and Android app Tomcat:Null Pointer尝试访问Web服务时出现异常 - Tomcat: Null Pointer Exception when trying to access web service 尝试获取sun-web-app_2_5时,glassfish中的连接超时 - connection timeouts in glassfish when trying to get sun-web-app_2_5
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM