gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 03:26:27 24 4
gpt4 key购买 nike

我正在尝试使用改造上传 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 字符串 "Data.getMyupload()"

的 pojo Object
        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)

Find a solution for this,

  1. 从图像路径创建一个Bitmap文件并将其转换为byte[]
    private byte[] convertToBase64(String imagePath) {

Bitmap bm = BitmapFactory.decodeFile(imagePath);

ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();

return b;
}
  • 使用Base64.encodeToString获取Base64字符串。
  •     Base64.encodeToString(convertToBase64 (imagePath), Base64.DEFAULT);

    最佳答案

    您的响应正文似乎为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());

    }
    });

    关于java - 通过 Retrofit2 上传 Base64 编码图像返回空响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56905118/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com