Android Tutorial Trial Tutorial

timo

Active Member
Licensed User
Longtime User
this piece of code
B4X:
'Optional: Remove '[' and ']' from the response received from my server:-> array[{jsonObject}]
 Dim tmp As String
 Dim lungh As Int
 tmp=result
 lungh= tmp.Length
 tmp= tmp.SubString2(1,lungh-1) 'cut first+last char
 result=tmp
 '-----------------------------------------------------
is for putting [] away. If you don't do it, i think that your response begins with an array (list), not an object(map) and next object gets error.
 
Last edited:

Fox

Active Member
Licensed User
Longtime User
the imei of the emulator should be: "000000000000000"; verify in your Mysql records if it is so.


the imei of emulator is zero yes. so the solution for this would be :sign0163:
 

Fox

Active Member
Licensed User
Longtime User
OK. Your code is writing right in the MSql DB. To see if the response is also correct, you can maybe post PHP and B4A code


php code:

B4X:
<?php
$hst  = "localhost";
$db = "xxxxxxxxxx";
$usr = "xxxxxxxx";
$pwd = "xxxxxxxx";
$imi= $_GET['imei'];

$conn_DB=mysql_connect($hst,$usr,$pwd) or die(mysql_error());
mysql_select_db($db) or die(mysql_error());

$query="SELECT * FROM gtrial WHERE imei ='$imi'";
$result=mysql_query($query,$conn_DB) or die(mysql_error());
$numrows=mysql_num_rows($result);

If ($numrows == 0) {
$restano=3;
$queryINS="INSERT INTO gtrial (imei,startsleft) VALUES ('$imi',$restano)";
mysql_query($queryINS);
                    }
else if ($numrows > 0) {
//never > 1 anyway
$restano=mysql_result($result,0,'startsleft'); 
        if($restano>0)         {
            $restano=$restano -1;
$queryUPDT="UPDATE gtrial SET startsleft = $restano WHERE imei='$imi'";
mysql_query($queryUPDT);
                            }
                        }
                        
$queryOUT="SELECT * FROM gtrial WHERE imei ='$imi'";
$output=mysql_query($queryOUT);    

$rows = array();
    while($r = mysql_fetch_assoc($output)) {
        $rows[] = $r;
    }
    print json_encode($rows);
?>

b4a code:

B4X:
Sub Process_Globals
Dim httpC As HttpClient
'put here to obfuscate user,password,php.name and hosting [->Release(obfuscated)]
Dim indirizzo As String : indirizzo= "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx?imei="
'Dim parcheggio As String: parcheggio="http://www.yourSite.com/" '(no more needed here)
End Sub

Sub Globals
Dim p As PhoneId
Dim deviceID As String
deviceID = p.GetDeviceId
Dim m As Map

End Sub

Sub Activity_Create(FirstTime As Boolean)
deviceID = p.GetDeviceId
Log(deviceID)
If FirstTime Then
httpC.Initialize("httpC")
End If
checkTrialsRemaining
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
Activity.Finish
End Sub

Sub checkTrialsRemaining
Dim req As HttpRequest
req.InitializeGet(indirizzo&deviceID)
httpC.Execute(req,1)
ProgressDialogShow("Validation ...")

End Sub


Sub httpC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Log("Response: "&Response) '<-add this line
Dim result As String
Dim JSON As JSONParser
Dim restOpen As Int
result = Response.GetString("UTF8")
Log(result)

'Optional: Remove '[' and ']' from the response received from my server:-> array[{jsonObject}]
Dim tmp As String
Dim lungh As Int
tmp=result
lungh= tmp.Length
tmp= tmp.SubString2(1,lungh-1) 'cut first+last char
result=tmp
'-----------------------------------------------------
Log(result)

JSON.Initialize(result)
Log(result)
Dim m As Map
m=JSON.NextArray

restOpen=m.Get("startsleft")
ProgressDialogHide

If restOpen=0 Then
Msgbox("Trial expired"," Trial")
Response.Release
Activity.Finish
Else
If restOpen > 1 Then
Msgbox((restOpen - 1)&" out of 3 openings remaining"," Trial")
Else
Msgbox("Last opening of 3"," Trial")
End If
End If
Response.Release
End Sub

Sub httpC_ResponseError (Response As HttpResponse, Reason As String, StatusCode As Int, TaskId As Int)
'no Reason shown in order not to give the user informations about hosting and php names:
ProgressDialogHide
Msgbox("Connection error. Try later again."," Trial")

If Response <> Null Then
Log("Resp.not Null")
Response.Release
End If
'If no web connection is available, Response is naturally Null and you don't have to release a Null object
ExitApplication
End Sub
 
Last edited:

timo

Active Member
Licensed User
Longtime User
Seems correct. Just one thing: with my provider if i use:
B4X:
<?php$hst  = "localhost";
...
it doesn't work: I have to put the hole addres. Anyway, if your app writes on the db, i assume it works in your case.

You say you have an error in responsesuccess on the device. Try if the response you receive (by adding a MsgBox(Response,"")' in that sub instead of Log) is
different:
1 - By runningi in the emulator (where it seems to work)
2 - and then in the phone (when not attached to PC)
If in case 2 no strange carachter is added to the response I don't know why it gives you error. Post eventually the text of the response.
Another question: When you run it in the emulator x times: do you receive the correct countdown? (2-1-Trial expired?)
 
Last edited:

Fox

Active Member
Licensed User
Longtime User

ok i have tested it with the message box on phone i get this result:


anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper@4621cf68

and in emulator this:

anywheresoftware.b4a.http.HttpClientWrapper$HttpResponeWrapper@44ee46f0

but i think thats are no json results

and yes the app working in emulator 2,1 trial expired!
 

Fox

Active Member
Licensed User
Longtime User
thanks all you guys for your help but i will giveup at this chapter sorry but i have no clue to solve it... i don't understand this..
 

timo

Active Member
Licensed User
Longtime User
Last thing, if you want;
add this line just after "JSON.Initialize(result)":

msgbox("JSON STRING : "&result,"")

to see your json string.
This should now begin with "{" and end with "}"

b4a code:

B4X:
Sub httpC_ResponseSuccess (Response As HttpResponse, TaskId As Int)
Dim result As String
Dim JSON As JSONParser
Dim restOpen As Int
result = Response.GetString("UTF8")

'Optional: Remove '[' and ']' from the response received from my server:-> array[{jsonObject}]
Dim tmp As String
Dim lungh As Int
tmp=result
lungh= tmp.Length
tmp= tmp.SubString2(1,lungh-1) 'cut first+last char
result=tmp
'-----------------------------------------------------

JSON.Initialize(result)

msgbox("JSON STRING : "&result,"")'------------------------------->add this

'...
'...etc...
End Sub
 
Last edited:
Cookies are required to use this site. You must accept them to continue using the site. Learn more…