I have working OkHttp MultiPart
request
:
multi = new MultipartBody.Builder()
.setType(MultipartBody.FORM)
.addFormDataPart("email", m)
.addFormDataPart("password", p)
.addFormDataPart("user_name", n)
.addFormDataPart("user_phone", phone)
.addFormDataPart("photo", "avatar.jpg", RequestBody.create(MediaType.parse("image/jpg"), imgToArray(bmpChosenPic)))
//.addFormDataPart("photo", "1")
.build();
request = new Request.Builder()
.url(Utils.TEST_BASE_URL + "" + REG_URL)
.post(multi)
.build();
client.newCall(request).enqueue(new Callback()...
Now I try to make the same function using Retrofit 2.0
. Here's the service:
@Multipart
@POST("/api/v1/auth/register")
Call<Registration> registration (@Part("email") RequestBody email,
@Part("password") RequestBody password,
@Part("user_name") RequestBody userName,
@Part("photo") RequestBody url,
@Part("phone") RequestBody phone);
So according to this interface
I try to make a Call
:
RequestBody photoR = RequestBody.create(MediaType.parse("image/jpg"), imgToArray(bmp));
RequestBody nameR = RequestBody.create(MediaType.parse("text/plain"), name);
RequestBody emailR = RequestBody.create(MediaType.parse("text/plain"), email);
RequestBody passR = RequestBody.create(MediaType.parse("text/plain"), pass);
RequestBody phone = RequestBody.create(MediaType.parse("text/plain"), "");
RegistaionI service = ServiceGenerator.createService(RegistaionI.class);
retrofit2.Call<Registration> call = service.registration(
emailR,
passR,
nameR,
photoR,
phone);
call.enqueue(new retrofit2.Callback<Registration>() {
@Override
public void onResponse(retrofit2.Call<Registration> call, retrofit2.Response<Registration> response) {
RegistrationData data = response.body().getData();
System.out.println(data.getId());
}
@Override
public void onFailure(retrofit2.Call<Registration> call, Throwable t) {
}
});
But, unfortunately, I get NPE in this line:
RegistrationData data = response.body().getData();
So I suppose, that something's wrong in my code. I've read related topics on SOF and at the Retrofit branch on GitHub
but didn't find working solution. Please, help.