0

so I just created my first website and I'm using php file to communicate with my android app.. The android app is requesting for alot of data and the php is sending it back to it.

The only way I have found yet to send data from php to the android is via echo

I have alot of strings and pictures in my data and I seperate them all with ugly "BUFFER1" style buffers between each data as it all comes out eventually as a long string combined together..

Here is an example:

<?php

    $input_number = $_POST["In_Number"];

    $path = ("Folder/" . $input_number);
    $Data1 = file_get_contents($path . "/data1.txt");
    $Data2 = file_get_contents($path . "/data2.txt");
    $Data3 = file_get_contents($path . "/data3.txt");   
    echo $Data1;
    echo "STREAMING_BUFFER";
    echo $Data2;
    echo "STREAMING_BUFFER";
    echo $Data3;
    echo "STREAMING_BUFFER";

    ...

    // sending the pictures now (encoded..) 
    $pic_folder_path = ("Folder/" . $input_number . "/pics/");
    $pics_pathes = array_diff(scandir($pic_folder_path), array('..', '.'));
    foreach($pics_pathes as $pic_path){
        $image = file_get_contents($pic_folder_path . $pic_path);
        echo base64_encode($image);
        echo "PICTURE_BUFFER";
    }


    echo "STREAMING_BUFFER";

    // END
?>

The android to php communicate is much simpler.. I use the POST method to send <key,value> pairs and I can easily read them that way..

<?php
    $name = $_POST["name"];
    $image = $_POST["image"];

    $decodedImage = base64_decode("$image");
    file_put_contents("pictures/" . $name . ".JPG", $decodedImage);
?>

The name is easily separated from the image data.. and sure I could seperate that way much more data

Is there any similar way for the php to android way?

Thanks.

Yarden Cohen
  • 584
  • 2
  • 4
  • 19
  • 1
    Show the code you are using. Then (and only then) will someone be able to suggest an improvement. – kainaw May 20 '16 at 16:21
  • Provided it's your first website maybe it's not too late to move to a better web technology, e.g. Django? It's good for beginners, and guided by their [docs](https://docs.djangoproject.com/en/1.9/intro/overview/) you will create a nice REST service to communicate with mobile devices. – sonderlain May 20 '16 at 16:27
  • 1
    added code example.. – Yarden Cohen May 20 '16 at 16:31
  • 1
    so if you want to deliver data from php website to android, you can build a restful api that delivers json format content, and build a json object parser in your andriod to fetch the data, this is the normal way to go. there is a lot online doc covering this. – Maytham Fahmi May 20 '16 at 16:38
  • There is nothing wrong with echo. Output only one type of data per request. If you want multiple data in one request, use XML, json or similar format. – Cano64 May 20 '16 at 16:39
  • @Cano64 every thing is possible, in my opinion XML is almost dead for this kind of work, echo is not long term solution. again depending on OP plans. – Maytham Fahmi May 20 '16 at 16:42
  • pretty simple api https://www.leaseweb.com/labs/2015/10/creating-a-simple-rest-api-in-php/ you could use to deliver your content, the only thing you need more is android json parser. an example of it here http://stackoverflow.com/questions/5566669/how-to-parse-a-json-object-in-android – Maytham Fahmi May 20 '16 at 16:44
  • you could format your data1.txt as json and still parse it the same way – Maytham Fahmi May 20 '16 at 16:47

1 Answers1

0

You can look at any PHP MVC framework, that would allow clean separation of php code & response (could be html, json - depending on your requirement). For example https://laravel.com/ (this is current popular & one of the best, imho). For your scenario, typically, you would want to build your PHP end as REST webservices, returning JSON responses to android. Good luck!

Ashish-G
  • 417
  • 3
  • 7