簡體   English   中英

使用 Retrofit 在不同設備中上傳圖像/文件的問題,出現錯誤代碼:500 或內部服務器錯誤

[英]Problem Upload Image/File in different devices with Retrofit, Got an Error Code:500 or Internal Server Error

我想使用改造 2 上傳圖像/文件,這是我的代碼

這是我的界面

@POST("Upload/UploadFile") Call<ResponseUpload> postImage (@Body RequestBody files);

這是我發布到網絡服務的方法

    File fotoKTP = new File(Uri.parse(pathKTP).getPath());
    File fotoSelfie = new File(Uri.parse(pathSelfie).getPath());

    MultipartBody.Builder builder = new MultipartBody.Builder();
    builder.setType(MultipartBody.FORM);
    builder.addFormDataPart("phone_number", regisUser.getNo_hp());
    builder.addFormDataPart("foto_id",fotoKTP.getName(),RequestBody.create(MediaType.parse("image/jpeg"), fotoKTP));
    builder.addFormDataPart("foto_selfie",fotoSelfie.getName(),RequestBody.create(MediaType.parse("image/jpeg"), fotoSelfie));

    MultipartBody requestBody = builder.build();

    Call<ResponseUpload> call = client.postImage(requestBody);
    call.enqueue(new Callback<ResponseUpload>() {
        @Override
        public void onResponse(Call<ResponseUpload> call, Response<ResponseUpload> response) {
            ResponseUpload responseUpload = response.body();
            if (responseUpload != null){
                if (responseUpload.getSuccess().equals("1")) {
                    flag = true;
                    regisUser.setFoto_id(responseUpload.getFoto_id());
                    regisUser.setFoto_selfie(responseUpload.getFoto_selfie());
                    Toast.makeText(AutentikasiActivity.this, "Url Foto : " + responseUpload.getFoto_id() + "dan" + responseUpload.getFoto_selfie(), Toast.LENGTH_SHORT).show();

                } else if (responseUpload.getSuccess().equals("-9")) {
                    flag = false;
                    Toast.makeText(AutentikasiActivity.this, "Balikan Dari Server :" + responseUpload.getError_msg(), Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(AutentikasiActivity.this, "Response Null", Toast.LENGTH_SHORT).show();
                }
            }
        }

        @Override
        public void onFailure(Call<ResponseUpload> call, Throwable t) {
            Toast.makeText(AutentikasiActivity.this, "Check Your Connection", Toast.LENGTH_SHORT).show();
        }
    });

我的問題是為什么我在使用不同設備時收到內部服務器錯誤/代碼:500。

我用我的設備 Lenovo K6 Note Android 7 API 23 測試它運行良好,我得到代碼:200,這意味着成功,但是當我用我的朋友設備 OnePlus 5 T(Android 8.1 API 27)進行測試時,它得到錯誤代碼:500 “內部服務器錯誤”,我已經用第三台設備 Samsung Note FE Android 8.1 API 27 測試過它有一些錯誤代碼。 有誰知道如何解決這個問題? 謝謝。

這不是真正的解決方案,但如果您也是后端的作者或管理員,我會嘗試檢查您的后端收到的數據類型以及服務器日志說明后端腳本失敗的原因,因為 500 是服務器錯誤。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM