Android Question How Do You Pass A Variable From a Php Script to an Activity

Mahares

Expert
Licensed User
Longtime User
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%

Thank you
 

NJDude

Expert
Licensed User
Longtime User
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.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Like @NJDude already stated you need to do a httputilscall to the script and the script need to return the values

PHP:
$res = array();
$res["resultcode"] = 0;
$res["user"] = $user;
$res["password"] = $password;
echo json_encode($res);
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
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
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
I get a string from the Php like this:
it is a json string.
Call http://basic4ppc.com:51042/json/index.html and insert the string in the upper left box and click parse

in return you get a code to parse this

B4X:
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")
 
Upvote 0

Mahares

Expert
Licensed User
Longtime 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
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Thats the meaning of a map. Put keys & value pairs in it and get it back by the key
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
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
@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
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
in this case the "1st array" in the json is a map. So you just can use it, sure.
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?
 
Upvote 0
Top