HttpsURLConnection致命异常

时间:2013-01-28 08:05:31

标签: android httpsurlconnection

我是android程序的新手。我遵循api示例https://developer.android.com/reference/java/net/HttpURLConnection.html以打开连接。

这是我的功能:

        public void mylogin()
{       
    URL url = null;
    try {
        url = new URL("https://myurl");
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    HttpsURLConnection urlConnection = null;
    try {
        urlConnection = (HttpsURLConnection)
        url.openConnection();
        Toast.makeText(this, "success", Toast.LENGTH_SHORT).show();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, "io err", Toast.LENGTH_SHORT).show();
    }
    try {
        urlConnection.connect();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        Toast.makeText(this,"io err2", Toast.LENGTH_SHORT).show();
        e.printStackTrace();
    }

}

但是当我启动我的应用程序时,它会崩溃并且它会给我一个错误列表:

     FATAL EXCEPTION: main
     java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.example.connexion/com.example.connexion.MainActivity}:   android.os.NetworkOnMainThreadException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
at android.app.ActivityThread.access$600(ActivityThread.java:130)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4745)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.os.NetworkOnMainThreadException
at android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode.java:1117)
at java.net.InetAddress.lookupHostByName(InetAddress.java:385)
at java.net.InetAddress.getAllByNameImpl(InetAddress.java:236)
at java.net.InetAddress.getAllByName(InetAddress.java:214)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:70)
at libcore.net.http.HttpConnection.<init>(HttpConnection.java:50)
at libcore.net.http.HttpConnection$Address.connect(HttpConnection.java:341)
at libcore.net.http.HttpConnectionPool.get(HttpConnectionPool.java:87)
at libcore.net.http.HttpConnection.connect(HttpConnection.java:128)
at libcore.net.http.HttpEngine.openSocketConnection(HttpEngine.java:315)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.makeSslConnection(HttpsURLConnectionImpl.java:461)
at libcore.net.http.HttpsURLConnectionImpl$HttpsEngine.connect(HttpsURLConnectionImpl.java:433)
at libcore.net.http.HttpEngine.sendSocketRequest(HttpEngine.java:289)
at libcore.net.http.HttpEngine.sendRequest(HttpEngine.java:239)
at libcore.net.http.HttpURLConnectionImpl.connect(HttpURLConnectionImpl.java:80)
at libcore.net.http.HttpsURLConnectionImpl.connect(HttpsURLConnectionImpl.java:165)
at com.example.connexion.MainActivity.mylogin(MainActivity.java:84)
at com.example.connexion.MainActivity.onCreate(MainActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5008)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
... 11 more

我搜索了论坛,但无法找到解决方案。

提前谢谢。

1 个答案:

答案 0 :(得分:3)

您的问题是您正在主线程上执行网络操作:NetworkOnMainThreadException

您应该在单独的Thread中执行任何网络内容,例如AsyncTask