2

Developing in AndroidStudio, I've got the following code to connect and post some data to a server:

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

And an Exception is thrown in the line con.connect(); I don't understand why and I can't find the mistake. Thank you for the help

This is my 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
Lal
  • 14,893
  • 4
  • 41
  • 68
JOSEMAFUEN
  • 593
  • 2
  • 13
  • 36
  • what is the exception? Is that NetworkOnMainThread Exception? could you please post the logcat too.. – Lal Aug 02 '15 at 19:10
  • your connection is null..check if such a connection exists – Lal Aug 02 '15 at 19:16
  • I have checked for it if(con==null) Log.d("Null","Your connection is null"); if(con!=null) Log.d("Null","Your connection isn't null"); and it isn't null – JOSEMAFUEN Aug 02 '15 at 19:20

1 Answers1

1

Use this line of code

       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();
Dharmaraj
  • 1,141
  • 11
  • 12