【问题标题】:Retrofit returning internal server error status 500改造返回内部服务器错误状态 500
【发布时间】:2017-12-13 06:01:00
【问题描述】:

我正在我的 android 应用程序中实现用户登录的改造。输入类型为 Json。每次我尝试通过 Retrofit 发送 POST 请求时,都会出现 500 个内部服务器错误。

   ApiInterface apiInterface=AppController.GetRetrofitObject().create(ApiInterface.class);
        // prepare call in Retrofit 2.0
        try {
            JSONObject paramObject = new JSONObject();
            try {
                paramObject.put(ApiName.USERNAME, username);
                paramObject.put(ApiName.PASSWORD, password);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            Log.d(TAG,paramObject.toString());
            Call<Login> call = apiInterface.getLoginDetails(paramObject.toString());
            call.enqueue(new Callback<Login>() {
                @Override
                public void onResponse(@NonNull Call<Login> call, @NonNull Response<Login> response) {
                    String message = response.message();
                    int status = response.code();

                    Log.d(TAG, String.valueOf(status));
                    Intent homeIntent = new Intent(getApplicationContext(), MainActivity.class);
                    startActivity(homeIntent);
                }

                @Override
                public void onFailure(Call<Login> call, Throwable t) {

                }
            });
        }catch (Exception e){
            e.printStackTrace();
        }

每次我的状态为 500。但我在 POSTMAN 中得到响应。我的接口类是

  public interface ApiInterface {

@POST(ApiName.LOGIN)
Call<Login> getLoginDetails(@Body String body);
} 

【问题讨论】:

  • 在api调用函数中打印url

标签: android json http-post retrofit2


【解决方案1】:

String body 更改为Java pojo。例如:User

@POST(ApiName.LOGIN)
Call<Login> getLoginDetails(@Body User body);
}

.......

class User {

private String username;
private String password;

public User() {}

public User(String username, String password) {
   this.username = username;
   this.password = password;
}

// setter + getter//

}

然后调用:

....

User user = new User(username, password);
Call<Login> call = apiInterface.getLoginDetails(user);

....

【讨论】:

    猜你喜欢
    • 2021-06-21
    • 1970-01-01
    • 2014-05-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-12
    相关资源
    最近更新 更多