Android Question HTTP on API19, API23 and API28

warwound

Expert
Licensed User
Longtime User
I'm updating some previously working code which uses the OkHttpUtils2 library.
The code needs to be compatible with android api levels 19, 23 and 28.
Since the code was last compiled there has been an update to the native java OkHttp library and android api level 19 is no longer supported.
On an api 19 device I now see the error:
Caused by: java.lang.IllegalStateException: Expected Android API level 21+ but was 19
at okhttp3.internal.platform.AndroidPlatform.<clinit>(AndroidPlatform.kt:153
On an api 23 device the code works fine, and on an api 28 device the code works fine.

I tried switching from OkHttpUtils2 to the older HttpUtils2 library.
This works on api 19 and api 23 but not on api 28 where I see the error:
Caused by: java.lang.ClassNotFoundException: Didn't find class "org.apache.http.client.methods.HttpGet" on path
In api 28 (or some earlier version) Google have removed the old Http library.

I thought I'd detect the device api level at runtime.
If it's api 19 then use the 'low level' HttpClient and HttpRequest libraries.
If it's greater than 19 then use the OkHttpUtils2 library.
This is a non starter as it fails to compile with the error:
src\mypackagename\httptest\main.java:535: error: cannot access ClientProtocolException
_httpclient1.Initialize("HttpClient1");
^
class file for org.apache.http.client.ClientProtocolException not found
Seems like I'd have to compile with android.jar api level 21 or older to enable this to compile.

Looks like I could download a version of the native java OkHttp from before when they dropped support for api 19.
And replace the built in native java OkHttp library with the old version.
But I'm reluctant to do this as it'll surely create problems at a later date - when I next update b4a for example and have to remember to manually replace the latest OkHttp with an old version.

Am I missing something obvious here?
Can b4a no longer create apps which can make HTTP requests and are compatible with android api levels 19 and later?
 

DonManfred

Expert
Licensed User
Longtime User
Upvote 1

warwound

Expert
Licensed User
Longtime User
From https://www.b4x.com/android/forum/threads/android-jar-targetsdkversion-minsdkversion.87610/#content

- 28 - The old http SDK is not available by default. This will cause problems with native libraries such as Google Maps who rely on the old SDK. To enable it: https://www.b4x.com/android/forum/t...ar-error-in-android-9-pie.103247/#post-649875
Hmm you mean:
B4X:
AddApplicationText(
<uses-library
      android:name="org.apache.http.legacy"
      android:required="false" />
)

How will that help me?
Are you suggesting I change the false to true and then I'll find the old HTTP library available on all devices regardless of api level?
I can then use my idea:
I thought I'd detect the device api level at runtime.
If it's api 19 then use the 'low level' HttpClient and HttpRequest libraries.
If it's greater than 19 then use the OkHttpUtils2 library.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Are you suggesting I change the false to true and then I'll find the old HTTP library available on all devices regardless of api level?
i´m not sure it that works. You can try and if it works you can
I can then use my idea
that´s what i tried to suggest. yes.

I remember Erel said in the past (where another user want to use Api <23 too) that the user can create two versions of the app. One for old devices (using the old okhttp Version)), one for new devices (using the newest okhttputils).

I don´t know if that is ok for you.

In my case all my apps are running on Android 5 or higher so i can live with it that it does not work on Android 4 devices.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I remember Erel said in the past (where another user want to use Api <23 too) that the user can create two versions of the app. One for old devices (using the old okhttp Version)), one for new devices (using the newest okhttputils).
This is actually a pretty easy solution...

I can check library HttpUtils2 and uncheck OkHttpUtils2, compile and get an api 19 compatible apk.

I can uncheck library HttpUtils2 and check OkHttpUtils2, compile and get an api 20+ compatible apk.

Now can I automate this with 2 different build configurations?
Can a build configuration check or uncheck a library?
I'll search the forum for an answer shortly.

Meanwhile I found another possible solution...
My app only uses HTTP to ping a java Tomcat app we have running on a server to check that the Tomcat app is available.
I could instead ping my Tomcat app using UDP and then have no need to use HTTP within my b4a app.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Can a build configuration check or uncheck a library?
i don´t think so.
I could instead ping my Tomcat app using UDP and then have no need to use HTTP within my b4a app.
i would prefer this one then. You can stick with the newest (and suggested) okhttputils2.
 
Upvote 0

warwound

Expert
Licensed User
Longtime User
I looked at my java tomcat app source to see how easy it'd be to add a UDP server.
Then noticed it already runs a websocket server - used when a browser connects to the tomcat app.
So i'm now thinking - can my b4a app ping the websocket server without running into api level compatibility problems?
Then there's no need for a new UDP server.
 
Upvote 0
Top