【问题标题】:Curl using Retrofit android使用 Retrofit android 卷曲
【发布时间】:2019-10-29 00:04:42
【问题描述】:

我使用 curl -F datafile="@myIMage.png" http://www.myUrl.com/tools/index.php/testUpload/do_upload 将图像上传到服务器 我必须使用 android 中的 http 调用复制此上传,我正在使用改造,这是我的代码:

public static class NetworkClient {
    private static final String BASE_URL = "http://myUrl.com/tools/index.php/";
    private static Retrofit retrofit;

    public static Retrofit getRetrofitClient(Context context) {
        if (retrofit == null) {
            OkHttpClient okHttpClient = new OkHttpClient.Builder()
                    .build();
            retrofit = new Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .client(okHttpClient)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }
}

public interface UploadAPIs {
    @Multipart
    @POST("/testUpload/do_upload")
    retrofit2.Call<SimpleAdapter> uploadImage(@Part MultipartBody.Part file, @Part("datafile") RequestBody requestBody);
}

    upload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Retrofit retrofit = NetworkClient.getRetrofitClient(getApplicationContext());
            UploadAPIs uploadAPIs = retrofit.create(UploadAPIs.class);

            File file = new File(path); // the path where is stored the image

            RequestBody fileReqBody = RequestBody.create(MediaType.parse("image/*"), file);
            MultipartBody.Part part = MultipartBody.Part.createFormData("datafile", "@"+file.getName(), fileReqBody);
            RequestBody description = RequestBody.create(MediaType.parse("text/plain"), "image-type");

            final retrofit2.Call call = uploadAPIs.uploadImage(part, description);

            call.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    Log.d("SICC", String.valueOf(call.request()));
                }

                @Override
                public void onFailure(Call call, Throwable t) {
                    Log.d("FAIL", t.toString());
                }
            });
        }
    });
}

但是这样我无法将照片发布到我的服务器上,有什么建议吗?

【问题讨论】:

    标签: java android curl retrofit


    【解决方案1】:

    你的 api 服务类

    @POST("/testUpload/do_upload")
    @Multipart
    Call<SimpleAdapter> uploadImage(@PartMap Map<String, RequestBody> map);
    

    为你的 hashmap 赋值

     HashMap hashMap = new HashMap<String, RequestBody>();
     RequestBody fileReqBody = RequestBody.create(
                  MediaType.parse("multipart/form-data"), new File(path));
     hashMap.put("datafile\"; filename=\"image_" 
                              + System.currentTimeMillis() 
                              +".jpeg\"", fileReqBody);
    

    调用api方法

    Retrofit retrofit = NetworkClient.getRetrofitClient(getApplicationContext());
    UploadAPIs uploadAPIs = retrofit.create(UploadAPIs.class);
    
    final retrofit2.Call call = uploadAPIs.uploadImage(hashMap);
    
    call.enqueue(new Callback() {
                @Override
                public void onResponse(Call call, Response response) {
                    Log.d("SICC", String.valueOf(call.request()));
                }
    
                @Override
                public void onFailure(Call call, Throwable t) {
                    Log.d("FAIL", t.toString());
                }
            });
    

    【讨论】:

    • 请检查您的文件和路径。
    • 这是响应:D/SICC: Response{protocol=http/1.1, code=404, message=Not Found, url=myUrl.com/testUpload/do_upload}
    • 请用邮递员测试你的api链接。我认为你的 api 方法不存在。
    猜你喜欢
    • 1970-01-01
    • 2017-03-05
    • 1970-01-01
    • 1970-01-01
    • 2011-03-02
    • 2022-01-12
    • 1970-01-01
    • 2011-10-12
    • 1970-01-01
    相关资源
    最近更新 更多