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:
It threw a java.lang.illegalStateException: Already connected.
How could it be already connected?????? Doesn't make sense.
This time, I called the method inside my Handler, like this:
Code:
final Handler handler = new Handler();
handler.post(new Runnable() {
@Override
public void run() {
//TODO
String slat = String.valueOf(dd);
sysmsgview.setText(slat);
latitudeview.setText(latitude);
longitudeview.setText(longitude);
accuracyview.setText(accuracy);
if (running) {
dd++;
// hardvoded for testing
latitude = "84.012345";
longitude = "-114.2345";
if (latitude != null && longitude != null){
Toast.makeText(getApplicationContext(),
"Uploading "+ dd + " Latitude:Longitude ->" + latitude +":" + longitude,
Toast.LENGTH_LONG).show();
new UploadActivity(getApplicationContext()).execute( latitude, longitude);
}
}
handler.postDelayed(this, 10000);
}
});
}
It threw a java.lang.illegalStateException: Already connected.
How could it be already connected?????? Doesn't make sense.
Code:
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
if (!(conn instanceof HttpURLConnection)) {
throw new IOException("ERROR: urlConn not instance of HttpUrlConnection.");
}
conn.setRequestMethod("POST");
conn.connect();
conn.setDoOutput(true);
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream()); // <-- THROWS EXCEPTION HERE!
writer.write(data);
writer.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line = null;
// Read Server Response
while ((line = reader.readLine()) != null) {
stringBuilder.append(line);
break;
}
reader.close();
return stringBuilder.toString();
} catch (Exception e) {
return new String("Exception: " + e.getMessage());
}