Android Question Http Requests - Problem after restart

TechAdmin

Member
Hello everyone. I face a problem and I can not find any solution.
I have create a test application using Default Project in B4A IDE that when I press or long press a button I make a http job.
The code in the Main Activity is the follow:

[CODE lang="b4x" title="Main Activity"]#Region Project Attributes
#ApplicationLabel: TESTelev
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#BridgeLogger: True

#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
Dim j As HttpJob
Dim p As Phone
Dim user_agent As String

user_agent = Application.LabelName & "/" & Application.VersionCode & " (Android; API-LEVEL 30; " & p.Model & ")"
j.Initialize("j",Me)
j.Download("http://10.0.0.205:8440")
j.GetRequest.SetHeader("Connection", "Close")
j.GetRequest.SetHeader("User-Agent",user_agent)
j.GetRequest.SetHeader("Accept","*/*")
j.GetRequest.SetHeader("Accept-Encoding","gzip, deflate, br")
j.GetRequest.Timeout = 20000

Wait For (j) JobDone (j As HttpJob)


If j.Success Then
LogColor(j.GetString, Colors.Green)
ToastMessageShow("Success!",False)
Activity.Color=Colors.Green

Else
LogColor("** Get Request has been failed! **", Colors.Red)
ToastMessageShow("Fail!",False)
Activity.Color=Colors.Red

End If

j.Release

Sleep(500)

Activity.Color = Colors.White

End Sub

Sub Button1_LongClick
Dim link As String, Data As String
Dim user_agent As String
Dim p As Phone

user_agent = Application.LabelName & "/" & Application.VersionCode & " (Android; API-LEVEL 30; " & p.Model & ")"

link = "http://10.0.0.205:8440"
Data = "FL2"

Dim j As HttpJob, resp = "" As String
j.Initialize("j",Me)
j.PostString(link,Data)
j.GetRequest.SetContentType("text/plain")
j.GetRequest.SetHeader("AUTHORIZATION", "mypassword")
j.GetRequest.SetHeader("Connection", "Close")
j.GetRequest.SetHeader("Accept","*/*")
j.GetRequest.SetHeader("User-Agent",user_agent)
j.GetRequest.SetHeader("Accept-Encoding","gzip, deflate, br")
j.GetRequest.SetHeader("Cache-Control", "no-cache")
j.GetRequest.Timeout = 20000

Wait For (j) JobDone (j As HttpJob)


If j.Success Then
resp = j.GetString
LogColor(resp, Colors.Green)
ToastMessageShow("Success!",False)
Activity.Color=Colors.Green

Else
resp = j.ErrorMessage
LogColor("** Post Request has been failed! **", Colors.Red)
ToastMessageShow("Fail!",False)
Activity.Color=Colors.Red

End If

j.Release

Sleep(500)

Activity.Color = Colors.White

End Sub[/CODE]

The layout contains only 1 button. There are no extra classess and the service starter is the same as the default one.
Manifest is the follow:

[CODE lang="b4x" title="Manifest"]AddManifestText(
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)

CreateResourceFromFile(Macro, Core.NetworkClearText)

AddPermission("android.permission.INTERNET")
AddPermission("android.permission.ACCESS_WIFI_STATE")
AddPermission("android.permission.ACCESS_NETWORK_STATE")
AddPermission("android.permission.WRITE_EXTERNAL_STORAGE")

SetApplicationAttribute(android:usesCleartextTraffic, "true")
'End of default text.[/CODE]

When I first compile the code it runs correctly. My server on ip: 10.0.0.205 and port: 8440 runs and accept the message so I receive in my app a success message. The problem occurs after device restart. If I power off the device (or restart it) and when it turns on, I get into my application and press the button. I receive instantly an error message while the postman receives success message, either I hit the button before that or after the button has been pressed. I noticed that if I leave my app idle for some minutes then if I press the button it will sent with success the post/get request.After that I have tried that if I make force close the app or turn on again the bridge ("START") or close and open the app or turn off WiFi and turn it on, it will send the request with success.
I realy do not understand why I face that error. If anyone knows anything, his/her help will be very helpful.
Note that I have test the code in B4A IDE Version 13.0, the library httputils is up to date (version 3.04), I have tried to install the app through bridge, or usb debugging or just installed the apk, I have test 3 different android devices and all of them has the same problem, I have compiled from 2 differents pc. While I faced problem using my app (the above code) I were testing my server using postman and the postman was working with no problem.

