Android Question http PostString / Paramlist Problem

FrankDev

Active Member
Licensed User
Longtime User
Hello

I have a (small) problem with HttpPost when passing parameters.
So far I always did this with a parameter list.
Actually this works always except I have a '&' character in a variable. Then I get errors of course.
I always mask the & character and it works that way.
But this is not reasonable.

But I don't want to put the whole thing into a Json string,
because I want to receive the whole on the PHP page as shown below.

$username = isset($_POST['username']) ? $_POST['username'] : '?

best regards
Frank


B4X:
Dim post_params As String = "table=" & table & "&username=" & username
    
Dim read_locations As HttpJob
read_locations.Initialize("read_locations",Me)
Dim URL As String = Server & "/xxx.php"
read_locations.PostString(URL,post_params)
    
Wait For (read_locations) JobDone(read_locations As HttpJob)
If read_locations.Success Then
    'do something   
Else
    'do something else   
End If

read_locations.Release
 

mc73

Well-Known Member
Licensed User
Longtime User
Try to encode your params:
B4X:
private strUtils as stringUtils
Private post_params As String = "table=" & strUtils.EncodeUrl(table,"UTF8") & "&username=" & strUtils.EncodeUrl(username,"UTF8")
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
As the name says it's a single string. I put all the parms in a map (or list with maps if you have multiple "rows") and convert it to a Json string (Base64).

The receiver (e.g.) php has the same logic (here a map is an array). See my php examples.

Big plus: As I encrypt everything, it's just a simple string.
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
ok,

I wanted to keep it simple.
Actually, I already put the parameters into a Json string.
(at the post)
I still have to mask the '&', otherwise it won't work.


B4X:
Dim JSONList As List
JSONList.Initialize

Dim mapData As Map
mapData.Initialize

Dim text As String = dbCursor.GetString("text").trim
text = text.Replace("&","\u0026")
mapData.put("text",text)

mapData.put("media",dbCursor.GetInt("media"))
mapData.put("time",dbCursor.GetString("time"))

JSONList.add(mapData)

Dim JSONGen1 As JSONGenerator
JSONGen1.Initialize2(JSONList)

Dim JSONstring As String
JSONstring = JSONGen1.ToString

.......
post_params = "id=" & id & "&MyJSON=" & JSONstring
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Why are you not using @mc73's solution? You have to URL encode your values. Right now it's only the &, but what if it is something else, then your code
B4X:
text = text.Replace("&","\u0026")
will fail, since it is only taking care of encoding one element of the possible elements that need to be encoded.
 
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
this was my previous solution
B4X:
text = text.Replace("&","\u0026")


when I have everything in 'JSONstring
would the following be possible?
B4X:
post_params = "id=" & id & "&MyJSON=" & strUtils.EncodeUrl(JSONstring,"UTF8")
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
Upvote 0

FrankDev

Active Member
Licensed User
Longtime User
Hello,

thanks a lot for your support!!

@OliverA
At which place in my source would I have to use it now ?

UrlEncodeMap(aMap)

I do understand the principle. But the conversion from map to string and then the whole thing in Json is not so clear to me yet

Best regards
Stay healthy!


B4X:
Dim JSONList As List
JSONList.Initialize

Dim mapData As Map
mapData.Initialize

Dim text As String = dbCursor.GetString("text").trim
text = text.Replace("&","\u0026")
mapData.put("text",text)

mapData.put("media",dbCursor.GetInt("media"))
mapData.put("time",dbCursor.GetString("time"))

JSONList.add(mapData)

Dim JSONGen1 As JSONGenerator
JSONGen1.Initialize2(JSONList)

Dim JSONstring As String
JSONstring = JSONGen1.ToString

.......
post_params = "id=" & id & "&MyJSON=" & JSONstring
 
Upvote 0

OliverA

Expert
Licensed User
Longtime User
But the conversion from map to string and then the whole thing in Json is not so clear to me yet
1) Did you mean string to map?
2) Instead of hand-coding JSON, you can let B4X do it for you via the JSONGenerator. A B4X map will get encoded to a JSON map and a B4X list will get encoded to a JSON list.

At which place in my source would I have to use it now ?
UrlEncodeMap(aMap)
In your case, when you assign your field-value pairs to the post_params variable. So instead of
B4X:
post_params = "id=" & id & "&MyJSON=" & JSONstring
you can do
B4X:
post_params = UrlEncodeMap(CreateMap("id" : id, "MyJSON" : JSONString))

Note:
Since UrlEncodeMap takes care of encoding your JSONString, this line
B4X:
text = text.Replace("&","\u0026")
is not needed anymore

But I don't want to put the whole thing into a Json string,
Then why are you putting everything into a JSON string? If all you want todo is pass your data as parameters, then you can just do
B4X:
Dim mapData As Map
mapData.Initialize
mapData.put("text", dbCursor.GetString("text").trim)
mapData.put("media",dbCursor.GetInt("media"))
mapData.put("time",dbCursor.GetString("time"))
mapData.put("id", id)

post_params = UrlEncodeMap(mapData)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…