doujingjiao0015 2019-01-18 08:22 采纳率: 0%
浏览 103

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

This is my PHP code to upload file:

<?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);

This is my Android code:

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()...

When I am trying upload only file, all is ok. But when I upload file and POST value, I have got errors ( JSON from reponse isn't valid )

My PHP script when I trying POST value:

<?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);

?>

WEBApiInterface:

@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)

Am I doing it right ? I am learning how to use Retrofit. I am asking for your understanding

  • 写回答

1条回答 默认 最新

  • duanliaoyu8419 2019-01-18 14:30
    关注
    // 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>
    

    Response method: pastebin

    评论

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计