B4A Library OkHttp - Replaces the Http library

Discussion in 'Additional libraries, classes and official updates' started by Erel, Jun 9, 2015.

  1. Erel

    Erel Administrator Staff Member Licensed User

    The current Http library is based on the Android version of Apache HttpClient. Google has stopped updating this SDK a long time ago and they are removing it from Android M. This means that it is a good time now to switch to a different implementation.

    Note that if you are using HttpUtils2 (as you should) it is also based on Http library.

    The OkHttp library is based on the OkHttp SDK: http://square.github.io/okhttp/
    As this is a newer SDK it supports new features such as SSL SNI, SPDY, Patch requests and better performance.

    The OkHttp wrapper API is almost identical to Http library API. The main difference is that the object names start with Ok (OkHttpClient, OkHttpResponse and OkHttpRequest).

    This library requires Android 2.3+.

    Attached is a modified version of HttpUtils2 library (v2.12) that is based on OkHttp instead of Http.

    If you want to use the new HttpUtils2 library you need to copy it to the internal libraries folder.

    V1.01 - Compatible with B4J.
    V1.00 - Adds support for digest authentication.

    OkHttpUtils2 v2.20 is attached. It adds support for multipart requests.

    Example:
    Code:
    Dim j As HttpJob
    j.Initialize(
    "j", Me)
    Dim fd As MultipartFileData
    fd.Initialize
    fd.KeyName = 
    "file"
    fd.Dir = 
    File.DirAssets
    fd.FileName = 
    "image.png"
    fd.ContentType = 
    "image/png"
    j.PostMultipart(
    "http://...", CreateMap("param1""value1"), Array(fd))
    OkHttp and OkHttpUtils2 libraries are included in the IDE.
     

    Attached Files:

    Last edited: Jun 12, 2017
  2. thedesolatesoul

    thedesolatesoul Expert Licensed User

    Do you know why? Are they trying to merge it into GooglePlayServices? Or are they moving towards Volley?
     
    Carlos marin and Peter Simpson like this.
  3. Erel

    Erel Administrator Staff Member Licensed User

    They moved to HttpUrlConnection (which is simpler to work with but less powerful).
     
    lemonisdead and thedesolatesoul like this.
  4. MarcoRome

    MarcoRome Expert Licensed User

    I tried the version and without changing any code works perfectly. Good job.
     
    smercan and Erel like this.
  5. KMatle

    KMatle Expert Licensed User

    Any restrictions (copyright?)
     
  6. thedesolatesoul

    thedesolatesoul Expert Licensed User

    KMatle and Peter Simpson like this.
  7. incendio

    incendio Well-Known Member Licensed User

    What about RDC? As I know, RDC use HttpUtils2 library, is this new library works OK with RDC?
     
  8. Peter Simpson

    Peter Simpson Well-Known Member Licensed User

    I've migrated over from Http to OkHttp and everything appears to be working fine, thank you...
     
    Last edited: Mar 14, 2016
    lemonisdead likes this.
  9. tchart

    tchart Active Member Licensed User

    On a slightly related topic, would this library have a synchronous http method?
     
  10. DonManfred

    DonManfred Expert Licensed User

    just replace the old httputils2 with the one provided here.
     
  11. Erel

    Erel Administrator Staff Member Licensed User

    It is not possible (for good reasons) to use synchronous requests in Android. Such requests can easily freeze the app for a long time and Android will kill the process after 5 seconds.
     
    luke2012 likes this.
  12. tchart

    tchart Active Member Licensed User

    Thanks Erel, Ive brought this up before and understand the issue on Android. I was actually hoping for a solution I could use on B4J. I have a requirement for a synchronous web request.

    Ive started wrapping https://github.com/kevinsawicki/http-request which works nicely and supports synchronous requests.

    Thanks
     
  13. tchart

    tchart Active Member Licensed User

    Also, did you mean "not possible" or "not recommended"?
     
  14. Erel

    Erel Administrator Staff Member Licensed User

    I'm talking about Android here. It is very simple to create synchronous http requests (simpler than implementing asynchronous requests). However if the request takes more than 5 seconds to complete and the user touches the screen, Android will show the ANR dialog.

    If you want to further discuss this then please start a new thread.
     
  15. itgirl

    itgirl Active Member Licensed User

    "they are removing it from Android" so newer Androids wont work with our old precious HttpUtils2:confused: so should we move on to the OkHttp and update all apps or stick with the old method ?! since it's a beta :confused:
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    No need to hurry. We are talking about Android 6 which hasn't been released yet.
     
    Peter Simpson, lemonisdead and itgirl like this.
  17. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Dear Erel : I probably did something wrong as I am getting some error in the log window

    Code:
    ** Activity (main) Create, isFirst = true **
    java.lang.NoClassDefFoundError: java.net.IDN
        at com.squareup.okhttp.HttpUrl$Builder.domainToAscii(HttpUrl.java:
    1315)
        at com.squareup.okhttp.HttpUrl$Builder.canonicalizeHost(HttpUrl.java:
    1195)
        at com.squareup.okhttp.HttpUrl$Builder.parse(HttpUrl.java:
    959)
        at com.squareup.okhttp.HttpUrl.parse(HttpUrl.java:
    585)
        at com.squareup.okhttp.Request$Builder.url(Request.java:
    156)
        at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$OkHttpRequest.InitializeGet(OkHttpClientWrapper.java:
    257)
        at anywheresoftware.b4a.samples.httputils2.httpjob._v7(
    httpjob.java:73)
        at anywheresoftware.b4a.samples.httputils2.main._activity_create(main.java:
    328)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    521)
        at anywheresoftware.b4a.BA.raiseEvent2(BA.java:
    187)
        at anywheresoftware.b4a.samples.httputils2.main.afterFirstLayout(main.java:
    100)
        at anywheresoftware.b4a.samples.httputils2.main.access$
    100(main.java:17)
        at anywheresoftware.b4a.samples.httputils2.main$WaitForLayout.run(main.java:
    78)
        at android.os.Handler.handleCallback(Handler.java:
    587)
        at android.os.Handler.dispatchMessage(Handler.java:
    92)
        at android.os.Looper.loop(Looper.java:
    123)
        at android.app.ActivityThread.main(ActivityThread.java:
    4627)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:
    521)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:
    868)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:
    626)
        at dalvik.system.NativeStart.main(Native Method)

    What I did is : copy the OkHttp Library and the HttpUtils2 XML and Jar in my libraries folder. Copied the HttpJob shared to my shared modules folder.
    I tried to compile and install on Nexus 6 (Lollipop), Nexus 4 (not upgraded KitKat) and Moto G XT 1032 (Lollipop)
    I did not modify the HttpUtils2 example.
     
    Last edited: Jun 11, 2015
  18. DonManfred

    DonManfred Expert Licensed User

    But you REPLACED your old httputils2 with the one here provided? That´s a NEED
     
    lemonisdead likes this.
  19. lemonisdead

    lemonisdead Well-Known Member Licensed User

    Hello Manfred,
    I did in my Additional Libraries folder but I have missed to remove the ones in the C:\Basic4android\5\Libraries. It works now. On my system, strangely (or perhaps not), the Additional Libraries folder is not selected against the standard folder.
    I am sorry for the disturb : it works now
     
    DonManfred likes this.
  20. DonManfred

    DonManfred Expert Licensed User

    happy i could help dear @lemonisdead :)
     
    lemonisdead likes this.
Loading...