Android Tutorial For beginners: How to communicate with a server using httputils2 (Part 3: php, MySql & JSON)

Anser

Well-Known Member
Licensed User
Longtime User
Need a help.

To test B4A and MySQL via PHP, today, I purchased a one month web hosting plan (Linux/MySQL/Php)

I want to test B4A and MySQL via PHP.
I have my MySQL Database ready on a remote server on the internet. I can connect to the the MySQL database via HeidiSQL and is working fine.
I have a domain name registered. I have not loaded any webpage on this site. When I checked the webroot, I found the following files are available.

  1. 404.shtml
  2. home.html
  3. layout-style.css
  4. A folder named cgi-bin

Now how do I start using the PHP ?
I have found the php code attached in this thread.

If I copy the php_example.php file available in this thread to my website home (webroot) will it work ?
Do I need to have a index.html on my webroot ?
I don't have any plans to have a website (as of now). My only purpose of this web server is to use the MySQL database via Php

Any help regarding this will be appreciated. The answers to this post may be helpful for other newbies like me.

Regards
Anser
 

Anser

Well-Known Member
Licensed User
Longtime User
I understand that to use php you don't need to have html file and that you can directly call the php file saved in your www root or any other folder

For eg. www.mydomain.com/php/myscripts.php?TestScript

Unfortunately, I am getting the following error

Access denied for user 'testusers'@'xxx.xxx.xxx.xxx' (using password: YES)

Anybody any idea why is it giving such an error ?
I have given full permission to the user testusers

Regards
Anser
 

Anser

Well-Known Member
Licensed User
Longtime User
Hi all,

Got it working.
The error message which I posted in my previous message is related with the Web Hosting and permission. It has nothing to do with the HttpUtils2 and PHP.

Regards
Anser
 

achtrade

Active Member
Licensed User
Longtime User
Great tutorial. could you please post the content of the db.php file ??

thanks.
 

Anser

Well-Known Member
Licensed User
Longtime User
Great tutorial. could you please post the content of the db.php file ??

thanks.
I assume that the following could be the code for db.php
Anyway it is working for me. But I am not aware whether there are other better ways to code the db.php

I am a newbie here.
B4X:
<?php

$host = "YourDomainName_Or_IPaddress";
$db = "YourDatabaseName";
$user = "YourDatabaseUserName";
$pw = "YourDatabaseUserPassword";

?>

Regards
Anser
 

achtrade

Active Member
Licensed User
Longtime User
I assumed that is how the file looks like, but I wanted to be sure how to do it.

thanks.
 

Anser

Well-Known Member
Licensed User
Longtime User
Hi,

How would I retrieve a Varchar and Image data together in JSON format via php

