Other Error using okhttputils2...

rbghongade

Active Member
Licensed User
Friends ,
I am using Okhttputils2 to download the same file using a timer.
First I use the following code to check the file size:
B4X:
Dim dd As DownloadData
    
        dd.url = link1
        dd.EventName = "dd"
        dd.Target = Me
        Dim j As HttpJob
        j.Initialize(dd.url, Me)
        j.Head(dd.url)
        Wait For (j) JobDone(j As HttpJob)
        j.Release
        Sleep(1000)
        If j.Success Then
        Dim fs As List=j.Response.GetHeaders.GetValueAt(0)
        Dim fsval As Int=fs.Get(0)
Then if the file size is changed I download the file using:
B4X:
Dim J1 As HttpJob
            J1.Initialize(dd.url, Me)
            Dim tag As JobTag
            tag.Initialize
            tag.data = dd
            J1.tag = tag
            jobs.Put(dd.url, J1)
            J1.Download(dd.url)
            Wait For (J1) JobDone(J1 As HttpJob)
Both these code snippets are in a timer_tick sub:
I have ensured that the timer is disabled after entering the timer sub and again enabling it after the file is downloaded and processed.
But I get intermittent error response as shown below:
B4X:
java.net.ProtocolException: Unexpected status line: 001550 19-12-15 9:02:25 19.82 85.6
    at okhttp3.internal.http.StatusLine.parse(StatusLine.java:69)
    at okhttp3.internal.http1.Http1Codec.readResponse(Http1Codec.java:191)
    at okhttp3.internal.http1.Http1Codec.readResponseHeaders(Http1Codec.java:132)
    at okhttp3.internal.http.CallServerInterceptor.intercept(CallServerInterceptor.java:54)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.connection.ConnectInterceptor.intercept(ConnectInterceptor.java:45)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.cache.CacheInterceptor.intercept(CacheInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.internal.http.BridgeInterceptor.intercept(BridgeInterceptor.java:93)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RetryAndFollowUpInterceptor.intercept(RetryAndFollowUpInterceptor.java:120)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:92)
    at okhttp3.internal.http.RealInterceptorChain.proceed(RealInterceptorChain.java:67)
    at okhttp3.RealCall.getResponseWithInterceptorChain(RealCall.java:179)
    at okhttp3.RealCall.execute(RealCall.java:63)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.executeWithTimeout(OkHttpClientWrapper.java:156)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper.access$0(OkHttpClientWrapper.java:153)
    at anywheresoftware.b4h.okhttp.OkHttpClientWrapper$ExecuteHelper.run(OkHttpClientWrapper.java:201)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
    at java.lang.Thread.run(Thread.java:748)
ResponseError. Reason: java.net.ProtocolException: Unexpected status line: 001550 19-12-15 9:02:25 19.82 85.6, Response: 58267
The bold part is the content of the file and file size respectively
This error is quite intermittent especially when the file size is larger than 50kb
 

DonManfred

Expert
Licensed User
Dim fs As List=j.Response.GetHeaders.GetValueAt(0)
- You are asuming the 1st key in the headersmap is the wanted one. This must not always be the case.
- you should NOT use GetValueAt. Iterate though the keys of the map and make sure to use the correct header key.
- why you are not using just okhttputils? as far as i know the DownloadData is from a b4a example!? It is not needed

B4X:
    Dim J1 As HttpJob
   J1.Initialize("", Me)
   J1.Head("http://snapshots.basic4android.de/3dmark0107.png")
   Wait For (J1) JobDone(Job As HttpJob)
   If Job.Success Then
       Dim headers As Map=Job.Response.GetHeaders
       For Each k As String In headers.Keys
           Log($"Header: ${k} = ${headers.Get(k)}"$)
       Next
       Dim lengthl As List = headers.Get("content-length")     
       Log($"${lengthl.Get(0)}"$)
   Else
       Log($"Job unsuccessful: ${Job.ErrorMessage}"$)
   End If
   Job.Release ' note that you are releasing the job before you access the result. It should be the last line
Header: accept-ranges = [bytes]
Header: connection = [Keep-Alive]
Header: content-length = [45085]
Header: content-type = [image/png]
Header: date = [Sun, 13 Oct 2019 08:17:24 GMT]
Header: etag = ["50e0d2b-b01d-52e30ce1c27c2"]
Header: keep-alive = [timeout=2, max=1000]
Header: last-modified = [Wed, 16 Mar 2016 21:00:34 GMT]
Header: server = [Apache]
45085 ' Log($"${lengthl.Get(0)}"$)
 
Last edited:

rbghongade

Active Member
Licensed User
Apologies for the insufficient data!
Here is the code:
B4X:
Sub timer1_tick
    
    Try
        timer1.Enabled=False
        Dim j As HttpJob
        j.Initialize(link1, Me)
        j.Head(link1)
        Wait For (j) JobDone(j As HttpJob)
        If j.Success Then
            Dim headers As Map=j.Response.GetHeaders
            For Each k As String In headers.Keys
                Log($"Header: ${k} = ${headers.Get(k)}"$)
            Next
            Dim lengthl As List = headers.Get("content-length")
            Dim fsval As Int=lengthl.Get(0)
            j.Release


            If fsval>fsval0 Then
                Dim J1 As HttpJob
                J1.Initialize(link1, Me)
                J1.Download(link1)
                Wait For (J1) JobDone(J1 As HttpJob)
                If J1.Success Then
'                    Dim o As OutputStream
                    o = File.OpenOutput(File.DirApp, "datafile.txt", False)
                    File.Copy2(J1.GetInputStream, o)
                    o.Close
                End If
                Log("Job completed: " & J1.Success)
                J1.Release
                fsval0=fsval
                Process'---some processing sub
            Else
                Log("File unchanged!")
                timer1.Enabled=True
            End If
        
        Else
            Log($"Job unsuccessful: ${j.ErrorMessage}"$)
            j.Release
            timer1.Enabled=True
        End If

    Catch
        timer1.Enabled=True
    End Try
End Sub
 
Top