【问题标题】:File Uploading using Retrofit2 library in Android在 Android 中使用 Retrofit2 库上传文件
【发布时间】:2016-10-22 23:58:15
【问题描述】:

在此先感谢...我在使用 Retrofit2 库将文件(图像文件)上传到服务器时需要帮助。我已经实现了简单的(基于文本的)请求和响应。但是我在将图像文件上传到服务器时遇到了问题。以下是我的 Android 代码:

上传功能

Map<String, RequestBody> map = new HashMap<>();
    File file = new File(mediaPath);
    RequestBody requestBody = RequestBody.create(MediaType.parse("*/*"), file);
    map.put("file\"; filename=\"" + file.getName() + "\"", requestBody);
    ApiConfig getResponse = AppConfig.getRetrofit().create(ApiConfig.class);
    Call<ServerResponse> call = getResponse.upload("token", map);




    call.enqueue(new Callback<ServerResponse>() {
        @Override
        public void onResponse(Call<ServerResponse> call, Response<ServerResponse> response) {
            ServerResponse serverResponse = response.body();
            if (serverResponse != null) {
                if (serverResponse.getSuccess()) {
                    Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(getApplicationContext(), serverResponse.getMessage(),Toast.LENGTH_SHORT).show();
                }
                Log.e("Retro", serverResponse.getMessage());
            } else {
                Log.v("Response", serverResponse.toString());
            }
            progressDialog.dismiss();
        }

        @Override
        public void onFailure(Call<ServerResponse> call, Throwable t) {

        }
    });

界面

@Multipart
@POST("laundryapp/upload_image.php")
Call<ServerResponse> upload(
        @Header("Authorization") String authorization,
        @PartMap Map<String, RequestBody> map
);

服务器响应文件

public class ServerResponse {

@SerializedName("success")
boolean success;
@SerializedName("message")
String message;

public String getMessage() {
    return message;
}

public boolean getSuccess() {
    return success;
}
}

我在服务器上的 PHP 代码

    <?php

$target_dir = "uploads/";
$target_dir = $target_dir .basename($_FILES["file"]["name"]);
$response = array();

// Check if image file is a actual image or fake image
if (isset($_FILES["file"])) 
{


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

$response["success"] = $success;
$response["message"] = $message;
echo json_encode($response);

?>

我的问题

我面临的主要问题是我总是得到

上传时出错

来自服务器。我检查了变量 $target_dir ,它包含我要上传的图像文件的名称。但实际上没有文件上传到我的上传文件夹......请在这件事上提供任何帮助。我现在太累了

【问题讨论】:

标签: php android file-upload retrofit2


【解决方案1】:

在搜索和帮助人们的建议后,我知道我没有授予我的文件和文件夹的权限。因此,在授予许可后,我的代码可以正常工作。此代码是正确的,因此任何人将来都可以使用此代码...如果需要有关此代码的一些帮助,请随时问我。并感谢所有帮助我解决问题的人。祝您有美好的一天...!

【讨论】:

    猜你喜欢
    • 2019-11-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-28
    • 2015-11-22
    • 2019-08-05
    • 2021-04-09
    相关资源
    最近更新 更多