Android Question httpResponse Asynchronously and JSON

Discussion in 'Android Questions' started by frutuopa, May 9, 2015.

  1. frutuopa

    frutuopa Member Licensed User

    Hello

    I have B4A 2.22

    Before i have a code like this

    Sub hcimport_ResponseSuccess (Response As HttpResponse, tabela As Int)
    Dim res As String
    'Msgbox("sucesso","")
    res = Response.GetString("UTF8")
    Log("Response from server: " & res)
    Dim parser As JSONParser
    parser.Initialize(res)
    Dim dadosget As List

    dadosget = parser.NextArray 'returns a list with maps
    'sql1.BeginTransaction
    ..................................................................................................


    Now because GetString
    is deprecated and will not work properly on Android 4+ device.
    i try to use Use GetAsynchronously instead.

    i do that:

    Sub Process_Globals
    Dim my_buffer As OutputStream
    end sub

    Sub hcimport_ResponseSuccess (Response As HttpResponse, tabela As Int)
    Dim Res As String
    Dim TaskId As Int
    Msgbox("sucesso","")
    my_buffer.InitializeToBytesArray(5000) ' I expect less than 2000 bytes here
    Msgbox("sucesso","")
    Response.GetAsynchronously("Response",my_buffer,True,TaskId)
    End Sub

    Sub Response_StreamFinish (Success As Boolean, TaskId As Int)

    Msgbox (my_buffer,"")
    Log("Response from server: " & my_buffer)
    Dim tabela As Int

    Dim parser As JSONParser
    parser.Initialize(my_buffer)

    Dim dadosget As List
    Dim conty,contx As Int
    'Dim tid As Int
    'Dim tcodigo As Int
    'Dim tnome As String
    'Dim tnomereduzido As String
    Dim vsql As String
    Dim stabela As String
    tabela=1


    dadosget = parser.NextArray 'returns a list with maps

    ..............................................................................


    i have an error "JSON Array expected".....
    I undestand because the first line begin with (ByteArrayOutputStream)

    .............................................................................................................
    Response from server: (ByteArrayOutputStream)
    [{"id":"2","codigo":"1001","nome":"Carter Chapeu 003 F1A","nomereduzido":"CC-003"},{"id":"69","codigo":"1068","nome":"CARTER CILINDRO 180","nomereduzido":"CC-180"},{"id":"90","codigo":"1087","nome":"CARTER CILINDRO 220","nomereduzido":"CC-220"}]


    CAN YOU HELP ME

    Thanks
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Please use [code]code here...[/code] tags when posting code.

    You should use HttpUtils2. If the library is not compatible with B4A v2.2 then you can use the source code.

    Though I recommend you to upgrade to a newer version as you will encounter other issues with new Android devices.
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice