Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

请问下,网络请求上传图片文件这些怎么写 #129

Open
edcedc opened this issue May 18, 2023 · 1 comment
Open

请问下,网络请求上传图片文件这些怎么写 #129

edcedc opened this issue May 18, 2023 · 1 comment

Comments

@edcedc
Copy link

edcedc commented May 18, 2023

可以求个方法吗

@edcedc edcedc changed the title 请问下,上传图片文件这些怎么写 请问下,网络请求上传图片文件这些怎么写 May 18, 2023
@hegaojian
Copy link
Owner

这个库的网络请求框架用的是Retrofit,你可以去搜一下Retrofit上传图片的相关代码,以下我放个ChatGPT提供的示例:

@Multipart
    @POST("upload")
    Call<ResponseBody> uploadImage(@Part MultipartBody.Part image);
File file = new File("path/to/image.jpg");
RequestBody requestBody = RequestBody.create(MediaType.parse("image/jpeg"), file);
MultipartBody.Part imagePart = MultipartBody.Part.createFormData("image", file.getName(), requestBody);
Call<ResponseBody> call = apiService.uploadImage(imagePart);
call.enqueue(new Callback<ResponseBody>() {
    @Override
    public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
        if (response.isSuccessful()) {
            // 上传成功
        } else {
            // 上传失败
        }
    }

    @Override
    public void onFailure(Call<ResponseBody> call, Throwable t) {
        // 请求失败
    }
});

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants