简体   繁体   English

使用Android中的Retrofit2库进行文件上传

[英]File Uploading using Retrofit2 library in Android

Thanks in advance... I need help in uploading file (image file) to server using Retrofit2 library. 在此先感谢...我需要帮助使用Retrofit2库将文件(图像文件)上传到服务器。 I have already acheived simple (text based) request and response. 我已经实现了简单(基于文本)的请求和响应。 But i am facing issue in uploading image file to server. 但我在将图像文件上传到服务器时面临问题。 Below is my Android Code: 以下是我的Android代码:

Upload Function 上传功能

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

        }
    });

Interface 接口

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

ServerResponse File ServerResponse文件

public class ServerResponse {

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

public String getMessage() {
    return message;
}

public boolean getSuccess() {
    return success;
}
}

My PHP Code on server 我在服务器上的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);

?>

My Problem 我的问题

The main problem i am facing is that i always get 我面临的主要问题是我总能得到

Error while Uploading 上传时出错

from server. 从服务器。 I have checked the variable $target_dir and it contains the name of image file i am trying to upload. 我检查了变量$ target_dir ,它包含我试图上传的图像文件的名称。 But in actual no file is uploading to my uploads folder... Please any help in this matter. 但实际上没有文件上传到我的上传文件夹...请在此问题上有任何帮助。 I am exhausted too much now 我现在已经筋疲力尽了

After searching and helping suggestions from people, I came to know that I have not granted the permission to my file and folder. 在搜索并帮助人们提出建议后,我发现我没有将权限授予我的文件和文件夹。 So after granting permission my code works perfectly fine. 因此,在授予权限后,我的代码完全正常。 This code is correct so anybody can use this code in future... If need some help regarding this code then feel free to ask me. 这段代码是正确的,所以任何人都可以在将来使用此代码...如果需要一些关于此代码的帮助,请随时问我。 And thanks alot everyone who helped me to solve my problem. 非常感谢帮助我解决问题的每个人。 Have a nice day...! 祝你今天愉快...!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM