gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 00:05:31 25 4
gpt4 key购买 nike

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

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

这是我的类(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;
}

我将三个 TextView 中的姓名、年龄和城市放在创建新用户对象之后

 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 那里我放了下面的东西并且它有效 postman put request

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

最佳答案

您已将 json 数据放入您的服务器,并在 postman 中使用了 application/x-www-form-urlencoded 内容类型。尝试添加@FormUrlEncoded@PUT("users/update/{email}")

之前

关于android - 使用 Retrofit 时出现内部服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49090030/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com