HttpUrlConnection.connect 抛出异常

HttpUrlConnection.connect throws an exception

在 AndroidStudio 中开发,我有以下代码连接和post一些数据到服务器:

try {
            EditText et = (EditText)findViewById(R.id.friend);
            EditText et2 = (EditText)findViewById(R.id.name);
            HashMap<String,String> hm = new HashMap<>();
            hm.put("name", et2.getText().toString());
            hm.put("friend_name", et.getText().toString());
            URL url = new URL("http://wwtbamandroid.appspot.com/rest/friends");
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("POST");
            con.setDoOutput(true);
            con.connect();
            BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), "UTF-8"));
            writer.write(getPostDataString(hm));
            writer.flush();
            writer.close();
        }
        catch (Exception e){
            Log.e("Error", "Connection error -> "+e.getMessage());}

并且在 con.connect() 行抛出一个异常;我不明白为什么,我找不到错误。谢谢你的帮助

这是我的 logcat:

08-02 21:15:07.798  16864-16864/? I/art﹕ Late-enabling -Xcheck:jni
08-02 21:15:08.070  16864-16881/android.josema.whowantstobeamillionaire I/art﹕ Background partial concurrent mark sweep GC freed 187(24KB) AllocSpace objects, 0(0B) LOS objects, 31% free, 35MB/51MB, paused 6.048ms total 24.693ms
08-02 21:15:08.091  16864-16897/android.josema.whowantstobeamillionaire D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
08-02 21:15:08.096  16864-16864/android.josema.whowantstobeamillionaire D/Atlas﹕ Validating map...
08-02 21:15:08.149  16864-16897/android.josema.whowantstobeamillionaire I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/14/15, ab0075f, Id3510ff6dc
08-02 21:15:08.150  16864-16897/android.josema.whowantstobeamillionaire I/OpenGLRenderer﹕ Initialized EGL, version 1.4
08-02 21:15:08.163  16864-16897/android.josema.whowantstobeamillionaire D/OpenGLRenderer﹕ Enabling debug mode 0
08-02 21:15:15.181  16864-16864/android.josema.whowantstobeamillionaire 
E/Error﹕ Connection error -> null

使用这行代码

       Thread background = new Thread(new Runnable() {


                // After call for background.start this run method call
        public void run() {
        try {

        HashMap<String,String> hm = new HashMap<>();
        hm.put("name", et2.getText().toString());
        hm.put("friend_name", et.getText().toString());
        URL url = new URL("http://wwtbamandroid.appspot.com/rest/friends");
        HttpURLConnection con = (HttpURLConnection) url.openConnection();
        con.setRequestMethod("POST");
        con.setDoOutput(true);
        con.connect();
        BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(con.getOutputStream(), "UTF-8"));
        writer.write(getPostDataString(hm));
        writer.flush();
        writer.close();


         } catch (Throwable t) {
                   // just end the background thread
                  Log.i("Animation", "Thread  exception " + t);                       }
                }



            });
            // Start Thread
            background.start();