Android Question Http > OkHttp Library Migration Issue

RichardN

Well-Known Member
Licensed User
Longtime User
This code to cache a web page worked fine under the original Http library but after rewriting for OkHttp I am getting the Sub Http_ResponseError' triggered every time returning 'Internal Server Error'.

I guess this may be something to do with the execution/formation of the Get Request URL but I can't see what it should be..... Any ideas?

B4X:
Sub Activity_Create(FirstTime As Boolean)

   Dim OkClient As OkHttpClient               'Global
   OkClient.Initialize("Http")
   HttpRequestId = 1000                         'Global

End Sub

Sub Button_Click            'Initiates URL Get

    Dim URL As String = "http://www.aviationweather.gov/adds/tafs/index.php?station_ids=EGLL&submit_both=a"
    Dim Request As OkHttpRequest
    Request.InitializeGet(URL)
    OkClient.Execute(Request,HttpRequestID)

End Sub

Sub Http_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
    Dim out As OutputStream
    out.InitializeToBytesArray(0)
    Response.GetAsynchronously("Result",File.OpenOutput(File.DirInternalCache, "page.html", False), True, HttpRequestID)  
End Sub


Sub Http_ResponseError (Response As OkHttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
    Msgbox("Unable To Download Data" & CRLF & Reason,"Station Weather")
End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
You should use OkHttpUtils.

This code works:
B4X:
ub Activity_Create(FirstTime As Boolean)
   Dim j As HttpJob
   j.Initialize("j", Me)
   j.Download("http://www.aviationweather.gov/adds/tafs/index?station_ids=EGLL&submit_both=a")
End Sub

Sub JobDone(j As HttpJob)
   If j.Success Then
     Log(j.GetString)
   Else
     Log(j.ErrorMessage)
   End If
   j.Release
End Sub
 
Upvote 0
Top