使用Retrofit时出现内部服务器错误

时间:2018-03-03 22:46:05

标签: android retrofit retrofit2 put internal-server-error

我在Android应用上使用Retrofit执行PUT请求时出现内部服务器错误。 这是我的界面

public interface RetrofitInterface 
{
//for updating user
@PUT("users/update/{email}")
Call<Response> updateUser(@Path("email") String email,@Body User user); 
}

而我的班级是

   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;
}

}

我将三个textViews中的名称,年龄和城市放在下面,创建一个新的User对象

 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);

这是我用来更新用户个人资料的功能

   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());
        }
    });

从邮递员那里我把下面的内容付诸实践 postman put request

我的服务器是node.js服务器,它具有updateUser()函数的定义。 但它不适用于android。我错过了什么?

1 个答案:

答案 0 :(得分:1)

您已将json数据放入您的服务器,而邮递员则使用了application/x-www-form-urlencoded内容类型。尝试添加 @FormUrlEncoded之前的@PUT("users/update/{email}")