gpt4 book ai didi

java - retrofit2 上没有这样的文件或目录调用响应

转载 作者:行者123 更新时间:2023-11-29 02:32:04 25 4
gpt4 key购买 nike

我想使用 retrofit2 POST 在 RequestBody 中添加一个文件。但是调用响应中的响应始终是“没有这样的文件或目录”。不知道哪里出了问题。

Manidest 许可:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

方法如下:

    public static void stackoverflowGuysHelpMePls(){


File file = new File("android.resource://myapps.me.test/drawable/myimage");
ApiInterface apiInterface = ApiClient.getApiClient().create(ApiInterface.class);
RequestBody requestFile =
RequestBody.create(MediaType.parse("image/*"),file);

MultipartBody.Part body = MultipartBody.Part.createFormData("data",file.getName(),requestFile);

Call<JsonObject> call = apiInterface.addSound4( body);
call.enqueue(new Callback<JsonObject>() {
@Override
public void onResponse(Call<JsonObject> call,
Response<JsonObject> response) {
Log.v("xxx", "success");
}

@Override
public void onFailure(Call<JsonObject> call, Throwable t) {
Log.e("xxx", ":( "+t.getMessage());
}
});
}

谢谢,

最佳答案

使用这个

     if (file != null) {
RequestBody requestFile =
RequestBody.create(MediaType.parse("multipart/form-
data"),file);

MultipartBody.Part body =
MultipartBody.Part.createFormData("data",file.getName(),requestFile);
}

关于java - retrofit2 上没有这样的文件或目录调用响应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49123964/

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