With regards,
Nikos.
 
Hello everyone. I face a problem and I can not find any solution.
I have create a test application using Default Project in B4A IDE that when I press or long press a button I make a http job.
The code in the Main Activity is the follow:

[CODE lang="b4x" title="Main Activity"]#Region Project Attributes
#ApplicationLabel: TESTelev
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#BridgeLogger: True

#End Region

#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region

Sub Process_Globals

End Sub

Sub Globals

End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("Layout")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub Button1_Click
Dim j As HttpJob
Dim p As Phone
Dim user_agent As String

user_agent = Application.LabelName & "/" & Application.VersionCode & " (Android; API-LEVEL 30; " & p.Model & ")"
j.Initialize("j",Me)
j.Download("http://10.0.0.205:8440")
j.GetRequest.SetHeader("Connection", "Close")
j.GetRequest.SetHeader("User-Agent",user_agent)
j.GetRequest.SetHeader("Accept","*/*")
j.GetRequest.SetHeader("Accept-Encoding","gzip, deflate, br")
j.GetRequest.Timeout = 20000

Wait For (j) JobDone (j As HttpJob)


If j.Success Then
LogColor(j.GetString, Colors.Green)
ToastMessageShow("Success!",False)
Activity.Color=Colors.Green

Else
LogColor("** Get Request has been failed! **", Colors.Red)
ToastMessageShow("Fail!",False)
Activity.Color=Colors.Red

End If

j.Release

Sleep(500)

Activity.Color = Colors.White

End Sub

Sub Button1_LongClick
Dim link As String, Data As String
Dim user_agent As String
Dim p As Phone

user_agent = Application.LabelName & "/" & Application.VersionCode & " (Android; API-LEVEL 30; " & p.Model & ")"

link = "http://10.0.0.205:8440"
Data = "FL2"

Dim j As HttpJob, resp = "" As String
j.Initialize("j",Me)
j.PostString(link,Data)
j.GetRequest.SetContentType("text/plain")
j.GetRequest.SetHeader("AUTHORIZATION", "mypassword")
j.GetRequest.SetHeader("Connection", "Close")
j.GetRequest.SetHeader("Accept","*/*")
j.GetRequest.SetHeader("User-Agent",user_agent)
j.GetRequest.SetHeader("Accept-Encoding","gzip, deflate, br")
j.GetRequest.SetHeader("Cache-Control", "no-cache")
j.GetRequest.Timeout = 20000

Wait For (j) JobDone (j As HttpJob)


If j.Success Then
resp = j.GetString
LogColor(resp, Colors.Green)
ToastMessageShow("Success!",False)
Activity.Color=Colors.Green

Else
resp = j.ErrorMessage
LogColor("** Post Request has been failed! **", Colors.Red)
ToastMessageShow("Fail!",False)
Activity.Color=Colors.Red

End If

j.Release

Sleep(500)

Activity.Color = Colors.White

End Sub[/CODE]

The layout contains only 1 button. There are no extra classess and the service starter is the same as the default one.
Manifest is the follow:

