有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java无法使用改型作为原始JSON发布数据

我试图在原始json的基础上使用Reformation2将数据发布到服务器,但服务器没有响应。进度条继续加载,但服务器没有响应

我的服务器正在发送以下json对象作为成功消息:

{
"status": "Success",
"statusCode": "S001",
"loginResponse": {
    "authenticationStatus": false,
    "sessionid": null,
    "errorDescription": null,
    "predictionStatus": null,
    "predictionPercentage": null,
    "predictionPercentageA": null,
    "predictionPercentageB": null,
    "predictionPercentageC": null
 }
}

我只想检查status是否是它的Success,所以我在下面的POJO类LoginResponse中做了这个检查

登录回复

public class LoginResponse {

@SerializedName("status")
String status;

public LoginResponse(){

}

public LoginResponse(String status) {
    this.status = status;
}

public String getStatus() {
    return status;
}

public void setStatus(String status) {
    this.status = status;
}
}

下面是我的代码:

ApiService。班级

public interface ApiService {

@POST("userLoginVerification")
Call<LoginResponse> getResponse(@Body JsonObject body);
}

主要活动。班级

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    login = findViewById(R.id.login);
    email = findViewById(R.id.email);
    pwd = findViewById(R.id.pwd);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            final ProgressDialog prg = new ProgressDialog(MainActivity.this);
            prg.setCancelable(false);
            prg.setMessage("Logging in...");
            prg.show();

            String str1 = email.getText().toString();
            String str2 = pwd.getText().toString();

            if(str1.equals("")){

                prg.dismiss();
                TastyToast.makeText(getApplicationContext(),"Enter email",TastyToast.LENGTH_SHORT,
                        TastyToast.ERROR).show();
            }
            else if(str2.equals("")){

                prg.dismiss();
                TastyToast.makeText(getApplicationContext(),"Enter password",TastyToast.LENGTH_SHORT,
                        TastyToast.ERROR).show();
            }
            else{

                Retrofit retrofit = RetrofitClient.getInstance();
                ApiService apiService = retrofit.create(ApiService.class);

                JsonObject jsonObject= new JsonObject();
                jsonObject.addProperty("userId",str1);
                jsonObject.addProperty("password",str2);

                Call<LoginResponse> call = apiService.getResponse(jsonObject);

                call.enqueue(new Callback<LoginResponse>() {
                    @Override
                    public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response) {

                       try{
                            JSONObject jsonObject1 = new JSONObject(resp);

                            String str = jsonObject1.getString("status");

                            if(str.equals("Success")){

                                prg.dismiss();
                                email.setText("");
                                pwd.setText("");
                            }
                           }
                          catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }

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

                        TastyToast.makeText(getApplicationContext(),t.getMessage(),TastyToast.LENGTH_SHORT,
                                   TastyToast.ERROR).show();
                    }
                });

            }
        }
    });
  }

服务器对实现上述代码没有响应。有人请让我知道我做错了什么。任何帮助都将不胜感激

谢谢


共 (2) 个答案

  1. # 1 楼答案

    为你效劳

    @FormUrlEncoded
    @POST("userLoginVerification")
    Call<User> getResponse(@FieldMap HashMap<String, String> data);
    

    还有HashMap

    HashMap<String, String> map = new HashMap<>();
            map.put("userId", "theUserId");
            map.put("password", "thePassword");
    

    另外,一个大问题是你的回复是格式的

    { "status": "Success" }
    

    但在ApiService中,您希望服务器返回一个用户! 必须更改getResponse返回类型以匹配服务器响应格式

    另一个问题就在眼前

    if (response.equals("Success"))
    

    这个条件永远不会为真,因为response不是String,并且不能用这种方式检查相等性。您应该使用response.body()

  2. # 2 楼答案

    public void onResponse(Call<LoginResponse> call, Response<LoginResponse> response)

    不要在onResponse中创建JSONObjectresponse.body()为您提供LoginResponse对象。使用该对象获取状态。如果活动或片段未被销毁,请在onResponseonFailure之后立即关闭进度条