In my android app, I am trying to pull data from server by doing a POST request.
I'm using HttpURLConnection class to make the requests as Apache's HttpClient is no longer maintained by android.
Here's what I'm doing.
private boolean callWS() {
try {
// To avoid the bug in httpurlconnection prior froyo which
// causes the getInputStream to return headers along with response
if (Build.VERSION.SDK_INT < 8)
System.setProperty("http.keepAlive", "false");
mHttpResponseCode = 0;
mErrorMessage = "";
// Initialize connection
URL connectURL = new URL(mServerUrl);
HttpURLConnection conn = (HttpURLConnection) connectURL.openConnection();
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setUseCaches(false);
conn.setInstanceFollowRedirects(true);
conn.setReadTimeout(30000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
// Set some headers
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestProperty("Accept-Encoding", "deflate, gzip");
connection.setRequestProperty("Content-Length", mParameters.length() + "");
// Connect to host
conn.connect();
// Write parameters to connection
OutputStreamWriter writer = new OutputStreamWriter(conn.getOutputStream());
writer.write(mParameters);
writer.flush();
writer.close();
// Wait for http response code
mHttpResponseCode = conn.getResponseCode();
// Read response from connection
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
ByteArrayBuffer baf = new ByteArrayBuffer(50);
int read = 0;
int bufSize = 1024;
byte[] buffer = new byte[bufSize];
while (true) {
read = bis.read(buffer);
if (read == -1)
break;
baf.append(buffer, 0, read);
}
// Decompress gzipped response
if (conn.getHeaderField("Content-Encoding") != null && conn.getHeaderField("Content-Encoding").contains("gzip"))
mResponseString = decompress(baf.toByteArray());
else
mResponseString = new String(baf.toByteArray());
mResponse.setResponse(mResponseString);
isWSCallSuccessfull = true;
} catch(UnknownHostException unknownHostException) {
isWSCallSuccessfull = false;
mErrorMessage = "Unknown host exception";
unknownHostException.printStackTrace();
mLogger.putStacktrace(unknownHostException);
} catch(SocketException socketException) {
isWSCallSuccessfull = false;
mErrorMessage = "Socket Exception";
socketException.printStackTrace();
mLogger.putStacktrace(socketException);
} catch(SocketTimeoutException socketTimeOutException) {
isWSCallSuccessfull = false;
mErrorMessage = "Socket Timeout Exception";
socketTimeOutException.printStackTrace();
mLogger.putStacktrace(socketTimeOutException);
} catch(SSLException sslException) {
isWSCallSuccessfull = false;
mErrorMessage = "SSL Exception";
sslException.printStackTrace();
mLogger.putStacktrace(sslException);
} catch(IOException ioException) {
isWSCallSuccessfull = false;
mErrorMessage = "IO Exception " + ioException.getMessage();
ioException.printStackTrace();
mLogger.putStacktrace(ioException);
}
mResponse.setHttpResponseCode(mHttpResponseCode);
mResponse.setErrorMessage(mErrorMessage);
mResponse.isWSCallSuccessful(isWSCallSuccessfull);
return isWSCallSuccessfull;
}
This works fine on every device except devices running 2.2 (did not try it on 2.1).
In 2.2, it works fine. But if I leave this part of code idle for more than 30s, it returns me with -1 as http response code the very next time.
Another thing to note is that this happens only with HTTPS urls and not with HTTP Urls. I do not want to use HttpsURLConnection class because at times I may want to use http as well.
I'm not closing the connection just to keep the connection alive. What am I doing wrong?
If you want to use Https and Http at the same time and does not want to create a seperate connection -and if HttpsUrlConnection solves your "-1 issue" you can use the following approach:
URLConnection conn = new URL(url).openConnection();
if (conn instanceof HttpsURLConnection) {
// do stuff with cast to HttpsUrlConection
}
else {
// do stuff with cast to HttpUrlConnection
}
I took this answer as a reference
Related
I am kind of new in android development and I am having a weird issue.
The following code is supposed to work:
URL url = new URL("http://127.0.0.1/test.html");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
int code = connection.getResponseCode();
System.out.println("code: "+code);
The problem is, after I do the connection.connect(); nothing happens, even if I add a textX.setText() after the connect, I am not able to do any action.
Any idea what might be the issue?
This is my whole method, all I am trying to do is get some text from the API, which says "OK" actually, but I am not able to make it work.
public void conn (View view)
{
TextView text2 = (TextView)findViewById(R.id.text2);
text2.setText("connecting...");
String output="";
//All working until here
URL url;
HttpURLConnection urlConnection = null;
try {
output="about to connect";
text2.setText(output);
url = new URL("http://127.0.0.1:4444/localweb/api/api.php");
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("GET");
connection.connect();
InputStream in = urlConnection.getInputStream();
text2.append("\nabout to get code");
int code = connection.getResponseCode();
text2.setText(Integer.toString(code));
//urlConnection = (HttpURLConnection) url.openConnection();
//urlConnection.connect();
//output=urlConnection.getResponseMessage();
//text2.setText(output);
InputStreamReader isw = new InputStreamReader(in);
int data = isw.read();
while (data != -1) {
char current = (char) data;
data = isw.read();
System.out.print(current);
output=output+current;
}
//text2.setText(output);
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null) {
urlConnection.disconnect();
}
}
}
There wasn't something 'wrong' with the http client code, the issue is that you cannot launch the httpurlclient from the parent thread as I was trying to do, it must be executed in the background through an AsyncTask, after moving all the httpurlconnection stuff into an additional async function now I am able to get all the web details I needed.
I am trying to hit a web service. it is working fine with android 4.4 or android 5.X. but when i am trying to hit "http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID" using android 4.1.1 it always returning me 307 status code. but this url is working fine with android 4.4 or 5.x. i also tried to hit other url it is working fine on android 4.1.1.
so please tell me what is the problem
Log.i(TAG, url);
String response = null;
HttpURLConnection conn = null;
try {
URL webServiceUrl = new URL(url);
conn = (HttpURLConnection) webServiceUrl
.openConnection();
Log.i(TAG, "Connection open");
conn.setRequestMethod(GET);
conn.setConnectTimeout(CONNECTION_TIME_OUT);
conn.setRequestProperty(CONTENT_TYPE, contentType);
conn.setRequestProperty(ACCEPT_TYPE, acceptType);
conn.setDoInput(true);
conn.connect();
Log.i(TAG, "Connection Connected");
if (conn.getResponseCode() == HttpURLConnection.HTTP_OK && conn.getInputStream() != null) {
response = StreamUtility.convertStreamToString(conn.getInputStream());
conn.getInputStream().close();
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (conn != null) {
conn.disconnect();
}
}
return response;
}
Replace your URL address with http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID/ (pay attention to / at the end). The response code will be 200.
UPDATE:
With your current URL address (http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID) without / at the end, you can use the following code:
String address = "http://inmotion-prod.cloudapp.net:145/service1.svc/json/GetCustomerUUID";
URL url = new URL(address);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setInstanceFollowRedirects(false);
// if print out (debug or logging), you will see secondURL has / at the end
URL secondURL = new URL(urlConnection.getHeaderField("Location"));
HttpURLConnection urlConnection1 = (HttpURLConnection) secondURL.openConnection();
Then use urlConnection1.getResponseCode()
Hope it helps!
BNK's answer helped, I fixed it like this so that it also works on newer devices (location header was returned as null / empty!)
String headerLocation = httpsUrlConnection.getHeaderField("Location");
logger.debug("Location header: " + headerLocation);
// if the redirect URL ends with a "/" sign, but the original URL does not, it's probably the redirect bug
String originalURL = url.toString();
if (!TextUtils.isEmpty(headerLocation) && headerLocation.endsWith("/") && !originalURL.endsWith("/"))
{
logger.info("Redirect Location differs from original URL, create new connection to: " + headerLocation);
httpsUrlConnection = (HttpsURLConnection) new URL(headerLocation).openConnection();
// optional
httpsUrlConnection.setSSLSocketFactory(sslSocketFactory);
}
I'm trying to create an android application which depends on JSON responses. Sometimes it takes a lot of time for the server to respond and ends in a time out exception. Therefore I would like to add a restriction like my webservice call should abort after 20seconds if there is no response. Can you please help me achieving this idea.
Thanks in Advance.
You're not giving much details on the actual implementation that you have.
However, messing with the timeout seems like it may be an emergency fix to an underlying problem that should be fixed.
However, using websockets for transport could be a possible (and probably more elegant) solution. They provide a persistent connection between client and server once created.
Using websockets on Android and IOS
There are several ways to achieve the goal.
We can using HttpURLConnection to do the http request.
public String doPost() {
if (!mIsNetworkAvailable) {
return null;
}
try {
URL url = new URL(mURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setDoOutput(true);
conn.setDoInput(true);
for (String key : mHeadersMap.keySet()) {
conn.setRequestProperty(key, mHeadersMap.get(key));
}
conn.setRequestProperty("User-Agent", "Android");
conn.setConnectTimeout(30000);
conn.setReadTimeout(30000);
conn.setRequestProperty("Content-Type", "application/json");
conn.getOutputStream().write(mContent);
conn.getOutputStream().flush();
int rspCode = conn.getResponseCode();
if (rspCode >= 400) {
return null;
}
byte[] buffer = new byte[8 * 1024];
BufferedInputStream bis = new BufferedInputStream(conn.getInputStream());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
int len;
while ((len = bis.read(buffer)) > 0) {
baos.write(buffer, 0, len);
}
baos.flush();
final String result = new String(baos.toByteArray());
baos.close();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
setConnectTimeout :Sets the maximum time in milliseconds to wait while connecting.
setReadTimeout:Sets the maximum time to wait for an input stream read to complete before giving up.
Reference: http://developer.android.com/reference/java/net/URLConnection.html
I am trying to create android application using HttpURLConnection API. I successfully logging and now trying to fire another query say (http://someserver.com/myapp/getemployeebyid?userid.) On execution of this statement I am getting following exception.
java.net.protocolexception: connection already established
I have closed input stream.
My code is.
mymethod(urlString){
InputStream myis = null;
URL url = new URL(urlstring);
HttpURLConnection httpURLconnection = (HttpURLConnection)
url.openConnection();
httpURLconnection.setReadTimeout(10000);
httpURLconnection.setConnectTimeout(15000);
httpURLconnection.setRequestMethod("GET");
httpURLconnection.setDoInput(true);
httpURLconnection.connect();
int myresponse = httpURLconnection.getResponseCode();
if (myresponse == 200) {
ResponseFlag = true;
myis = httpURLconnection.getInputStream();
// Convert the InputStream into a string
contentAsString = convertIttostring(myis);
}
else {
ResponseFlag = false;
somemesg = "incorrect response ";
}
}
catch(Exception Ex){
System.out.println(Ex.toString());
somemesg="connection failed";
}
finally {
if (myis != null) {
myis.close();
}
}
}
Noow I have 2 queries.
Do i need to write connection.disconnect(); below myis.close(), seperately?
After login success I am trying to reconnect. Cant I connect once and fired the queries. Once all the operation is done close the connection?
Im currently working on an Android app with heavy server side communication. Yesterday I got a bug report saying that the users aren't able to send (simple) special characters such as ëäï.
I searched but didn't find anything helpful
Possible duplicate ( without answer ):
https://stackoverflow.com/questions/12388974/android-httpurlconnection-post-special-charactes-to-rest-clint-in-android
My relevant code:
public void execute(String method) {
HttpsURLConnection urlConnection = null;
try {
URL url = new URL(this.url);
urlConnection = (HttpsURLConnection) url.openConnection();
urlConnection.setRequestMethod(method);
urlConnection.setReadTimeout(30 * 1000);
urlConnection.setDoInput(true);
if (secure)
urlConnection.setRequestProperty("Authorization", "Basic " + getCredentials());
if (body != null) {
urlConnection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
urlConnection.setFixedLengthStreamingMode(body.length());
urlConnection.setDoOutput(true);
DataOutputStream dos = new DataOutputStream(urlConnection.getOutputStream());
dos.writeBytes(body);
dos.flush();
dos.close();
}
responseCode = urlConnection.getResponseCode();
message = urlConnection.getResponseMessage();
InputStream in = null;
try {
in = new BufferedInputStream(urlConnection.getInputStream(), 2048);
} catch (Exception e) {
in = new BufferedInputStream(urlConnection.getErrorStream(), 2048);
}
if (in != null)
response = convertStreamToString(in);
} catch (UnknownHostException no_con) {
responseCode = 101;
}catch (ConnectException no_con_2){
responseCode = 101;
}catch(IOException io_ex){
if(io_ex.getMessage().contains("No authentication challenges found")){
responseCode = 401;
}else
responseCode = 101;
} catch (Exception e) {
e.printStackTrace();
} finally {
if (urlConnection != null)
urlConnection.disconnect();
}
}
body is a String ;-)
Hope we can solve this together
UPDATE:
Tried:
writeUTF()
need a server capable of understanding the modified UTF-8
byte[] buf = body.getBytes("UTF-8");
dos.write(buf, 0, buf.length);
strings work but no special chars
update: Got it working with StringEntity(* string, "UTF-8") then parse the result to a byte[] and write it with dos.write(byte[])!
--
Setting the encoding of the StringEntity did the trick for me:
StringEntity entity = new StringEntity(body, "UTF-8");
seen here: https://stackoverflow.com/a/5819465/570168
i am not totally sure buy try this utility for your case
URLEncoder.encode(string, "UTF-8")
I faced this problem in android while passing a json with special char (ñ).
In my WebApi method, [FromBody] param is giving null, it seems it can't parse the json.
I got it working by getting bytes as UTF-8 then writing it in DataOutputStream (Client-side fix).
byte[] b = jsonString.getBytes("UTF-8");
os.write(b, 0, b.length);