Android httpUrlConnection connecting to server but not internet - android

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

You need to write
conn.setDoInput( true ); after this line
conn.setConnectTimeout(25000 /* milliseconds */);
conn.setReadTimeout(15000/*10000*/ /* milliseconds */);
Its Sets the flag indicating whether this URLConnection allows input.

The emulator can't receive any response from server. I think the problem with your network connection. Check your proxy settings.

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...

Related

Android keep trying POST until it goes through

I have a POST message I absolutely have to send on Android in a given circumstance, to the point I would like it to keep trying until it completes. I was under the understanding that setting:
urlConnection.setConnectTimeout(0);
would keep trying the connection until it goes through, but what is actually happening is the try block is failing, and the UnknownHostException is being thrown instead:
private class SendAlert extends AsyncTask<String, String, String> {
protected String doInBackground(String... strings) {
Log.d(TAG, "sendAlarm: sending alarm");
String stringUrl = createUri();
HttpsURLConnection urlConnection = null;
BufferedReader reader = null;
String postData = "";
Log.d(TAG, "sendAlarm: apikey: " + apiKey);
try{
Log.d(TAG, "sendAlarm: trying");
URL finalURL = new URL(stringUrl);
urlConnection = (HttpsURLConnection)finalURL.openConnection();
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(0);
urlConnection.setRequestProperty("Content-Type", "application/json;charset=UTF-8");
urlConnection.setRequestProperty("Accept","application/json");
urlConnection.setRequestProperty("x-api-key",apiKey);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(true);
int responseCode = urlConnection.getResponseCode();
Log.d(TAG, "doInBackground: response code = " + responseCode);
}catch (MalformedURLException e) {
e.printStackTrace();
Log.d(TAG, "doInBackground: error 1 " + e.toString());
}catch(UnknownHostException e){
Log.d(TAG, "doInBackground: e: " + e);
Log.d(TAG, "doInBackground: retrying");
}
catch(Exception e){
Log.d(TAG, "doInBackground: error 2 " + e.toString());
}
Wondering what the best way to set up the post message on Android is, to keep trying the connection until it goes through, even if the phone is on airplane mode for 5 hours.
Edit: going of #user3252344's answer below, is there any problem with calling the function again directly in the catch block of the AyncTask:
catch(UnknownHostException e){
Log.d(TAG, "doInBackground: e: " + e);
Log.d(TAG, "doInBackground: retrying");
SendAlarm sendAlarm = new SendAlarm;
sendAlarm.execute();
}
Setting the connection timeout to 0 will mean it won't timeout, but if it fails to connect it won't handle it still. I'm guessing you get a an UnknownHostException because it fails to resolve the url since it can't reach a DNS server.
I'd suggest you just set a reasonable connection timeout, catch the timeout exception if it happens and re-run.
final int READ_TIMEOUT = 500; // Timeout
final int RETRY_MS = 2000; //Retry every 2 seconds
final Handler handler = new Handler();
Runnable myUrlCall = () -> {
try {
//Make things
urlConnect.setReadTimeout(READ_TIMEOUT);
//Make the URL call, do response
} catch (SocketTimeoutException e) {
handler.postDelayed(myUrlCall, RETRY_MS);
} catch (/* other unintended errors*/ e) {
//Log the error or alert the user
}
};
handler.post(myUrlCall);
Possible even better idea: use Android settings to check if there's internet before you make the call. If there's no internet, use a longer delay. Something like this would be the code you're looking for.

Android to Django - always showing 401

I am trying to connect to my Django backend server from my app. While in local/dev with http connection the android app is getting connected to the server, it is rreturning HTT 401 error for all API calls via the app (except the login call). However, funny thing is using Postman, I'm being able to reach the prod server.
Following is one of the code snippets (android):
try{
URL targetUrl = new URL(targetURL);
httpConnection = (HttpURLConnection) targetUrl.openConnection();
httpConnection.setRequestMethod("GET");
httpConnection.setRequestProperty("Authorization", "jwt " + mToken);
httpConnection.setConnectTimeout(10000); //10secs
httpConnection.connect();
Log.i(TAG, "response code:" + httpConnection.getResponseCode());
if (httpConnection.getResponseCode() != 200){
Log.e(TAG, "Failed : HTTP error code : " + httpConnection.getResponseCode());
return Constants.Status.ERR_INVALID;
}
//Received Response
InputStream is = httpConnection.getInputStream();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
String line;
while((line = rd.readLine()) != null) {
response.append(line);
//response.append('\r');
}
rd.close();
Log.i(TAG, response.toString());
// Save the tenant details
return parseTenantInfo(response.toString());
}catch (MalformedURLException e) {
e.printStackTrace();
return Constants.Status.ERR_NETWORK;
} catch (SocketTimeoutException e) {
e.printStackTrace();
return Constants.Status.ERR_NETWORK;
}
catch (IOException e) {
e.printStackTrace();
return Constants.Status.ERR_UNKNOWN;
}finally {
if(httpConnection != null) {
httpConnection.disconnect();
}
}
Following is the target url:
private static final String targetURL = Constants.SERVER_ADDR + APIs.tenant_get;
Here, SERVER_ADDR is https://www.example.com/ and tenant_get is apitogettenantinfo/
I am always getting 401 error. Kindly help me out!!! Thanks.
The most irritating thing is Postman works, android login works. So it is seemingly no issue with server (else how would postman work?). And I can't understand what the android issue is.
EDIT:
Following is the screenshot of my postman. A few things are blacked out for security & privacy:
http://imageshack.com/a/img923/231/wUrOuS.png
401 indicates unauthorised request, make sure you are sending correct token.
Also remove httpConnection.connect();

HttpURLConnection response missing Status Line (ProtocolException)

I currently am trying to make a request (With HttpURLConnection) to an IIS server that will bounce me with redirects until the target destination.
The issue : java.net.ProtocolException: Unexpected status line: <html><head><title>Object moved</title></head><body>
This happens when urlConnection.getInputStream() is called (or urlConnection.getResponseCode(), which calls it).
I have seen numerous cases where the issue is simply a misconfigured server, but in this case, making a request from Chrome while faking an Android user agent yields the expected result.
Furthermore, the server is an ancient one, that has never been the source of problem, and is configured in the same way as one used in a production environnement.
Reaching another redirecting webpage works fine (Tested with https://com.google/, 2 redirections), and reaching a non-redirecting webpage on the server poses no problem.
I have been looking for two days, and have made no progress. ANY help would be appreciated.
The code that is currently used :
StringBuilder builder = new StringBuilder();
HttpsURLConnection urlConnection = null;
try {
URL url = new URL(urlStrings[0]);
urlConnection = (HttpsURLConnection) url.openConnection();
LogHelper.Log(getBaseContext(), "Request on URL :\n\"" + url + "\"");
urlConnection.setRequestMethod("GET");
urlConnection.setReadTimeout(10000);
urlConnection.setConnectTimeout(15000);
urlConnection.setRequestProperty("Accept","*/*");
urlConnection.setDoOutput(false);
urlConnection.setDoInput(true);
urlConnection.setInstanceFollowRedirects(true);
urlConnection.setUseCaches(false);
CookieManager cookieManager = new CookieManager();
cookieManager.setCookiePolicy(CookiePolicy.ACCEPT_ALL);
CookieHandler.setDefault(cookieManager);
urlConnection.connect();
int status = urlConnection.getResponseCode();
LogHelper.Log(getBaseContext(), "Status code is \"" + status + "\"");
if (!url.equals(urlConnection.getURL())) {
LogHelper.Log(getBaseContext(), "URL after redirection is :\n\"" + urlConnection.getURL() + "\"");
}
switch (status/100) {
case 1:
case 2:
case 3:
InputStream stream = urlConnection.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
String line;
while ((line = reader.readLine()) != null) {
builder.append(line).append("\n");
}
reader.close();
break;
case 4:
case 5:
InputStream errorStream = urlConnection.getErrorStream();
BufferedReader errorReader = new BufferedReader(new InputStreamReader(errorStream));
String errorLine;
while ((errorLine = errorReader.readLine()) != null) {
builder.append(errorLine).append("\n");
}
errorReader.close();
throw new Exception(builder.toString());
default:
throw new Exception("Unknown status code: " + status);
}
} catch (Exception e) {
LogHelper.Log(getBaseContext(), e);
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
return builder.toString();
The resulting error :
java.net.ProtocolException: Unexpected status line: <html><head><title>Object moved</title></head><body>
at com.android.okhttp.internal.http.StatusLine.parse(StatusLine.java:54)
at com.android.okhttp.internal.http.HttpConnection.readResponse(HttpConnection.java:239)
at com.android.okhttp.internal.http.HttpTransport.readResponseHeaders(HttpTransport.java:104)
at com.android.okhttp.internal.http.HttpEngine.readNetworkResponse(HttpEngine.java:1120)
at com.android.okhttp.internal.http.HttpEngine.readResponse(HttpEngine.java:951)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.execute(HttpURLConnectionImpl.java:482)
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(
at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponseCode(
at com.android.okhttp.internal.huc.DelegatingHttpsURLConnection.getResponseCode(
at com.android.okhttp.internal.huc.HttpsURLConnectionImpl.getResponseCode(
at com.pushmanager.clientmdm.activities.RegistrationView$1$2.doInBackground(
at com.pushmanager.clientmdm.activities.RegistrationView$1$2.doInBackground(
at android.os.AsyncTask$2.call(AsyncTask.java:295)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
at java.lang.Thread.run(Thread.java:818)
Turns out, this behavior was caused because of a \n character in the URL string.
I have no idea how okhttp managed this result, but with the character removed, the problem disappeared.

URLConnection - Cannot write request body after response has been read [duplicate]

I'm writing a program that connects to a servlet thanks to a HttpURLConnection but I stuck while checking the url
public void connect (String method) throws Exception {
server = (HttpURLConnection) url.openConnection ();
server.setDoInput (true);
server.setDoOutput (true);
server.setUseCaches (false);
server.setRequestMethod (method);
server.setRequestProperty ("Content-Type", "application / xml");
server.connect ();
/*if (server.getResponseCode () == 200)
{
System.out.println ("Connection OK at the url:" + url);
System.out.println ("------------------------------------------- ------- ");
}
else
System.out.println ("Connection failed");
}*/
I got the error :
java.net.ProtocolException: Cannot write output after reading input.
if i check the url with the code in comments but it work perfectly without it
unfortunately, I need to check the url so i think the problem comes from the getResponseCode method but i don t know how to resolve it
Thank you very much
The HTTP protocol is based on a request-response pattern: you send your request first and the server responds. Once the server responded, you can't send any more content, it wouldn't make sense. (How could the server give you a response code before it knows what is it you're trying to send?)
So when you call server.getResponseCode(), you effectively tell the server that your request has finished and it can process it. If you want to send more data, you have to start a new request.
Looking at your code you want to check whether the connection itself was successful, but there's no need for that: if the connection isn't successful, an Exception is thrown by server.connect(). But the outcome of a connection attempt isn't the same as the HTTP response code, which always comes after the server processed all your input.
I think the exception is not due toprinting url. There should some piece of code which is trying to write to set the request body after the response is read.
This exception will occur if you are trying to get HttpURLConnection.getOutputStream() after obtaining HttpURLConnection.getInputStream()
Here is the implentation of sun.net.www.protocol.http.HttpURLConnection.getOutputStream:
public synchronized OutputStream getOutputStream() throws IOException {
try {
if (!doOutput) {
throw new ProtocolException("cannot write to a URLConnection"
+ " if doOutput=false - call setDoOutput(true)");
}
if (method.equals("GET")) {
method = "POST"; // Backward compatibility
}
if (!"POST".equals(method) && !"PUT".equals(method) &&
"http".equals(url.getProtocol())) {
throw new ProtocolException("HTTP method " + method +
" doesn't support output");
}
// if there's already an input stream open, throw an exception
if (inputStream != null) {
throw new ProtocolException("Cannot write output after reading
input.");
}
if (!checkReuseConnection())
connect();
/* REMIND: This exists to fix the HttpsURLConnection subclass.
* Hotjava needs to run on JDK.FCS. Do proper fix in subclass
* for . and remove this.
*/
if (streaming() && strOutputStream == null) {
writeRequests();
}
ps = (PrintStream)http.getOutputStream();
if (streaming()) {
if (strOutputStream == null) {
if (fixedContentLength != -) {
strOutputStream =
new StreamingOutputStream (ps, fixedContentLength);
} else if (chunkLength != -) {
strOutputStream = new StreamingOutputStream(
new ChunkedOutputStream (ps, chunkLength), -);
}
}
return strOutputStream;
} else {
if (poster == null) {
poster = new PosterOutputStream();
}
return poster;
}
} catch (RuntimeException e) {
disconnectInternal();
throw e;
} catch (IOException e) {
disconnectInternal();
throw e;
}
}
I have this problem too, what surprises me is that the error is caused by my added code System.out.println(conn.getHeaderFields());
Below is my code:
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
conn.setRequestMethod("POST");
configureConnection(conn);
//System.out.println(conn.getHeaderFields()); //if i comment this code,everything is ok, if not the 'Cannot write output after reading input' error happens
conn.connect();
OutputStream os = conn.getOutputStream();
os.write(paramsContent.getBytes());
os.flush();
os.close();
I had the same problem.
The solution for the problem is that you need to use the sequence
openConnection -> getOutputStream -> write -> getInputStream -> read
That means..:
public String sendReceive(String url, String toSend) {
URL url = new URL(url);
URLConnection conn = url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.sets...
OutputStreamWriter out = new OutputStreamWriter(conn.getOutputStream());
out.write(toSend);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
String receive = "";
do {
String line = in.readLine();
if (line == null)
break;
receive += line;
} while (true);
in.close();
return receive;
}
String results1 = sendReceive("site.com/update.php", params1);
String results2 = sendReceive("site.com/update.php", params2);
...

POSTing JSON and retrieving a response in Android

I am trying to post a JSON message to a site and to retrieve a JSON message back.
java.net.ProtocolException: method does not support a request body: POST
Does anyone know what is wrong? Thanks in advance
HttpURLConnection conn=null;
try{
URL url=new URL(urlString);
String userPassword = userName +":" + passWord;
byte[] bytes=Base64.encode(userPassword.getBytes(),Base64.DEFAULT);
String stringEncoding = new String(bytes, "UTF-8");
conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("POST");
conn.setRequestProperty ("Authorization", "Basic " + stringEncoding);
conn.setRequestProperty("Content-Type", "application/json;charset=utf-8");
Log.i("Net", "length="+conn.getContentLength());
Log.i("Net", "contentType="+conn.getContentType());
Log.i("Net", "content="+conn.getContent());
conn.connect();
}catch(Exception e){
Log.d("Url Formation Connection", e.toString());
}
//output{
try{
String requestString="{“ ";
wr = new OutputStreamWriter(conn.getOutputStream());
wr.write(requestString.toString());
wr.flush();
//input{
BufferedReader rd = null;
String response=" ";
is = conn.getInputStream();
rd = new BufferedReader(new InputStreamReader(is));
String line;
StringBuffer responseBuff = new StringBuffer();
while ((line = rd.readLine()) != null) {
// Process line...
responseBuff.append(line);
}
response = responseBuff.toString();
Log.d("response", response);
}catch(Exception e){
Log.d("buffer error", e.toString());
}finally {
if (is != null) {
try {
wr.close();
is.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
It could be that the server you're connecting to doesn't allow POST operations. I would try a GET request first, to see if you have permissions for that web service method.
Also, you could try your luck with a simpler HttpClient, although I haven't tested this solution myself: http://www.geekmind.net/2009/11/android-simple-httpclient-to.html
Just a guess but can you try setting:
conn.setDoOutput(false);
The documentation says: "Optionally upload a request body. Instances must be configured with setDoOutput(true) if they include a request body." HttpURLConnection
Since you do not have anything in your body, might as well set it to false.
The documentation Android has for setRequestMethod is minimal, however your error states that POST is not a valid method. Try using PUT instead:
conn.setRequestMethod("PUT");
Also see this post for any tweaks you may need to make.

Categories

Resources