In the php script I have:
<?php
$User="b4auser";
$Password="12345%";
?>
In the Activity, I want to extract those 2 values from the script:
Dim strUser as String = the corresponding value from php script which is b4auser
Dim strPwrd as String = the corresponding value from php script which is 12345%
In order to do that, you will have to send a request to the PHP and get the response, your PHP must have a "echo $user . "+" . $password;" (that's of course a very crude sample but you get the idea).
You can use the HTTP lib or HTTPUtils to achieve that.
Thank you guys for holding my hand as my Php knowledge is virtually nill. I get a string from the Php like this:
B4X:
{"user":"b4auser ","password":"12345%"}
But the parsed string shows the map values reversed. b4auser should display first before 12345%. Am I taking the right approach here or am I way behind the curve still. Thank you.
B4X:
Dim m As Map
m.Initialize
m=parser.NextObject
For i=0 To m.Size-1
' Log(i & ": " & m.GetKeyAt(i) & "=" & m.GetValueAt(i))
Log(m.GetValueAt(i))
Next
Dim v0,v1 As String
v0= m.GetValueAt(0) :Log(v0) 'displays: 12345%
v1= m.GetValueAt(1) :Log(v1) 'displays: b4auser
Dim parser As JSONParser
parser.Initialize(<text>)
Dim root As Map = parser.NextObject
Dim password As String = root.Get("password")
Dim user As String = root.Get("user")
@DonManfred: That is a great tool, the json tree. It worked quite well. Thank you. @NJDude: I am not sure what you wanted me to do. I did the below and got the same reversed result as before, Thank you:
B4X:
For i=0 To m.Size-1
Log(m.Get(m.GetKeyAt(i)))
Next
Dim v0,v1 As String
v0= m.Get(m.GetKeyAt(0)) :Log(v0) 'displays: 12345%
v1= m.Get(m.GetKeyAt(1)) :Log(v1) 'displays: b4auser
I am familiar with maps, but what puzzles me is,why are the values reversed when I use the map code I used, although the json tree explained by @DonManfred solved my problem: How can you change the code I have here without going through the jason tree and give me the correct values in the correct order?
B4X:
Dim m As Map
m.Initialize
m=parser.NextObject
For i=0 To m.Size-1
Log(m.GetValueAt(i))
Next
Dim v0,v1 AsString
v0= m.GetValueAt(0) :Log(v0) 'displays: 12345%
v1= m.GetValueAt(1) :Log(v1) 'displays: b4auser
the order of entries in a json map is not garanteed due to some native jsonparsing thingy
Access the values like in the json-example and you are fine. Dont relay on a specific order.
@DonManfred and @NJDude:
Ok, that is what I wanted to hear: the order of the variables is not reliable. Therefore, how would you parse the json string without the use of a tree, by simply using a map in this case or are we out of luck?
Thank you
Please take a look at the json string:
{"user":"b4auser ","password":"12345%"}
But the map code I use displays 12345% before it displays b4auser . If I am relying exclusively on the map code of the first array and assume the Json tree is not available, why are the values reversed?
like i already stated. The ORDER inside a JSON is NOT garanteed. The jsonparser does create a map out of the json-array. And this map then also does not have a garanteed order.
If you know that there is a user and a password inside the map but you dont know the order.
Use a list with user and password and while iterating through that list you get the correcpondending key from the map.