0

Androidアプリからデータを取得するためにこのコードを作成しました。jsonデータを取得した後、それらをデコードしてから、画像のベース64コードであるキー変数と画像名であるori_name変数を取得します。その後、ベース64コードからサーバー側で画像ファイルを作成し、サーバーにファイルを保存しています。イメージがサーバーに保存されている場合は、イメージ名とイメージ パスを 1 つのデータベースに保存します。Androidアプリでこのコードを実行すると、Android側でnilのみが返されます。ここにデータをフェッチするためのソリューションはありますか? Android アプリは、私が作成したこのサービスを使用する友人によって開発されていることに注意してください。彼はそれを使用すると nil 値を取得します。この問題を解決するのを手伝ってください。

<?php
//process client request(via URL)
header("content-Type:application/json");
$data=$_REQUEST['jdata'];//request json data
$imgdata=json_decode($data);//decode json data
$basecode=$imgdata->{'key'};//fetch base64 code of image
$imgname=$imgdata->{'ori_name'};//fetch image name
$response=get_url($basecode,$imgname);//cal function for store image at server
return deliver_response($response);
function deliver_response($status)
{
header("HTTP/1.1");
$response['status']=$status;
$json_response=json_encode($response);
return $json_response;//return json response to android app
}
function get_url($basecode,$imgnm)
{
$decoded= base64_decode($basecode);//convert base 64 code to string format

$location=$_SERVER['HTTP_HOST']."/Clients/krishinternational/MobileService/images/".$imgnm;
$output_file="../images/".$imgnm;//define output file location
$ifp = fopen($output_file, "wb"); //create file
    $rs=fwrite($ifp, $decoded); //write image in file
    if($rs!=False)//check weather file is written or not
    {
        fclose($ifp); 
        $conn =mysqli_connect('host','user','password','db_name');//creating database connection
        if ($conn->connect_error) 
        {
        die("Connection failed: " . $conn->connect_error);
        } 


        $query="insert into tblimage values('".$imgnm."','".$location."')";//query to store image path in database
        if ($conn->query($query) === TRUE) 
        {
        //echo "New record created successfully";                               
        mysqli_close($conn);
        return True;//if successfully inserted then return true
        }           
        else 
        {
        return False;//if not succeed then return false
        }
      }
        else
       {
        return False;
       }
    }
    ?>
4

1 に答える 1

0

Indra に感謝します。Rest Client によるテスト結果を取得しました。エラーは、Android アプリから json データを要求していますが、実際には file_get_contents を使用する必要があります。

$input=file_get_contents("php://input");

ありがとうインドラ

于 2014-12-29T03:06:43.967 に答える