Returning a blob with json

后端 未结 3 710
温柔的废话
温柔的废话 2020-12-06 05:22

I\'m trying to create a rest service for my android application where an external database returns items to be stored in the applications local database. I\'ve got everythi

相关标签:
3条回答
  • 2020-12-06 05:51

    It seems that json_encode works with UTF-8 encoded data only. You can use json_last_error() to detect json_encode error.

    0 讨论(0)
  • 2020-12-06 05:52

    you have to make BLOB to base64_encode

     while($spot = $results->fetch_assoc()){
         $spots[] = $spot;                                                           
        }
    

    Then prepare a foreach loop like this

    foreach($spots as $key=>$value){
        $newArrData[$key] =  $spots[$key];
        $newArrData[$key]['picture'] = base64_encode($spots[$key]['picture']);
        $newArrData[$key]['thumbnail'] = base64_encode($spots[$key]['thumbnail']);
    }
    header('Content-type: application/json');
    echo json_encode($newArrData);
    
    0 讨论(0)
  • 2020-12-06 06:06

    Encode the binary data as base64 before you generate the JSON.

    $obj->picture = base64_encode($binaryData);
    

    You can then decode this in your Android application with any base 64 decoder. Since API level 8 there is a built in util class. Otherwise there are plenty of external libs you can use for targetting Android 2.1 or earlier.

    0 讨论(0)
提交回复
热议问题