指向 urlConnection.connect() 的 Android 运行时错误 "Unable to start activity"

标签 android httpwebrequest runtime-error

我收到以下错误:

10-24 13:31:14.252: E/AndroidRuntime(21983): FATAL EXCEPTION: main
10-24 13:31:14.252: E/AndroidRuntime(21983): java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.campusfqm.cfqm/br.com.campusfqm.cfqm.Launch}: android.os.NetworkOnMainThreadException
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.os.Handler.dispatchMessage(Handler.java:99)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.os.Looper.loop(Looper.java:137)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.app.ActivityThread.main(ActivityThread.java:4507)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at java.lang.reflect.Method.invokeNative(Native Method)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at java.lang.reflect.Method.invoke(Method.java:511)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at dalvik.system.NativeStart.main(Native Method)
10-24 13:31:14.252: E/AndroidRuntime(21983): Caused by: android.os.NetworkOnMainThreadException
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1099)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at java.net.InetAddress.lookupHostByName(InetAddress.java:391)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at java.net.InetAddress.getAllByNameImpl(InetAddress.java:242)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at java.net.InetAddress.getAllByName(InetAddress.java:220)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:71)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:351)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:86)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:308)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpEngine.connect(HttpEngine.java:303)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:282)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:232)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at br.com.campusfqm.cfqm.Launch.onCreate(Launch.java:54)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.app.Activity.performCreate(Activity.java:4465)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
10-24 13:31:14.252: E/AndroidRuntime(21983):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
10-24 13:31:14.252: E/AndroidRuntime(21983):    ... 11 more

指向

urlConnection.connect();

这是完整的代码

            URL url = new URL("http://www.google.com/");

        //create the new connection
        HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

        //set up some things on the connection
        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);

        //and connect!
        urlConnection.connect();

        //set the path where we want to save the file
        //in this case, going to save it on the root directory of the
        //sd card.
        File SDCardRoot = Environment.getExternalStorageDirectory();
        //create a new file, specifying the path, and the filename
        //which we want to save the file as.
        File file = new File(SDCardRoot,fileName);

        //this will be used to write the downloaded data into the file we created
        FileOutputStream fileOutput = new FileOutputStream(file);

        //this will be used in reading the data from the internet
        InputStream inputStream = urlConnection.getInputStream();

        //this is the total size of the file
        int totalSize = urlConnection.getContentLength();
        //variable to store total downloaded bytes
        int downloadedSize = 0;

        //create a buffer...
        byte[] buffer = new byte[1024];
        int bufferLength = 0; //used to store a temporary size of the buffer

        //now, read through the input buffer and write the contents to the file
        while ( (bufferLength = inputStream.read(buffer)) > 0 ) {
                //add the data in the buffer to the file in the file output stream (the file on the sd card
                fileOutput.write(buffer, 0, bufferLength);
                //add up the size so we know how much is downloaded
                downloadedSize += bufferLength;
                //this is where you would do something to report the prgress, like this maybe
                Log.v("cfqm",""+downloadedSize+"/"+totalSize);

        }
        //close the output stream when done
        fileOutput.close();

最佳答案

它是 android.os.NetworkOnMainThreadException 意味着您正在主线程上进行网络连接。使用AsyncTask或thread代替UI线程执行网络操作

Read here about AsyncTask

Example

Similar problem

关于指向 urlConnection.connect() 的 Android 运行时错误 "Unable to start activity",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13052581/

相关文章:

java - 我应该如何在 Android Studio 3.0.1 中导出到 zip 文件

android - Skobbler Android 无法为自定义 SKAnnotationView 设置动画

android - 以下包不可用 : - Package id lldb;3. 0 - Android Studio 3.0

c# - WebRequest - 防止重定向

debugging - 通过调试来跟踪 VB6 运行时错误

c++ - OpenCV 奇怪的内存损坏

Android:关于加载联系信息的奇怪事情

c# - Paypal API,HttpWebRequest 抛出 SSL WebException

c# - HttpWebRequest 自动发送需要的HttpRequests

c++ - 如何从系统日期获取昨天的日期并附加到字符串?