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

问题描述 投票:1回答:1

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

从邮递员我把以下,它工作qazxsw poi

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

android retrofit retrofit2 put internal-server-error
1个回答
1
投票

您已将json数据放入服务器,而邮递员则使用了postman put request内容类型。尝试在application/x-www-form-urlencoded之前添加@FormUrlEncoded

© www.soinside.com 2019 - 2024. All rights reserved.