Android Retrofit上传图片文件使用多部分请求

时间:2014-12-24 00:00:57

标签: android retrofit

我是Android改造的新手,需要使用多部分请求格式上传图像文件。 我能够使用iphone多部分请求成功上传。 但是,有了android改造的麻烦。 我使用https和令牌承载授权。

@Multipart
@POST("/Api/ApiSales/UploadImages")
void uploadImage(@Part("File") TypedFile file,
                      Callback<Response> callback);

RestAdapter.Builder builder = new RestAdapter.Builder().setEndpoint(appController.getInstance().getURL());
            builder.setRequestInterceptor(new RequestInterceptor() {
                @Override
                public void intercept(RequestFacade request) {
                    request.addHeader("Authorization", appController.getInstance().getAuthTokenString());
                }
            });
            builder.setLogLevel(RestAdapter.LogLevel.FULL);

            RestAdapter restAdapter = builder.build();

            RetrofitService service = restAdapter.create(RetrofitService.class);
            service.uploadImage(new TypedFile("image/png",toFile), new Callback<retrofit.client.Response>() {
                @Override
                public void success(retrofit.client.Response response, retrofit.client.Response response2) {
                    Log.i(TAG, response.toString());
                }

                @Override
                public void failure(RetrofitError error) {
                    Log.e(TAG, error.toString());
                }
            });

我收到了以下错误:

retrofit.RetrofitError: Write error: ssl=0x5ef8ad40: I/O error during system call, Connection reset by peer      

我为http请求设置服务器,仍然出错:

  retrofit.RetrofitError: sendto failed: ECONNRESET (Connection reset by peer)

1 个答案:

答案 0 :(得分:0)

我认为您的界面应该是这样的。

 @Multipart
    @POST("/Api/ApiSales/UploadImages") 
    void uploadImage(@Header("Authorization") String user, 
    @Part("File") TypedFile file, Callback<Response> callback);

首先应该从onActivityResult获取真实的图像路径

final String imagePath = getRealPathFromURI(imageUri);

并且getRealPathFromURI将是这样的。

private String getRealPathFromURI(Uri contentUri) {
        String[] projection = {MediaStore.Images.Media.DATA};
        CursorLoader loader = new CursorLoader(this, contentUri, projection, null, null, null);
        Cursor cursor = loader.loadInBackground();
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
        cursor.moveToFirst();
        String result = cursor.getString(column_index);
        cursor.close();
        return result;
    }

和TypedFile的初始化

File photoFile = new File(imagePath);// image will be your real path
        String mimeType = getMimeType(imagePath);
        TypedFile photoTypedFile;
        if (mimeType != null) {
            photoTypedFile = new TypedFile(mimeType, photoFile);
        } else {
            photoTypedFile = new TypedFile("image/jpg", photoFile);
        }

并且您的Restadapter初始化将是相同的,除了添加标题,您的电话将是。<​​/ p>

RetrofitService service = restAdapter.create(RetrofitService.class);
            service.uploadImage("yourAuthorization", photoTypedFil, new Callback<retrofit.client.Response>() {
                @Override
                public void success(retrofit.client.Response response, retrofit.client.Response response2) {
                    Log.i(TAG, response.toString());
                }

                @Override
                public void failure(RetrofitError error) {
                    Log.e(TAG, error.toString());
                }
            });