Does HttpURLConnection work for sdk22? - android

Does HttpURLConnection that works for android API 23 also work for android API22?
I'm trying to program for android version over 4.0.
I am unsure if the difference between sdk22 and sdk23 will cause a problem
// http client
URL url = new URL(url1);
HttpURLConnection httpClient = (HttpURLConnection) url.openConnection();
httpClient.setRequestMethod("POST");
httpClient.setUseCaches(false);
httpClient.setDoInput(true);
httpClient.setDoOutput(true);
httpClient.setRequestProperty("Connection", "Keep-Alive");
httpClient.addRequestProperty("Content-length", reqEntity.getContentLength() + "");
OutputStream os = httpClient.getOutputStream();
reqEntity.writeTo(httpClient.getOutputStream());
os.close();
httpClient.connect();
if (httpClient.getResponseCode() == HttpURLConnection.HTTP_OK) {
return readStream(httpClient.getInputStream());

Yes it will work without any issues, and the difference will not cause any problem
HttpUrlConnection is available in from API Level 1 , you can further get more insight from the docs.
Its a good practice to use HttpUrlConnection over deprecated Apache HttpClient classes,

Related

Which one to use for WebService call- HttpURLConnection or DefaultHttpClient?

I have used only DefaultHttpClient for WebService call in my apps.
But as DefaultHttpClient is deprecated i am confused that what to use.
I want to choose the best among them for my further development.
Also suggest me if any other best way to call WebServices.
DefaultHttpClient is kind of Apache Library that still used but nowadays, HttpURLConnection borns and it's recommend by google because it's more suitable for mobile application than Apache Library. DefaultHttpClient also can be used out of android environment, but in android, it's deprecated and we should use HttpUrlConnection with many advantages: good for limit android memory, good for battery ...
It would find it not too difficult to use, below is some pieces of code that can be useful
URL url = new URL(requestUrl);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(CONNECT_TIME_OUT);
connection.setReadTimeout(READ_TIME_OUT);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setDoOutput(true);
connection.setDoInput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=" + CHARSET);
connection.setRequestProperty("Content-Length", "" + Integer.toString(urlParams.getBytes().length));
if (headers != null) addHeaderFields(connection, headers);
if (!TextUtils.isEmpty(urlParams)) {
PrintWriter writer = new PrintWriter(new OutputStreamWriter(connection.getOutputStream(), CHARSET), true);
writer.append(urlParams);
writer.flush();
writer.close();
}
StringBuilder response = new StringBuilder();
// checks server's status code first
int status = connection.getResponseCode();
if (status == HttpURLConnection.HTTP_OK) {
BufferedReader reader = new BufferedReader(new InputStreamReader(
connection.getInputStream()));
String line = null;
while ((line = reader.readLine()) != null) {
response.append(line);
}
reader.close();
connection.disconnect();
} else {
throw new ApiException(ApiException.APP_EXCEPTION, "Server returned non-OK status: " + status);
}
return response.toString();
As Android have deprecated DefaultHTTPClient Class and methods.
Android 6.0 release removes support for the Apache HTTP client. If your app is using this client and targets Android 2.3 (API level 9) or higher, use the HttpURLConnection class instead. This API is more efficient because it reduces network use through transparent compression and response caching, and minimizes power consumption.
To continue using the Apache HTTP APIs, you must first declare the following compile-time dependency in your build.gradle file:
android {
useLibrary 'org.apache.http.legacy'
}
Here is URL from where you can have more details about HttpURLConnection.
https://developer.android.com/reference/java/net/HttpURLConnection.html
DefaultHTTPClient is deprecated in android and will no longer be supported in Marshmallow (Android 6.0) and the API's after it. HTTPURLConnection is preferred over HTTPClient.
For networking operations I would recommend you to use Volley , Android's official networking related well tuned library. It eases the way network calls are made and is faster plus it's operations are asynchronous by default (you don't need to worry about custom threads for network calls).
Here is a good tutorial to use Volley : Android working with Volley Library
Android guide for Volley : Transmitting Network Data Using Volley

HttpURLConnection getInputStream() returns empty for specific device

Im experiencing a strange behaviour when using HttpURLConnection getInputStream() on a different devices. On all devices except "Alcatel" im getting a valid input stream only for Alcatel devices im getting a different(empty) kind of input stream:
All devices except alcatel
Alcatel devices
Im making the exact same call and getting 200 for all the devices.
The code is pretty straightforward:
urlObj = new URL(url);
urlConnection = (HttpURLConnection)urlObj.openConnection();
urlConnection.setRequestMethod(method);
urlConnection.setDoInput(true);
urlConnection.setConnectTimeout(timeout);
urlConnection.setReadTimeout(timeout);
urlConnection.setDoOutput(true);
urlConnection.setFixedLengthStreamingMode(body.length);
urlConnection.setRequestProperty(HEADER_KEY_CONTENT_LENGTH, String.valueOf(body.length));
urlConnection.getOutputStream().write(body);
int responseCode = urlConnection.getResponseCode();
InputStream in = urlConnection.getInputStream();
Any suggestions?
Please check if you need to have proxy setting for alcatel. httpurlconnection may fail without proper proxy gateway.

setRequestProperty throwing java.lang.IllegalStateException: Cannot set request property after connection is made

I am getting java.lang.IllegalStateException:
java.lang.IllegalStateException: Cannot set request property after connection is made error when setRequestProperty method is called after
url.openConnection();
Here is what i am trying:
URL url = new URL("https://49.205.102.182:7070/obsplatform/api/v1/mediadevices/545b801ce37e69cc");
urlConnection = (HttpsURLConnection) url
.openConnection();
urlConnection.setRequestProperty("Content-Type","application/json");
any suggestions please? Thanks in advance.
This usually happens if you have in the debug watchers calls, such as conn.getResponseCode() or anything that queries the request result before the request was actually issued or completed.
This causes, that during debug, a request is performed by the watcher, before having properly set you request, and then it becomes invalid.
I only have this issue while in debugging mode,
Run without debugging (You can print logs) everything should work fine
The obvious thing is to think that you need to add properties before calling open on the URL. this however is not the case. i have seen many samples of settings being set AFTER url has been open (as counter intuitive as that is).
the problem in my case is that i had conn.getResponseCode() added in my watch list. removed that and all good.
... tricky.
please check below code
HttpURLConnection httpcon = (HttpURLConnection) ((new URL("a url").openConnection()));
httpcon.setDoOutput(true);
httpcon.setRequestProperty("Content-Type", "application/json");
httpcon.setRequestProperty("Accept", "application/json");
httpcon.setRequestMethod("POST");
httpcon.connect();
I was getting the same exception on setRequestProperty("Range","byte=" + downloadedSize + "-") .
After adding connection.setChunkedStreamingMode(0); the issue disappeared
I'm having the same issue.
I was observing this issue on Nexus 5. Code of my app constantly fails with the same exception (or its twin brother "cannot set request method ..")
What I've observed that it happens if i leave phone for a while. One it starts failing it fails all the time - but if i restart phone/emulator it's ok once again).
My suspicion is its either some bug in connection pooling on framework side, or somewhere in code resources are leaked.
i found the problem it's about ordering the code, if you are trying to add header and post parameters both, it's important to be careful about this
HttpURLConnection connection = (HttpURLConnection) urlConnection;
//// Add Request Headers
for (NameValuePair nvp :
request[0].getHeaderParams()) {
connection.setRequestProperty(nvp.getName(),nvp.getValue());
}
// done
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
//// Add Post Parameters
OutputStream outputStream = urlConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream));
List<NameValuePair> params = new ArrayList<>(request[0].getPostParams());
bufferedWriter.write(getQuery(params));
// done
connection.setConnectTimeout(3000);
connection.setReadTimeout(3000);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.flush();
outputStream.close();
connection.connect();
in here, i have added header parameters then set setDoInput and setDoOutput then setRequestMethod and finally you can add POST parameters.
i don't know what is wrong with setRequestMethod but i think its preparing the connection by opening it or something and that's why it throws exception
not invoke setRequestProperty after write byte to OutputStream.
OutputStream os = connection.getOutputStream();
os.write("k=v".getBytes());
os.close();
you should invoke setRequestProperty above the code
To avoid the error:
java.lang.IllegalStateException: Cannot set request property after connection is made
We have to check the connection response before access the request header fields :
URL url = new URL("https://49.205.102.182:7070/obsplatform/api/v1/mediadevices/545b801ce37e69cc");
urlConnection = (HttpsURLConnection) url
.openConnection();
//Check connection
if(urlConnection.getResponseCode() == 200/*Successful*/) {
urlConnection.setRequestProperty("Content-Type","application/json");
...
...
}

