Android Question HttpServer and download2 function question

Tim_Pennard

Member
Licensed User
Hi, I’m a novice with the okHttpUtils and the HttpServer libraries, but hoping for advice please. (the libraries seem to be excellent and just what I need - if I knew how to use them!)

I would like to request a series of variables (e.g.Key1, Key2) from another Android device running the HttpServer library. If I use 'download2', what should the server code look like to send the data back, and what code is needed on the requesting device to recieve it please? I tried using Response.SendString("Key1=Value1&Key2=Value2") on the server, and then Job.GetString on the requesting device. This works but then requires me to disassemble the string manually. I'm suspecting there is a neater way already built into the functions of the library that would give me the variables directly?

Many thanks for any help you can offer.

Code extracts for info

On the requesting device, I added ‘GetData’ to the URI and then add ‘User1’, ‘User2’ or ‘User3’ to tell the server which device is requesting information. Then on the server I look for URIs starting with ‘GetData’ and send back different data based on with user made the request.

Code on requesting device

Sub Button_Get_Click
Dim job4 As HttpJob
job4.Initialize("Job4", Me)
job4.Download2("http://192.168.0.45:5555/GetData/User1",Array As String("Key1", "value1", "Key2", "value2"))
End Sub

Sub JobDone(Job As HttpJob)
If Job.Success = True Then
Dim s As String
s = Job.GetString
Label1.Text = s
End If
End Sub

Code on server

Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
Select True
Case Request.RequestURI.StartsWith("/GetData/")
HandleGetData(Request,Response)
End Select
End Sub

Sub HandleGetData (Request As ServletRequest,Response As ServletResponse)
Dim UserName As String = DecodePath(Request.RequestURI.SubString("/GetData/".Length))
Response.SetContentType("text/html")
Select True
Case UserName = "User1"
Response.SendString("Key1=Value1&Key2=Value2")
End Select
End Sub
 

walterf25

Expert
Licensed User
Longtime User
Hi, I’m a novice with the okHttpUtils and the HttpServer libraries, but hoping for advice please. (the libraries seem to be excellent and just what I need - if I knew how to use them!)

I would like to request a series of variables (e.g.Key1, Key2) from another Android device running the HttpServer library. If I use 'download2', what should the server code look like to send the data back, and what code is needed on the requesting device to recieve it please? I tried using Response.SendString("Key1=Value1&Key2=Value2") on the server, and then Job.GetString on the requesting device. This works but then requires me to disassemble the string manually. I'm suspecting there is a neater way already built into the functions of the library that would give me the variables directly?

Many thanks for any help you can offer.

Code extracts for info

On the requesting device, I added ‘GetData’ to the URI and then add ‘User1’, ‘User2’ or ‘User3’ to tell the server which device is requesting information. Then on the server I look for URIs starting with ‘GetData’ and send back different data based on with user made the request.

Code on requesting device

Sub Button_Get_Click
Dim job4 As HttpJob
job4.Initialize("Job4", Me)
job4.Download2("http://192.168.0.45:5555/GetData/User1",Array As String("Key1", "value1", "Key2", "value2"))
End Sub

Sub JobDone(Job As HttpJob)
If Job.Success = True Then
Dim s As String
s = Job.GetString
Label1.Text = s
End If
End Sub

Code on server

Sub Server_HandleRequest (Request As ServletRequest, Response As ServletResponse)
Select True
Case Request.RequestURI.StartsWith("/GetData/")
HandleGetData(Request,Response)
End Select
End Sub

Sub HandleGetData (Request As ServletRequest,Response As ServletResponse)
Dim UserName As String = DecodePath(Request.RequestURI.SubString("/GetData/".Length))
Response.SetContentType("text/html")
Select True
Case UserName = "User1"
Response.SendString("Key1=Value1&Key2=Value2")
End Select
End Sub
Off the top of my head, you can use the JSON library to create a json string with the variables and values you want to send, you can parse the json response when you receive it and extract the values you need.

Regards,
Walter
 
Upvote 0

Tim_Pennard

Member
Licensed User
Walter, many thanks, that's really helpful.

If I extract the parameters from the string myself then think this method would also work with the basic 'download' rather than 'download2'. i.e. just send a GET request, job.download("http://192.168.0.45:5555/GetData") and then send back a string.

I was hoping that by using 'download2', e.g. job.download2("http://192.168.0.45:5555/GetData, Array As String("Key1", "Value1", "Key2", "Value2")) and specifying keys and value names in the request then there was a way to easier way extract them?

Any thoughts from you or others much appreciated.
Tim
 
Upvote 0
Top