Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/213.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 将上载文件转换为改装请求_Android_Retrofit - Fatal编程技术网

Android 将上载文件转换为改装请求

Android 将上载文件转换为改装请求,android,retrofit,Android,Retrofit,我需要上传图像文件到服务器。现行代码 File body = ... Properties headers = getDefaultHeaders(); HttpClient httpClient = new DefaultHttpClient(); HttpPost httpPost = new HttpPost(convertToURL(command).toString()); if (headers != null) { for (

我需要上传图像文件到服务器。现行代码

    File body = ...
    Properties headers = getDefaultHeaders();
    HttpClient httpClient = new DefaultHttpClient();
    HttpPost httpPost = new HttpPost(convertToURL(command).toString());
    if (headers != null) {
        for (Map.Entry<Object, Object> entry : headers.entrySet()) {
            httpPost.addHeader(entry.getKey().toString(), entry.getValue().toString());
        }
    }

    MultipartEntity mpEntity = new MultipartEntity();
    ContentBody cbFile = new FileBody(body, "image/jpeg");
    mpEntity.addPart("file", cbFile);
    httpPost.setEntity(mpEntity);

    HttpResponse response = httpClient.execute(httpPost);
    String entityString = EntityUtils.toString(response.getEntity());
    return new Gson().fromJson(entityString, new TypeToken<Slide>() {}.getType());
并得到错误的请求结果。我哪里出错了

其他非媒体类型(JSON)的请求工作正常

    @Multipart
    @POST(COMMAND_ADD_SLIDE)
    Call<Slide> addSlide (@PartMap Map<String, RequestBody> params);
    File body = ...
    Map<String, RequestBody> map = new HashMap<>();
    RequestBody fileBody = RequestBody.create(MediaType.parse("image/jpeg"), body);
    map.put("file", fileBody);
    Call<Slide> call = getApiService(true).addSlide(map);
getApiService(boolean isMedia) {
    OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
    httpClient.addInterceptor(new Interceptor() {
        @Override
        public Response intercept(Interceptor.Chain chain) throws IOException {
            Request original = chain.request();
            Request.Builder builder = original.newBuilder();
            builder....//add default headers
                    .header(Fields.CONTENT_TYPE, isMedia? "multipart/form-data" : "text/json");

            Request request = builder
                    .method(original.method(), original.body())
                    .build();
            return chain.proceed(request);
        }
    });

    OkHttpClient client = httpClient.build();

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(SERVICE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .client(client)
            .build();

    return retrofit.create(ApiService.class);
}