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

Discussion in 'Android Questions' started by Mahares, Feb 26, 2015.

  1. Mahares

    Mahares Well Known Member Licensed 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
     
  2. Reviewnow

    Reviewnow Active Member Licensed User

    thedesolatesoul likes this.
  3. NJDude

    NJDude Expert Licensed 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: Feb 26, 2015
    thedesolatesoul likes this.
  4. DonManfred

    DonManfred Expert Licensed 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);
     
    thedesolatesoul and KMatle like this.
  5. Mahares

    Mahares Well Known Member Licensed User

    Thank you guys for holding my hand as my Php knowledge is virtually nill. I get a string from the Php like this:
    Code:
    {"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.
    Code:
    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
     
    thedesolatesoul likes this.
  6. NJDude

    NJDude Expert Licensed User

    Don't use the values but the KEY names, that way you can get 'username' and 'password'.
     
    thedesolatesoul likes this.
  7. DonManfred

    DonManfred Expert Licensed User

    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

    Code:
    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")
     
  8. Mahares

    Mahares Well Known Member Licensed 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:
    Code:
    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
     
    thedesolatesoul likes this.
  9. KMatle

    KMatle Expert Licensed User

    You can write m.get("password") to get 12345%. You don't need m.Get(m.GetKeyAt(0)).

    Thats the meaning of a map. Put keys & value pairs in it and get it back by the key
     
    thedesolatesoul likes this.
  10. RandomCoder

    RandomCoder Well-Known Member Licensed User

    NJDude was suggesting to do it exactly the same way Manfred has demonstrated.

    @KMatle won me to the post ;)
     
    thedesolatesoul and KMatle like this.
  11. Mahares

    Mahares Well Known Member Licensed User

    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?
    Code:
    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
     
    thedesolatesoul likes this.
  12. NJDude

    NJDude Expert Licensed User

    Mahares, the order of the items in a map (in this case holding a JSON string) are not necessarily always in order, that's why you have to parse them.
     
  13. DonManfred

    DonManfred Expert Licensed User

    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.
     
  14. Mahares

    Mahares Well Known Member Licensed 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
     
  15. DonManfred

    DonManfred Expert Licensed User

    in this case the "1st array" in the json is a map. So you just can use it, sure.
     
  16. Mahares

    Mahares Well Known Member Licensed User

    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?
     
  17. DonManfred

    DonManfred Expert Licensed User

    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.
     
  18. DonManfred

    DonManfred Expert Licensed User

    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.
     
    thedesolatesoul likes this.
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