I am working on an application that uses HttpUrlConnection, connects beautifully with the server, but when fetching data from it says connection time out IOException.
The internet, & network permissions are already set in the android.manifest; there are no bars showing up in the android emulator (does this says anything).
Read at developer.android.com:
The functional limitations of the emulator include:
– No support for determining network connected state
– and few others….
Any help will be highly appreciated. And I don’t have an actual device to test this.
Received server info do gets printed in the logcat.
Thanks…
Here is the code:
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
try {
Log.i(INFO_TAG, "Received server:" + conn.toString());
conn.setInstanceFollowRedirects(true);
conn.setRequestMethod("GET");
conn.setRequestProperty("Content-length", "0");
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setConnectTimeout(25000 /* milliseconds */);
conn.setReadTimeout(15000/* 10000 *//* milliseconds */);
// conn.setDoInput(true);
// Starts the query
conn.connect();
status = conn.getResponseCode();
Log.d(DEBUG_TAG, "Status recevied is: " + conn.getResponseCode());
responseCode = status;
if (responseCode == 200) {
Log.i(INFO_TAG, "URL Connection OK");
contentIs = conn.getInputStream();
BufferedReader reader = new BufferedReader(
new InputStreamReader(contentIs));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line);
Log.i(INFO_TAG, "Data Read is: " + line);
}
} else {
Log.d(DEBUG_TAG, "Could not read data from web server");
Log.e(ConnectAndGetData.class.toString(),
"Failed to download content");
}
}// end of try
catch (Exception ex) {
// This is currently being printout
Log.d(DEBUG_TAG, "Received an exception" + ex.toString(), ex);
ex.printStackTrace();
throw new IOException("Error Connecting" + ex.toString());// "Error connecting");
} finally {
if (contentIs != null) {
contentIs.close();
conn.disconnect();
}
}
One more thing since, it throws a connection timeout exception it does not debug info at this line is printed:
Log.d(DEBUG_TAG, “Status recevied is: ” + conn.getResponseCode());
The problem was not the code above, it was the server code (the main culprit). Now implemented a web service and the code above is working fine.
Thanks all for commenting…