Why I can't use StringEntity when I use Android API 23 - android

client.post(AppConfig.getApplicationContext(),
HttpUrls.getUrl()
+ urlroot, new StringEntity(jsonData, "UTF-8"),
"application/json", handler);
I use StringEntity as HttpEntity when I post a request by XHttpClient in Android API 21, but when I use Android API 23, I cannot use it again. I think there should be a class in API 23 which equivalent to the class StringEntity in API 21, so which class can I use?

Support for Apache HTTP Client has been removed from Android 6.0. Check Android 6.0 changes.
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'
}

Related

OkHttp(s)URLConnection class is not available in 4.9.0 version

I am having com.squareup.okhttp3:okhttp-urlconnection:3.13.1 jar as dependency in gradle file of my Android project and used as below to get the URLConnection.
protected URLConnection getStandardHTTPURLConnection(URL url) throws IOException {
return new OkHttpURLConnection(url, builder.cookieJar(new JavaNetCookieJar(CookieHandler.getDefault())).build());
}
So I updated the jar to 4.9.0 and realized that OkHttpURLConnection is not available. It is leading to noClassDefinition exception in the run time. so what are the alternatives? Is it deprecated or moved under another hood?
Below are my imports.
import okhttp3.internal.huc.OkHttpURLConnection;
import okhttp3.internal.huc.OkHttpsURLConnection;
Kindly advice.
There's a compatibility implementation you can paste in that's mentioned in the OkHttp 3.14.0 release notes.
https://square.github.io/okhttp/changelog_3x/#version-3140
From https://square.github.io/okhttp/changelog_3x/ so I guess they got removed on 4.x
The Apache HTTP client and HttpURLConnection APIs are deprecated. They
continue to work as they always have, but we’re moving everything to
the new OkHttp 3 API. The okhttp-apache and okhttp-urlconnection
modules should be only be used to accelerate a transition to OkHttp’s
request/response API. These deprecated modules will be dropped in an
upcoming OkHttp 3.x release.

EncodingUtils no longer available in recent versions of Android

It looks as if in recent versions of Android, EncodingUtils from the Apache package has been deprecated. We call EncodingUtils.getBytes(postData, "base64") a lot in our code which now needs to be upgraded. Is there any way we could achieve the end using a more modern way?
If you want to use only EncodingUtils you can add the legacy dependency for Apache
android {
useLibrary 'org.apache.http.legacy'
}
In case your postData is a buffer it is enough to just call
byte[] bytes = {...}
String str = new String(bytes, "UTF-8"); // for UTF-8 encoding`

Android M HttpClient removal breaks compatibility?

We know that Android M has removed support for HttpClient. Apps linking with it will have to explicitly say so: add a library dependency in your gradle file.
But does it break backward compatibility with existing applications?
Suppose I wrote an app which supports Api level 1 and higher and it's never maintained since last year. The targetSdkLevel of the app is certainly lower than 22. Will it crash on Android M? The source code could have been lost.
The answer looks to be yes.
I looked in the Android M source code through the SDK manager, and the package for the client is not in the source.
Package name for the HttpClient:
org.apache.http.client.HttpClient
Existing paths:
org.apache.http.conn
org.apache.http.params
Ways to fix this:
Add this to your build.graddle:
android {
useLibrary 'org.apache.http.legacy'
}
Use OkHttp-UrlConnection as an almost drop in replacement by adding this to your build.graddle:
compile 'com.squareup.okhttp:okhttp-urlconnection:2.4.0'
Example usage:
private static OkHttpClient okHttpClient = new OkHttpClient();
HttpURLConnection urlConnection = new OkUrlFactory(okHttpClient).open(url);

API version range in an Android Studio project

I'm curious about what is the sense in setting both target and minimal supported API version when starting a new Android Studio project. I mean, if I set that minimal API is, say, 8, then I won't be able to use features from 22 (which could be my target), because it would break compatibility with API 8.
if I set that minimal API is, say, 8, then I won't be able to use
features from 22
You can use API level >= 8 features in application, but you have to check OS version of the device first, see following code, that's how you can maintain compatibility
if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB){
//use features of API 3.0
} else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB_MR1){
//use features of API 3.1
} else if (android.os.Build.VERSION.SDK_INT == android.os.Build.VERSION_CODES.HONEYCOMB_MR2){
//use features of API 3.2
}else{
// and so on....
}
Other then code, use can use resource folders on the basis of API level, like:
values-v11
values-v12
values-14
....
and
drawable-v11
drawable-v12
drawable-14
....
This is not correct. By setting target API to 8 you can't use any features added after, but specifying minimum API is just a guarantee that you application will work on such devices. You don't guarantee that it provide all features on such devices, nor minimum API level restricts what features it could use when running on later versions of OS.
Look at PE history - all win32 executable files are compatible with MSDOS, but all they say after executing is just "This is not a MSDOS program.". Similar to this, it is your decision what features you provide on which OS you support.

send socket request from android client to sails.js 0.11

have any idea how to make a socket.get() request in android?
I try this:
JSONObject obj = new JSONObject();
obj.put("url","/controller/action");
socketClient.emit("get",obj);
And the server response is:
Error (SAILS:HOOK:SOCKETS:PARSE_VIRTUAL_REQ):: Failed to parse incoming socket.io request
...
details: 'Sails v0.11.x is not compatible with the socket.io/sails.io.js client SDK version you are using (0.9.0). Please see the v0.11 migration guide on http://sailsjs.org for more information.'
Just change your url to something like below
http://xxx.xxx.xxx.xxx:1234/?__sails_io_sdk_version=0.11.0
For some reason the version is not available in the SDK META DATA information. In the file node_modules\sails\node_modules\sails-hook-sockets\lib\parse-sdk-metadata.js there is a code which checks if version is not available in the meta data then assume the version is 0.9.0
if (!handshake.query[SDK_META_PARAMS.version]) {
handshake.query[SDK_META_PARAMS.version] = '0.9.0';
}
So i changed 0.9.0 to 0.11.0 and voila! things are working.
I don't know about socket.get() request in android.
But I know that if your server is running v.0.11 of Sails, you need to upgrade your sails.io.js client to v0.11, as v0.9 is not compatible.
It's explained in the migration guide, with instructions on how to upgrade: sails generate sails.io.js --force. All the details are in the link:
https://github.com/balderdashy/sails/blob/master/0.11-migration-guide.md#differences

Categories

Resources