通过Retrofit2返回空响应上传Base64编码图像

时间:2019-07-05 14:36:10

标签: java android retrofit2

我正在尝试使用改型上载Base64图像字符串。但它不起作用。

这是我的界面

    @FormUrlEncoded
    @POST("index.php?r=unifyservice/savepropdocs_base64")
    Call<uploadResponce> UploadImage(@Field("fileName") String FileName,
                                     @Field("proposal_No") String ProposalNo,
                                     @Field("agent_code") String AgentCode,
                                     @Field("category") String Category,
                                     @Field("myupload") String Base64Image
    );

这是我的电话。 我有一个带有Base64字符串Object的pojo "Data.getMyupload()"

        ApiInterface apiInterface = ApiClient.getClient_myarena().create(ApiInterface.class);
        Call<uploadResponce> call = apiInterface.UploadImage(
                Data.getFileName(),
                Data.getProposal_No(),
                Data.getAgent_code(),
                Data.getCategory(),
                Data.getMyupload()
        );
        //Call<uploadResponce> call = apiInterface.UploadImage("application/json", Data);

        call.enqueue(new Callback<uploadResponce>() {
            @Override
            public void onResponse(Call<uploadResponce> call, Response<uploadResponce> response) {

                System.out.println("sdfdsf " + response.body().toString());
            }

            @Override
            public void onFailure(Call<uploadResponce> call, Throwable t) {
                Log.e("AAA Image Upload ", t.toString());

            }
        });

这是我运行应用程序时得到的 它崩溃并给出此异常

2019-07-05 19:52:50.133 8563-8563/lk.allianz.damitht.eproposal E/AndroidRuntime: FATAL EXCEPTION: main
    Process: lk.allianz.damitht.eproposal, PID: 8563
    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String lk.allianz.damitht.eproposal.model.ArenaUpload.uploadResponce.toString()' on a null object reference
        at lk.allianz.damitht.eproposal.fragments.ArenaUploads$12.onResponse(ArenaUploads.java:1005)
        at retrofit2.ExecutorCallAdapterFactory$ExecutorCallbackCall$1$1.run(ExecutorCallAdapterFactory.java:68)
        at android.os.Handler.handleCallback(Handler.java:873)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:193)
        at android.app.ActivityThread.main(ActivityThread.java:6669)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

1 个答案:

答案 0 :(得分:0)

您的回复正文似乎是null。在使用响应之前,您应该检查api调用是否成功:

call.enqueue(new Callback<uploadResponce>() {
            @Override
            public void onResponse(Call<UploadResponce> call, Response<uploadResponce> response) {
            if (response.isSuccessful()) {
             UploadResponceitem uploadResponce = response.body();
              if (uploadResponce != null) {
               System.out.println("sdfdsf " + uploadResponce.toString());
              }
             } 
            }

            @Override
            public void onFailure(Call<uploadResponce> call, Throwable t) {
                Log.e("AAA Image Upload ", t.toString());

            }
        });