首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Retrofit的Curl

使用Retrofit的Curl
EN

Stack Overflow用户
提问于 2019-10-29 00:04:42
回答 1查看 1.1K关注 0票数 0

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

代码语言:javascript
复制
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());
                }
            });
        }
    });
}

但是这样我不能把我的照片发到我的服务器上,有什么建议吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2019-10-29 04:46:50

您的api服务类

代码语言:javascript
复制
@POST("/testUpload/do_upload")
@Multipart
Call<SimpleAdapter> uploadImage(@PartMap Map<String, RequestBody> map);

将值放在hashmap上

代码语言:javascript
复制
 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方法

代码语言:javascript
复制
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());
            }
        });
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58599886

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档