B4J Question OKHttputils has a FileNotFoundException

xulihang

Active Member
Licensed User
Longtime User
I never encounter this error on my computer, but my users frequently report this to me. I cannot figure out the reason.

This happens when the app makes an HTTP request continuously (>2 times per second, sample code). Does this has anything to do with OKHttpUtils?

B4X:
error: java.io.FileNotFoundException: C:\Users\zero4\AppData\Local\Temp\14 (Access denied 拒绝访问。)
StackTrace: java.io.FileNotFoundException: C:\Users\zero4\AppData\Local\Temp\14 (�ܾ����ʡ�)
at java.io.FileOutputStream.open0(Native Method)
at java.io.FileOutputStream.open(Unknown Source)
at java.io.FileOutputStream.(Unknown Source)
at anywheresoftware.b4a.objects.streams.File.OpenOutput(File.java:439)
at org.xulihang.imagetrans.httputils2service._hc_responsesuccess(httputils2service.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:91)
at anywheresoftware.b4a.BA$3.run(BA.java:247)
at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
 
Last edited:

xulihang

Active Member
Licensed User
Longtime User
libraries used.

无标题.png
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
About this line in the OKHttpUtils's httputils2service class:

B4X:
Dim out As OutputStream = File.OpenOutput(TempFolder, TaskId, False)

Maybe there is a file with the same name but deleted by other programs.
 
Last edited:
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
I think the user is restricted and does not have permission to write to the desired folder.
I don't think he does not have permissions to this Temp folder. In fact, sometimes the http request can be made, but when it runs batch requests (> 2 times per second), this error will happen.
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
Are you checking that Job.Success is true and aren't releasing the HttpJob prematurely?
Here is the code:

B4X:
Sub Translate
    For i = 0 To 100
        Sleep(200)
        Wait For (MyMemory("Test text","en","zh")) Complete (result As String)
        Log(result)
    Next
End Sub

Sub MyMemory(source As String,sourceLang As String,targetLang As String) As ResumableSub
    Dim email As String = "[email protected]"
    Dim su As StringUtils
    source=su.EncodeUrl(source,"UTF-8")
    Dim job As HttpJob
    job.Initialize("job",Me)
    Dim langpair As String
    langpair=sourceLang&"|"&targetLang
    langpair=su.EncodeUrl(langpair,"UTF-8")
    Dim param As String
    param="?q="&source&"&langpair="&langpair
    If email<>"" Then
        param=param&"&de="&email
    End If
   
    job.Download("https://api.mymemory.translated.net/get"&param)
    Dim translatedText As String=""
    wait for (job) JobDone(job As HttpJob)
    If job.Success Then
        Try
            Log(job.GetString)
            Dim json As JSONParser
            json.Initialize(job.GetString)
            Dim response As Map
            response=json.NextObject.Get("responseData")
            translatedText=response.Get("translatedText")
        Catch
            Log(LastException)
        End Try
    End If
    job.Release
    Return translatedText
End Sub
 
Upvote 0

MarcoRome

Expert
Licensed User
Longtime User
What version of okhttputils2 are you using ?
Why don't you use okhttputils2 as a library ?
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
What version of okhttputils2 are you using ?
Why don't you use okhttputils2 as a library ?
Version 2.9.5 as in the screenshot I posted at #2. I use it as a library. I am just looking at the code to figure out which line causes the problem.
 
Upvote 0

Magma

Expert
Licensed User
Longtime User
My guess is that another process locks the file. Might be an anti-virus software.

You can change the TempFolder and see if it makes any difference.

B4X:
HttpUtils2Service.Service_Create
 HttpUtils2Service.TempFolder = xui.DefaultFolder
Is that possible to do it everytime you need to download ? 🤔 (change temp folder)
 
Upvote 0

xulihang

Active Member
Licensed User
Longtime User
My guess is that another process locks the file. Might be an anti-virus software.

You can change the TempFolder and see if it makes any difference.

B4X:
HttpUtils2Service.Service_Create
 HttpUtils2Service.TempFolder = xui.DefaultFolder

Thanks. I will let them have a try.
 
Upvote 0
Top