Android Question OKHttpUtils Issue

walterf25

Expert
Licensed User
Hello all, i'am trying to set a proxy using the httputils class, but am not having so much luck, the problem is that the url i'am trying to access only accept national requests, and as i'am outside of that country i always get an error, i found a code example given by @Erel, but i'am getting this error, i'm not sure if this code can only be used for B4J or if it can be used in B4A as well, any help will be greatly appreciated.

Here's the error I receive
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
httputils2service_setproxy (B4A line: 34)
jo.GetFieldJO("client").RunMethod("setProxy", Arr
java.lang.RuntimeException: Method: setProxy not found in: okhttp3.OkHttpClient
at anywheresoftware.b4j.object.JavaObject$MethodCache.getMethod(JavaObject.java:366)
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:119)
at com.genesis.buscadordeplacas.httputils2service._setproxy(httputils2service.java:346)
at com.genesis.buscadordeplacas.httputils2service._submitjob(httputils2service.java:382)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1151)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
And here is the code i'm referring to:

B4X:
Sub SetProxy (hc1 As OkHttpClient, Host As String, Port As Int)
    Dim jo As JavaObject = hc1
    Dim proxy, socketAddress As JavaObject
    socketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array (Host, Port))
    proxy.InitializeNewInstance("java.net.Proxy", Array ("HTTP", socketAddress))
    jo.GetFieldJO("client").RunMethod("setProxy", Array(proxy))
End Sub
Thanks everyone!

Walter
 

walterf25

Expert
Licensed User
Looks like the method setProxy is not available in okhttp 3
Hi @DonManfred I get that, the question is why would Erel post this code if that method is not available in the okhttpclient class, can you think of any solutions to get this working?

Regards,
Walter
 

DonManfred

Expert
Licensed User

Erel

Administrator
Staff member
Licensed User
Try this:
B4X:
'Replace the existing method in HttpUtils2Service
Sub Initialize
   If hc.IsInitialized = False Then
     TempFolder = File.DirTemp
     Dim jo As JavaObject = hc
Dim proxy, socketAddress As JavaObject
socketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array (Host, Port))
    proxy.InitializeNewInstance("java.net.Proxy", Array ("HTTP", socketAddress))
     Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))

     builder.RunMethod("proxy", Array(proxy)) '<---- fixed!!!
     jo.SetField("client", builder.RunMethod("build", Null))  
     TaskIdToJob.Initialize
   End If
End Sub
You need to replace Host and Post with your values.
 
Last edited:

walterf25

Expert
Licensed User
Try this:
B4X:
'Replace the existing method in HttpUtils2Service
Sub Initialize
   If hc.IsInitialized = False Then
     TempFolder = File.DirTemp
     Dim jo As JavaObject = hc
Dim proxy, socketAddress As JavaObject
socketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array (Host, Port))
    proxy.InitializeNewInstance("java.net.Proxy", Array ("HTTP", socketAddress))
     Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))

     builder.RunMethod("proxy", Array(False))
     jo.SetField("client", builder.RunMethod("build", Null))  
     TaskIdToJob.Initialize
   End If
End Sub
You need to replace Host and Post with your values.
Thank you @Erel I will give this a try.

Regards,
Walter
 

walterf25

Expert
Licensed User
Try this:
B4X:
'Replace the existing method in HttpUtils2Service
Sub Initialize
   If hc.IsInitialized = False Then
     TempFolder = File.DirTemp
     Dim jo As JavaObject = hc
Dim proxy, socketAddress As JavaObject
socketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array (Host, Port))
    proxy.InitializeNewInstance("java.net.Proxy", Array ("HTTP", socketAddress))
     Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))

     builder.RunMethod("proxy", Array(False))
     jo.SetField("client", builder.RunMethod("build", Null))  
     TaskIdToJob.Initialize
   End If
End Sub
You need to replace Host and Post with your values.
Hi Erel, i've placed your code inside the "Sub Service_Create" but i get the following error:

B4X:
Sub Service_Create
'''    TempFolder = File.DirInternalCache
'''    hc.InitializeAcceptAll("hc")
'''    ''hc.Initialize("hc")
'''   
    '''    TaskIdToJob.Initialize

    If hc.IsInitialized = False Then
        TempFolder = File.DirInternalCache
        Dim jo As JavaObject = hc
        Dim proxy, socketAddress As JavaObject
        socketAddress.InitializeNewInstance("java.net.InetSocketAddress", Array ("177.97.43.201", 8080))
        proxy.InitializeNewInstance("java.net.Proxy", Array ("HTTP", socketAddress))
        Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))

        builder.RunMethod("proxy", Array(False))
        jo.SetField("client", builder.RunMethod("build", Null))
        TaskIdToJob.Initialize
    End If
End Sub
and here's is the error:
*** Service (starter) Create ***
** Service (starter) Start **
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
httputils2service_service_create (B4A line: 29)
builder.RunMethod("proxy", Array(False))
java.lang.RuntimeException: Method: proxy not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:129)
at com.genesis.buscadordeplacas.httputils2service._service_create(httputils2service.java:299)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at com.genesis.buscadordeplacas.httputils2service.onCreate(httputils2service.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3481)
at android.app.ActivityThread.-wrap6(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
httputils2service_submitjob (B4A line: 67)
TaskIdToJob.Put(taskCounter, job)
java.lang.RuntimeException: Object should first be initialized (Map).
at anywheresoftware.b4a.AbsObjectWrapper.getObject(AbsObjectWrapper.java:50)
at anywheresoftware.b4a.objects.collections.Map.Put(Map.java:50)
at com.genesis.buscadordeplacas.httputils2service._submitjob(httputils2service.java:397)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.keywords.Common$11.run(Common.java:1151)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at anywheresoftware.b4a.Msgbox.waitForMessage(Msgbox.java:231)
at anywheresoftware.b4a.Msgbox.debugWait(Msgbox.java:190)
at anywheresoftware.b4a.debug.Debug.wait(Debug.java:216)
at anywheresoftware.b4a.debug.Debug.reachBP(Debug.java:362)
at anywheresoftware.b4a.debug.Debug.ErrorCaught(Debug.java:148)
at com.genesis.buscadordeplacas.httputils2service._service_create(httputils2service.java:312)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at com.genesis.buscadordeplacas.httputils2service.onCreate(httputils2service.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3481)
at android.app.ActivityThread.-wrap6(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
java.lang.RuntimeException: Unable to create service com.genesis.buscadordeplacas.httputils2service: java.lang.RuntimeException: java.lang.RuntimeException: Method: proxy not matched.
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3491)
at android.app.ActivityThread.-wrap6(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1721)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.RuntimeException: java.lang.RuntimeException: Method: proxy not matched.
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:223)
at anywheresoftware.b4a.BA.raiseEvent(BA.java:166)
at com.genesis.buscadordeplacas.httputils2service.onCreate(httputils2service.java:55)
at android.app.ActivityThread.handleCreateService(ActivityThread.java:3481)
... 8 more
Caused by: java.lang.RuntimeException: Method: proxy not matched.
at anywheresoftware.b4j.object.JavaObject.RunMethod(JavaObject.java:129)
at com.genesis.buscadordeplacas.httputils2service._service_create(httputils2service.java:299)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:186)
... 11 more
 
Top