1. Download our Official Android App: Forums for Android!

Apps Http Connection Error - Already Connected ? No it isn't!

Discussion in 'Android Development' started by ac4android, Aug 1, 2016.

  1. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    Hi, I am re-using some http connection code, they worked well in my AsyncTask, tested and true.

    This time, I called the method inside my Handler, like this:
    Code (Text):
    1.  
    2.   final Handler handler = new Handler();
    3.   handler.post(new Runnable() {
    4.   @Override
    5.   public void run() {
    6.   //TODO
    7.   String slat = String.valueOf(dd);
    8.   sysmsgview.setText(slat);
    9.   latitudeview.setText(latitude);
    10.   longitudeview.setText(longitude);
    11.   accuracyview.setText(accuracy);
    12.   if (running) {
    13.   dd++;
    14.   // hardvoded for testing
    15.   latitude = "84.012345";
    16.   longitude = "-114.2345";
    17.   if (latitude != null && longitude != null){
    18.   Toast.makeText(getApplicationContext(),
    19.   "Uploading "+ dd + " Latitude:Longitude ->" + latitude +":" + longitude,
    20. Toast.LENGTH_LONG).show();
    21.  
    22.  
    23.   new UploadActivity(getApplicationContext()).execute( latitude, longitude);
    24.   }
    25.   }
    26.   handler.postDelayed(this, 10000);
    27.   }
    28.   });
    29.   }
    30.  
    It threw a java.lang.illegalStateException: Already connected.
    How could it be already connected?????? Doesn't make sense.

    Code (Text):
    1.  
    2.   HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    3.   if (!(conn instanceof HttpURLConnection)) {
    4.   throw new IOException("ERROR: urlConn not instance of HttpUrlConnection.");
    5.   }
    6.   conn.setRequestMethod("POST");
    7.   conn.connect();
    8.   conn.setDoOutput(true);
    9.   OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); // <-- THROWS EXCEPTION HERE!
    10.   writer.write(data);
    11.   writer.flush();
    12.   BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
    13.   StringBuilder stringBuilder = new StringBuilder();
    14.   String line = null;
    15.   // Read Server Response
    16.   while ((line = reader.readLine()) != null) {
    17.   stringBuilder.append(line);
    18.   break;
    19.   }
    20.   reader.close();
    21.   return stringBuilder.toString();
    22.   } catch (Exception e) {
    23.   return new String("Exception: " + e.getMessage());
    24.   }
    25.  
    httpError.JPG

    httpError222.JPG
    httpError333.JPG
     

    Advertisement

  2. LV426

    LV426 I say we take off and nuke this place from orbit
    Moderator
    Rank:
     #11
    Points:
    1,988
    Posts:
    7,276
    Joined:
    Oct 16, 2015

    Oct 16, 2015
    7,276
    10,375
    1,988
    Male
    Software developer
    South West of England
    You could try calling disconnect() before openConnection()
     
    ac4android likes this.
  3. ac4android

    ac4android Well-Known Member
    Thread Starter
    Rank:
    None
    Points:
    53
    Posts:
    198
    Joined:
    Nov 3, 2015

    Nov 3, 2015
    198
    29
    53
    Hi and thx again.

    Yeah, That's what I am going to do tomorrow, but Java is supposed to close the connection when it hits reader.close(), it never got to that point.

    Still curious how the connection was in an "open" state because nothing in the code opens the connection prior to that point, unless the handler-thread and the AsyncTask were still alive after completing the last AsyncTask task.

    I thought after an AsyncTask completes it job, it dies and releases memory, and not hang around like a background thread
     
    #3 ac4android, Aug 2, 2016
    Last edited: Aug 2, 2016

Share This Page

Loading...