Sub Process_Globals
#if B4A or B4J
Private hc As OkHttpClient
#else
Private hc As HttpClient
#end if
Private TaskIdToJob As Map
Public TempFolder As String
Private taskCounter As Int
Public MyInterceptor As JavaObject
End Sub
Sub Service_Create
If hc.IsInitialized = False Then
TempFolder = File.DirTemp
Dim jo As JavaObject = hc
Dim builder As JavaObject = jo.RunMethod("sharedInit", Array("hc"))
MyInterceptor.InitializeNewInstance("b4j.example.httputils2service$MyInterceptor", Null) '<-------- change based on package name
builder.RunMethod("addNetworkInterceptor", Array(MyInterceptor))
jo.SetField("client", builder.RunMethod("build", Null))
TaskIdToJob.Initialize
End If
End Sub
#if Java
import okhttp3.*;
import okhttp3.Interceptor.*;
import java.io.IOException;
public static class MyInterceptor implements okhttp3.Interceptor {
public Headers headers;
public Response intercept(Chain chain) throws IOException {
Request r = chain.request();
headers = r.headers();
return chain.proceed(r);
}
}
#End If