Can't Upload the file in Retrofit2.0?

时间:2019-04-08 13:10:06

标签: java retrofit2 okhttp kaizala

am Using Retrofit2.0 not able to upload the file from java. It gives response as Code as 415.

RetofitCode

File uploadFile1 = new File("/home/art-pc-mob-001/MISSTorewise.png");

Retrofit retrofit = new Retrofit.Builder().baseUrl(endPointURL + "v1/")
            .addConverterFactory(GsonConverterFactory.create()).build();
Links links = retrofit.create(Links.class);

RequestBody requestFile = RequestBody.create(MediaType.parse("multipart/form-data"), uploadFile1);

MultipartBody.Part body = MultipartBody.Part.createFormData("FileName", uploadFile1.getName(), requestFile);

Call<JsonObject> callBack = links.uploadMedia(accessToken, body);
    callBack.enqueue(new Callback<JsonObject>() {

    @Override
    public void onResponse(Call<JsonObject> arg0, Response<JsonObject> arg1) {
        // TODO Auto-generated method stub

        System.out.println(arg1.code());
        System.out.println(new Gson().toJson(arg1.body()));

    }

    @Override
    public void onFailure(Call<JsonObject> arg0, Throwable arg1) {
        // TODO Auto-generated method stub

    }
});

Interface Code

public interface Links {

@Multipart
@POST("media")
Call<JsonObject> uploadMedia(@Header("accessToken") String acessToken, @Part MultipartBody.Part file);
}

In Postman Working fine

API Description in JSON

"request": {
                    "method": "POST",
                    "header": [
                        {
                            "key": "accessToken",
                            "value": "{{access-token}}"
                        }
                    ],
                    "body": {
                        "mode": "formdata",
                        "formdata": [
                            {
                                "key": "FileName",
                                "type": "file",
                                "src": ""
                            }
                        ]
                    },
                    "url": {
                        "raw": "{{endpoint-url}}/v1/media",
                        "host": [
                            "{{endpoint-url}}"
                        ],
                        "path": [
                            "v1",
                            "media"
                        ]
                    },
                    "description": "upload media"
                }

Help me to solve this issue.

0 个答案:

没有答案
相关问题