Android HTC Sense - Http Request/ Response exception

Working on the emulator, but when I am trying on an HTC Sense, i have got an exception on the last line of this code
url = new URL(urlString);
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json; charset=utf-8");
//Send request
Gson requestGson = new Gson();
OutputStream wr = connection.getOutputStream();
The exception is java.net.ProtocolException: Does not support output for the connection.getOutputStream();
I am testing on API 10, Gingerbread on an HTC Sense (remote debugging).
anyone has encountered this problem before?
Thanks.
David.
Try calling setDoOutput(true). It is needed for POST requests. The first part of your code would change to look like this:
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/json; charset=utf-8");
connection.setDoOutput(true);
Source: https://groups.google.com/forum/?fromgroups=#!topic/android-developers/2aEYpsZEMvs
See also this other StackOverflow answer: What exactly does URLConnection.setDoOutput() affect?

FileNotFoundException for HttpURLConnection in Ice Cream Sandwich

I have an Android app that works fine with Android 2.x and 3.x, but it fails when run on Android 4.x.
The problem is in this section of code:
URL url = new URL("http://blahblah.blah/somedata.xml");
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestMethod("GET");
urlConnection.setDoOutput(true);
urlConnection.connect();
InputStream inputStream = urlConnection.getInputStream();
When the application is running on Android 4.x, the getInputStream() call results in a FileNotFoundException. When the same binary is running on earlier versions of Android, it succeeds. The URLs also work fine in web browsers and with curl.
Apparently something about HttpURLConnection has changed in ICS. Does anybody have any idea what has changed, and/or what the fix might be?
Try removing the setDoOutput call. Taken from this blog:
a blog
Edit: This is needed when using a POST call.
A FileNotFoundException may also been thrown if the server returns a bad error code (e.g., 400 or 401). You can handle this as follows:
int responseCode = con.getResponseCode(); //can call this instead of con.connect()
if (responseCode >= 400 && responseCode <= 499) {
throw new Exception("Bad authentication status: " + responseCode); //provide a more meaningful exception message
}
else {
InputStream in = con.getInputStream();
//etc...
}
I Don't know why, but dealing manually with redirection resolves the problem.
connection.setInstanceFollowRedirects(false);
A little late but you can also verify the accepted content. You can add this line to accept all kinds of contents
urlConnection.setRequestProperty("Accept","*/*");

Categories

Resources