dongyuan1870 2016-05-20 16:19
浏览 46

更好的方式将数据从PHP传输到android而不是回声

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.

  • 写回答

1条回答 默认 最新

  • doutuan8887 2016-05-20 16:47
    关注

    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!

    评论

报告相同问题?

悬赏问题

  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决
  • ¥15 processing提取音乐节奏
  • ¥15 gg加速器加速游戏时,提示不是x86架构
  • ¥15 python按要求编写程序
  • ¥15 Python输入字符串转化为列表排序具体见图,严格按照输入
  • ¥20 XP系统在重新启动后进不去桌面,一直黑屏。
  • ¥15 opencv图像处理,需要四个处理结果图
  • ¥15 无线移动边缘计算系统中的系统模型
  • ¥15 深度学习中的画图问题