使用Retrofit和PHP脚本上传多个数据

时间:2019-01-18 08:22:54

标签: php android kotlin retrofit

这是我上传文件的PHP代码:

<?php

$target_dir = "uploads/";
$rand = rand(0, 99999999);
$target_file_name = $target_dir .basename("$rand.png");
$response = array();

$postTest = $_POST["postTest"];

if (isset($_FILES["upload_file"]))
{
    if (move_uploaded_file($_FILES["upload_file"]["tmp_name"], $target_file_name))
    {
        $success = true;
        $message = "Successfully Uploaded";
    }
    else
    {
        $success = false;
        $message = "Error while uploading";
    }
}
else
{
    $success = false;
    $message = "Required Field Missing";

}

$response["success"] = $success;
$response["message"] = $message;
$response["postTestStatus"] = $postTestStatus;
$response["postTestMessage"] = $postTestMessage;

echo json_encode($response);

这是我的Android代码:

val file = File(imagePath)

    val requestBody = RequestBody.create(MediaType.parse("*/*"), file)

    val fileToUpload = MultipartBody.Part.createFormData("upload_file", file.name, requestBody)

    val requestPOST = RequestBody.create(MediaType.parse("plain/text"), textToPost)

    var call = webApiInterface.uploadFile(fileToUpload, requestPOST)
    call.enqueue()...

当我尝试仅上传文件时,一切正常。 但是当我上传文件和POST值时,出现了错误(响应的JSON无效)

我尝试POST值时的PHP脚本:

<?php

error_reporting(0);
$target_dir = "uploads/";
$rand = rand(0, 99999999);
$target_file_name = $target_dir .basename("$rand.png");
$response = array();

$postTest = $_POST["postTest"];

if (isset($_FILES["upload_file"]))
{
    if (move_uploaded_file($_FILES["upload_file"]["tmp_name"], $target_file_name))
    {
        $success = true;
        $message = "Successfully Uploaded";
    }
    else
    {
        $success = false;
        $message = "Error while uploading";
    }
}
else
{
    $success = false;
    $message = "Required Field Missing";

}

if(isset($postTest))
{
    $postTestStatus = true;
    $postTestMessage = $postTest;
}
else
{
    $postTestStatus = false;
    $postTestMessage = "Value isn't set";
}

$response["success"] = $success;
$response["message"] = $message;
$response["postTestStatus"] = $postTestStatus;
$response["postTestMessage"] = $postTestMessage;

echo json_encode($response);

?>

WEBApi接口:

@Multipart
@POST("retrofit-example/multipartPOSTtest.php")
fun uploadFile(@Part file: MultipartBody.Part,
               @Part("postTest") valForPOST: RequestBody): Call<FileResponse>

MainActivity:

val fileToUpload = MultipartBody.Part.createFormData("upload_file", file.name, requestBody)

    val requestPOST = RequestBody.create(MediaType.parse("plain/text"), textToPost)

    var call = webApiInterface.uploadFile(fileToUpload, requestPOST)

我做对了吗? 我正在学习如何使用翻新。 我要你的理解

1 个答案:

答案 0 :(得分:0)

// First
@Multipart
@POST("retrofit-example/multipartPOSTtest.php")
fun uploadFile(@Part file: MultipartBody.Part): Call<FileResponse>

// Second
@Multipart
@POST("retrofit-example/multipartPOSTtest.php")
fun uploadFile(@Part file: MultipartBody.Part,
               @Part("postTest") valForPOST: RequestBody): Call<FileResponse>

响应方法:pastebin