HttpUrlConnection.connect抛出异常

时间:2015-08-02 19:09:31

标签: android android-studio connect httpurlconnection

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

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

1 个答案:

答案 0 :(得分:1)

使用此行代码

       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();