For eg. In my Table, I have ItemName (Varchar) and ItemPicture (BLOB) and the follwing php code retrieves the Image as null
B4X:
 Case "PriceList":
        $catg = mysql_real_escape_string($_GET["Category"]);
        $q = mysql_query("SELECT Item_code, Item_Picture FROM Items_master WHERE Category = '$catg'");
        $rows = array();
        while($r = mysql_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        mysql_free_result($q);
        print json_encode($rows);       
    break;

In my B4A app the above given php code retrieves JSON records with the image data as null.

I understand that base64_encode() should be used in php code, but I don't know how to apply the base64_encode() to the image data alone and then retrieve the Item_code and Item_Picture together.

Any help will be appreciated.

Regards
Anser
 

Anser

Well-Known Member
Licensed User
Longtime User
don't you mind tell us how ?

Here is the code .
B4X:
 Case "PriceList":
        $catg = mysql_real_escape_string($_GET["Category"]);
        $q = mysql_query("SELECT Item_code, Item_Picture FROM Items_master WHERE Category = '$catg'");
        $rows = array();
        while($r = mysql_fetch_assoc($q))
        {
            $rows[] = $r;
        }
        /* Converting the Image column to Base64Encoded in JSON */
       for ($i=0; $i < sizeof($rows); $i++)
        {
            $rows[$i]['Item_Picture']=base64_encode($rows[$i]['Item_Picture']);
        }
        mysql_free_result($q);
        print json_encode($rows);      
    break;

Regards
Anser
 

achtrade

Active Member
Licensed User
Longtime User
Thanks, and how b4a catch this item_picture value ?
 

Anser

Well-Known Member
Licensed User
Longtime User
Thanks, and how b4a catch this item_picture value ?
B4X:
Dim ImageData As String
Dim MyBitmap As Bitmap
Dim ImageBytes() As Byte
Dim su As StringUtils
Dim MyInputStream As InputStream

ImageData = oRecord.Get("Item_Picture") 
ImageBytes = su.DecodeBase64(ImageData)
     
MyInputStream.InitializeFromBytesArray(ImageBytes,0,ImageBytes.Length)
MyBitmap.Initialize2(MyInputStream)
MyInputStream.Close

Hope this will be helpful and saves time for newbies like me.

Regards
Anser
 

Anser

Well-Known Member
Licensed User
Longtime User
Hi aeric,

Please keep in mind that according to me this is not an elegant solution due to the following reasons.

  1. Php retrieves MySQL BLOB data first via SQL Query.
  2. Php loops thru the data to convert the BLOB data TO Baase64 encoded data.
  3. Inside B4A, the JSON data received as response from the server is processed again to convert it to Imagedata
All the above may have an impact on the performance of our app.

The process number 2 in the above list can be avoided if we use MySQL ver 5.6.1 OR above

From MySQL ver 5.6.1 onwards we can retrieve BLOB data as Base64 Encoded string directly via SQL without the need of Php converting it thru a loop. Unfortunately many Hosting service providers still use MySQL version 5.5 only. Even though MySQL version 5.7 is available, they consider 5.5 as the most stable version.

Hope this is helpful to others.

Regards
Anser
 

aidymp

Well-Known Member
Licensed User
Longtime User
Hi, This all works as expected for me, BUT!

The App I want to use the code with is using the 3 modules "DownloadService.bas" "HttpJob.bas" "HttpUtils2Service.bas" As the whole point of my app is to download large files, and i use these a lot. when i use the code with these 3 modules instead of the "HttpUtils2" lib, i get the following error

** Activity (main) Resume **
Installing file.
** Activity (main) Pause, UserClosed = false **
PackageAdded: package:com.FireMatic.kodi
** Activity (main) Create, isFirst = true **
mac = 58:12:43:a7:b7:54
MAC = 58:12:43:A7:B7:54
** Activity (main) Resume **
** Service (httputils2service) Create **
** Service (httputils2service) Start **
httputils2service_hc_responsesuccess (java line: 148)
java.lang.ClassCastException: java.lang.Object cannot be cast to com.FireMatic.kodi.downloadservice$_jobtag
at com.FireMatic.kodi.httputils2service._hc_responsesuccess(httputils2service.java:148)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
at anywheresoftware.b4a.BA$3.run(BA.java:332)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4430)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
at dalvik.system.NativeStart.main(Native Method)
java.lang.RuntimeException: java.lang.ClassCastException: java.lang.Object cannot be cast to com.FireMatic.kodi.downloadservice$_jobtag
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:213)
at anywheresoftware.b4a.BA$3.run(BA.java:332)
at android.os.Handler.handleCallback(Handler.java:605)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4430)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:559)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: java.lang.Object cannot be cast to com.FireMatic.kodi.downloadservice$_jobtag
at com.FireMatic.kodi.httputils2service._hc_responsesuccess(httputils2service.java:148)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at anywheresoftware.b4a.BA.raiseEvent2(BA.java:187)
... 10 more
** Activity (main) Resume **

Obviously I have tried it removing te modules and using the lib, but that is not what I want!

How can I get the download2 function to work correctly with these modules?

Thanks

Aidy
 

alienhunter

Active Member
Licensed User
Longtime User


Hi what is oRecord.get ? a list ? Map ?
thanks AH
 
Cookies are required to use this site. You must accept them to continue using the site. Learn more…