I am getting internal server error while executing a PUT request using Retrofit on my android app. Here is my Interface
public interface RetrofitInterface
{
//for updating user
@PUT("users/update/{email}")
Call<Response> updateUser(@Path("email") String email,@Body User user);
}
And Heres my class
public void updateProfile(User user) {
private String name;
private String email;
private String city;
private int age;
private String password;
private String created_at;
private String newPassword;
private String token;
public void setName(String name) {
this.name = name;
}
public void setEmail(String email) {
this.email = email;
}
public void setCity(String city) {
this.city = city;
}
public void setAge(Integer age) {
this.age = age;
}
public void setPassword(String password) {
this.password = password;
}
public String getName() {
return name;
}
public String getEmail() {
return email;
}
public String getCity()
{
return city;
}
public Integer getAge() {
return age;
}
public String getCreated_at() {
return created_at;
}
public void setNewPassword(String newPassword) {
this.newPassword = newPassword;
}
public void setToken(String token) {
this.token = token;
}
}
I am putting the name,age and city from three textViews as follwing creating a new User object
User muser = new User();
muser.setName(nameText.getText().toString());
muser.setAge(Integer.parseInt(ageText.getText().toString()));
muser.setCity(cityText.getText().toString());
// user.setEmail(mEmail);
updateProfile(muser);
And this is the function I am using to update the user profile
public void updateProfile(User user) {
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("https://fitnessrace.herokuapp.com/")
.addConverterFactory(GsonConverterFactory.create())
.build();
RetrofitInterface retrofitInterface = retrofit.create(RetrofitInterface.class);
Call<Response> call = retrofitInterface.updateUser(mEmail,user);
//
call.enqueue(new Callback<Response>()
{
@Override
public void onResponse(Call<Response> call, retrofit2.Response<Response> response)
{
if (response.isSuccessful())
{
Response responseBody = response.body();
Log.d("success", response.toString());
Log.d("success2", responseBody.toString());
}
else
{
ResponseBody errorBody = response.errorBody();
// Gson gson = new Gson();
Gson gson = new GsonBuilder().create();
try
{
Log.d("error1", errorBody.toString());
//Response response1 = gson.fromJson(errorBody);
}
catch (Exception e)
{
e.printStackTrace();
Log.d("error2", e.toString());
}
}
}
@Override
public void onFailure(Call<Response> call, Throwable t)
{
Log.d(TAG, "onFailure: "+t.getLocalizedMessage());
}
});
From postman I put the follwing and it works postman put request
My server is a node.js server which has the definition for updateUser() function. But it is not working from android. What am I missing?
You have put json data to your server and in postman you have used application/x-www-form-urlencoded
content type. Try to add
@FormUrlEncoded
before @PUT("users/update/{email}")