Android Question Connect to remote server returns error

MikeThe1

New Member
Hi All,

Im new to PHP and right now im facing this problem.
I have tried Erel's example to connect to remote DB by creating a database in free hosting webserver.
Erel's example from https://www.b4x.com/android/forum/threads/connect-android-to-mysql-database-tutorial.8339/
I have the PHP file hosted in there also. the table countries already there with 2 rows in it.
but when i run the app in B4A will show error something like this :
** Activity (main) Create, isFirst = true **
** Activity (main) Resume **
*** Service (httputils2service) Create ***
** Service (httputils2service) Start **
Response from server: <html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("fe17c9c74924c548c24c67574444650b");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://www.rumahkostum.unaux.com/countries.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>
Error occurred on line: 58 (Main)
java.lang.RuntimeException: JSON Array expected.
at anywheresoftware.b4a.objects.collections.JSONParser.NextArray(JSONParser.java:62)
at anywheresoftware.b4a.samples.mysql.main._jobdone(main.java:502)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:710)
at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:339)
at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:249)
at java.lang.reflect.Method.invoke(Native Method)
at anywheresoftware.b4a.ShellBA.raiseEvent2(ShellBA.java:139)
at anywheresoftware.b4a.BA$2.run(BA.java:360)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6077)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:866)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)

Im not sure what cause this problems. can someone help ?
thanks in advance.

regards,
 

DonManfred

Expert
Licensed User
Longtime User
What is the URL to this PHP File?
Post some code you are using. Hard to help without seeing your code.
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Response from server: <html><body><script type="text/javascript" src="/aes.js" ></script><script>function toNumbers(d){var e=[];d.replace(/(..)/g,function(d){e.push(parseInt(d,16))});return e}function toHex(){for(var d=[],d=1==arguments.length&&arguments[0].constructor==Array?arguments[0]:arguments,e="",f=0;f<d.length;f++)e+=(16>d[f]?"0":"")+d[f].toString(16);return e.toLowerCase()}var a=toNumbers("f655ba9d09a112d4968c63579db590b4"),b=toNumbers("98344c2eee86c3994890592585b49f80"),c=toNumbers("fe17c9c74924c548c24c67574444650b");document.cookie="__test="+toHex(slowAES.decrypt(c,2,a,b))+"; expires=Thu, 31-Dec-37 23:55:55 GMT; path=/"; location.href="http://www.rumahkostum.unaux.com/countries.php?i=1";</script><noscript>This site requires Javascript to work, please enable Javascript in your browser or use a browser with Javascript support</noscript></body></html>

Looks like your php is using Javascript? this is a Mistake.
 
Upvote 0

tigrot

Well-Known Member
Licensed User
Longtime User
I gave a look to B4A source:
B4X:
Dim job As HttpJob
job.Initialize(JobName, Me)
job.PostString("https://www.b4x.com/android/countries.php", Query)
Should never produce such a result. Maybe there is a little confusion in the web space.
 
Upvote 0

MikeThe1

New Member
the url to the PHP file is http://www.rumahkostum.unaux.com/countries.php
I changed it to http://rumahkostum.unaux.com/countries.php and run the app but it show the same error result.

this is the script in countries.php, connect.php is the connection to the database.
<?php

include("connect.php");

$con = mysqli_connect($databasehost,$databaseusername,$databasepassword, $databasename) or die(mysqli_error($con));
mysqli_set_charset ($con , "utf8");
$query = file_get_contents("php://input");
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysqli_error($con);
}
else
{
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
$res = json_encode($rows);
echo $res;
mysqli_free_result($sth);
}
mysqli_close($con);
?>


I also try another scenario to make sure the PHP file get the result from table, so I change the countries.php like this.
<?php

include("connect.php");

$con = mysqli_connect($databasehost,$databaseusername,$databasepassword, $databasename) or die(mysqli_error($con));
mysqli_set_charset ($con , "utf8");
//$query = file_get_contents("php://input");
$query = "select id, name from countries";
$sth = mysqli_query($con, $query);

if (mysqli_errno($con)) {
header("HTTP/1.1 500 Internal Server Error");
echo $query.'\n';
echo mysqli_error($con);
}
else
{
$rows = array();
while($r = mysqli_fetch_assoc($sth)) {
$rows[] = $r;
}
$res = json_encode($rows);
echo $res;
mysqli_free_result($sth);
}
mysqli_close($con);
?>


then I run it from browser, the result like this.

upload_2018-5-23_16-3-4.png
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Your free host returns a JavaScript that "does something" and then returns the actual page.
Probably some tracking.

You can see it with this code:
B4X:
Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("http://rumahkostum.unaux.com/countries.php?i=1")
   Wait For (j) JobDone(j As HttpJob)
   Log(j.GetString)

It cannot be used as a backend.
 
Upvote 0

MikeThe1

New Member
Your free host returns a JavaScript that "does something" and then returns the actual page.
Probably some tracking.

You can see it with this code:
B4X:
Dim j As HttpJob
   j.Initialize("", Me)
   j.Download("http://rumahkostum.unaux.com/countries.php?i=1")
   Wait For (j) JobDone(j As HttpJob)
   Log(j.GetString)

It cannot be used as a backend.


Ok. thank you all for the help.
 
Upvote 0
Top