He estado toda la tarde trasteando con okHttpClient okHttpRequest OkHttpResponse. He probado varios ejemplos, incluso emplee HttpJob, parecía sencillo pero nada funciona.
Trate de emplear este código pero es para descargar archivos. Por lo que toca bajar el texto del json a un archivo y luego abrir el archivo para recuperar el json.
B4X:
Sub DownloadApkDim hc As OkHttpClient
hc.InitializeAcceptAll("hc")Dim req As OkHttpRequest
req.InitializeGet("http://xxxxxxxxxxxxxxxxxx")
hc.Execute(req, 1000)
End Sub
Sub hc_ResponseSuccess (Response As OkHttpResponse, TaskId As Int)
Log("apk size: " & Response.ContentLength)Log("response code: " & Response.StatusCode)
Response.GetAsynchronously("res", File.OpenOutput(File.DirRootExternal&"/Download", "Observer.apk", False), True, TaskId)
End Sub
¿Alguien sabe como enviar recibir un texto json con okHttpClient okHttpRequest OkHttpResponse? El ejemplo de Erel no lo entiendo. Ni con traductor lo entendí.
Depende bastante de la pagina a la que estas intentado mandar/recibir la informacion.
asi es como se hace un post y se recupera la respuesta (para mi caso especifico):
(requieres B4A 7 o superior)
B4X:
Dim js As JSONGenerator
js.Initialize(request)
Log(js.ToPrettyString(5))
Dim httpjob As HttpJob
httpjob.Initialize("request",Me)
httpjob.PostString("https://labws.gia.edu/ReportCheck/processrequest.do",js.ToString)
httpjob.GetRequest.SetContentType("application/json")
wait for (httpjob) JobDone (job1 As HttpJob)
' Log(job1.GetString)
Log(job1.Success)
Log(job1.GetString)
Dim jsParser As JSONParser
jsParser.Initialize(job1.GetString)
Depende bastante de la pagina a la que estas intentado mandar/recibir la informacion.
asi es como se hace un post y se recupera la respuesta (para mi caso especifico):
(requieres B4A 7 o superior)
B4X:
Dim js As JSONGenerator
js.Initialize(request)
Log(js.ToPrettyString(5))
Dim httpjob As HttpJob
httpjob.Initialize("request",Me)
httpjob.PostString("https://labws.gia.edu/ReportCheck/processrequest.do",js.ToString)
httpjob.GetRequest.SetContentType("application/json")
wait for (httpjob) JobDone (job1 As HttpJob)
' Log(job1.GetString)
Log(job1.Success)
Log(job1.GetString)
Dim jsParser As JSONParser
jsParser.Initialize(job1.GetString)
De todos modos si solo tienes la version 6.3 puedes hacerlo asi:
B4X:
private sub sendinfo
Dim httpjob As HttpJob
httpjob.Initialize("request",Me)
httpjob.PostString("https://labws.gia.edu/ReportCheck/processrequest.do",js.ToString)
httpjob.GetRequest.SetContentType("application/json")
end sub
private sub jobDone (job1 as HttpJob)
Log(job1.Success)
Log(job1.GetString)
Dim jsParser As JSONParser
jsParser.Initialize(job1.GetString)
End sub
private sub sendinfo
Dim httpjob As HttpJob
httpjob.Initialize("request",Me)
httpjob.Download("https://bittrex.com/api/v1.1/public/getticker?market=btc-ltc")
end sub
private sub jobDone (job1 as HttpJob)
Log(job1.Success)
Log(job1.GetString)
Dim jsParser As JSONParser
jsParser.Initialize(job1.GetString)
End sub
El programa compiló y corrió sin problemas en modo 'Release'
El código completo (Requiere B4A 7.1 o superior + Java 8 SDK)
B4X:
#Region Project Attributes
#ApplicationLabel: Bittrex 1
#VersionCode: 1
#VersionName:
'SupportedOrientations possible values: unspecified, landscape or portrait.
#SupportedOrientations: unspecified
#CanInstallToExternalStorage: False
#End Region
#Region Activity Attributes
#FullScreen: False
#IncludeTitle: True
#End Region
Sub Process_Globals
'These global variables will be declared once when the application starts.
'These variables can be accessed from all modules.
End Sub
Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.
'JSON
Dim json As JSONParser
Private ButtonGetBTC_LTC As Button
Private EditTextOut As EditText
Private EditTextJson As EditText
End Sub
Sub Activity_Create(FirstTime As Boolean)
'Do not forget to load the layout file created with the visual designer. For example:
Activity.LoadLayout("Layout1")
End Sub
Sub Activity_Resume
End Sub
Sub Activity_Pause (UserClosed As Boolean)
End Sub
Sub ButtonGetBTC_LTC_Click
Dim URL As String
URL = "https://bittrex.com/api/v1.1/public/getticker?market=BTC-LTC"
Dim httpjob As HttpJob
httpjob.Initialize("request",Me)
httpjob.Download(URL)
End Sub
private Sub jobDone (job1 As HttpJob)
Log(job1.Success)
Log(job1.GetString)
EditTextJson.Text = job1.GetString
getticker(job1.GetString)
End Sub
Sub getticker(Recibido As String)
Dim Success As String
Dim Bid As Double
Dim Ask As Double
Dim Last As Double
Dim m As Map
Dim json As JSONParser
json.Initialize(Recibido)
'Recuperamos el Objeto Raiz
m = json.NextObject
Success = m.Get("success") 'Recuperamos 'success'
m = m.Get("result")
Bid = m.get("Bid") 'Recuperamos 'Bid'
Ask = m.get("Ask") 'Recuperamos 'Ask'
Last = m.get("Last") 'Recuperamos 'Last'
'Mostramos los Datos
EditTextOut.Text = "Success: " & Success & CRLF
EditTextOut.Text = EditTextOut.Text & "Bid: " & Bid & CRLF
EditTextOut.Text = EditTextOut.Text & "Ask: " & Ask & CRLF
EditTextOut.Text = EditTextOut.Text & "Last: " & Last & CRLF
End Sub