[CODE lang="b4x" title="Manifest"]AddManifestText(
<uses-sdk android:minSdkVersion="21" android:targetSdkVersion="33"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
CreateResourceFromFile(Macro, Themes.LightTheme)

CreateResourceFromFile(Macro, Core.NetworkClearText)

AddPermission("android.permission.INTERNET")
AddPermission("android.permission.ACCESS_WIFI_STATE")
AddPermission("android.permission.ACCESS_NETWORK_STATE")
AddPermission("android.permission.WRITE_EXTERNAL_STORAGE")

SetApplicationAttribute(android:usesCleartextTraffic, "true")
'End of default text.[/CODE]

When I first compile the code it runs correctly. My server on ip: 10.0.0.205 and port: 8440 runs and accept the message so I receive in my app a success message. The problem occurs after device restart. If I power off the device (or restart it) and when it turns on, I get into my application and press the button. I receive instantly an error message while the postman receives success message, either I hit the button before that or after the button has been pressed. I noticed that if I leave my app idle for some minutes then if I press the button it will sent with success the post/get request.After that I have tried that if I make force close the app or turn on again the bridge ("START") or close and open the app or turn off WiFi and turn it on, it will send the request with success.
I realy do not understand why I face that error. If anyone knows anything, his/her help will be very helpful.
Note that I have test the code in B4A IDE Version 13.0, the library httputils is up to date (version 3.04), I have tried to install the app through bridge, or usb debugging or just installed the apk, I have test 3 different android devices and all of them has the same problem, I have compiled from 2 differents pc. While I faced problem using my app (the above code) I were testing my server using postman and the postman was working with no problem.

With regards,
Nikos.
I forgot to mention that in my original app (and to the test app) I have put the following code:
[CODE lang="b4x" title="StartHttpUtils2"]Private Sub StartHttpUtils2Service
#if release
If HttpUtils2Service.TempFolder = "" Then
Dim jo As JavaObject
jo.InitializeNewInstance(Application.PackageName & ".httputils2service", Null)
jo.RunMethod("onReceive", Array(Null, Null))
End If
#end if
End Sub[/CODE]
 
Upvote 0
orgot to mention that in my original app (and to the test app) I have put the following code:
1. This is not needed with the latest version.

2. Don't waste your time with activities. Switch to B4XPages. It might be related to this issue.

3. Should be removed: AddPermission("android.permission.WRITE_EXTERNAL_STORAGE"). Will fail on new devices.

4. After you switch to B4XPages, if it still fails then post the logs.
 
Upvote 0
1. This is not needed with the latest version.

2. Don't waste your time with activities. Switch to B4XPages. It might be related to this issue.

3. Should be removed: AddPermission("android.permission.WRITE_EXTERNAL_STORAGE"). Will fail on new devices.

4. After you switch to B4XPages, if it still fails then post the logs.
2. I have tried from the first time B4XPages but I faced the same problem.

4. The unfiltered logs:
java.net.ConnectException: Failed to connect to /10.0.0.205:8440
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:297)
at okhttp3.internal.connection.RealConnection.connect(RealConnection.kt:207)
at okhttp3.internal.connection.ExchangeFinder.findConnection(ExchangeFinder.kt:226)
at okhttp3.internal.connection.ExchangeFinder.findHealthyConnection(ExchangeFinder.kt:106)
at okhttp3.internal.connection.ExchangeFinder.find(ExchangeFinder.kt:74)
at okhttp3.internal.connection.RealCall.initExchange$okhttp(RealCall.kt:255)
at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.kt:32)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.kt:95)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.kt:83)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.kt:76)
at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.kt:109)
at okhttp3.internal.connection.RealCall.getResponseWithInterceptorChain$okhttp(RealCall.kt:201)
at okhttp3.internal.connection.RealCall.execute(RealCall.kt:154)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:175)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:172)
at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:220)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:462)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641)
at java.lang.Thread.run(Thread.java:920)
Caused by: java.net.ConnectException: failed to connect to /10.0.0.205 (port 8440) from /10.0.0.122 (port 50448) after 20000ms: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:349)
at libcore.io.IoBridge.connectErrno(IoBridge.java:238)
at libcore.io.IoBridge.connect(IoBridge.java:180)
at java.net.PlainSocketImpl.socketConnect(PlainSocketImpl.java:142)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:390)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:230)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:212)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:436)
at java.net.Socket.connect(Socket.java:632)
at okhttp3.internal.platform.Platform.connectSocket(Platform.kt:120)
at okhttp3.internal.connection.RealConnection.connectSocket(RealConnection.kt:295)
... 23 more
Caused by: android.system.ErrnoException: isConnected failed: ECONNREFUSED (Connection refused)
at libcore.io.IoBridge.isConnected(IoBridge.java:336)
... 33 more
ResponseError. Reason: java.net.ConnectException: Failed to connect to /10.0.0.205:8440, Response:
** Get Request has been failed! **

I have test it also in postman, but the postman receive success response.
 
Upvote 0
Update!
I did change again to B4XPages, I added again the "StartHttpUtils2Service" sub, which I reffered above in the comment, in the Starter. The difference now is that I did put that sub in Service_Start sub instead of Service_Create where I put before. The result is that now it works with no problem. Yesterady, I did the same scenarios where the project failed, now the project pass the tests with success.
@Erel because you mention it before, now I have some questions.
1. Do we have to use B4XPages in any project we create? Does the default project is "useless" now?
2. Does the compilation method occurs any problem to httpjob requests? The "Release" or "Release (obfuscated)".
 
Upvote 0
Back
Top