Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/368.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 无法使用改型作为原始JSON发布数据_Java_Android_Retrofit2_Rx Java2 - Fatal编程技术网

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

Java 无法使用改型作为原始JSON发布数据,java,android,retrofit2,rx-java2,Java,Android,Retrofit2,Rx Java2,我正试图在原始json中使用Reformation2将数据发布到服务器,但服务器没有响应。进度条继续加载,但服务器没有响应 我的服务器正在发送以下json对象作为成功消息: { "status": "Success", "statusCode": "S001", "loginResponse": { "authenticationStatus": false,

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

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

{
"status": "Success",
"statusCode": "S001",
"loginResponse": {
    "authenticationStatus": false,
    "sessionid": null,
    "errorDescription": null,
    "predictionStatus": null,
    "predictionPercentage": null,
    "predictionPercentageA": null,
    "predictionPercentageB": null,
    "predictionPercentageC": null
 }
}
我只想检查
状态
是否成功,因此我在下面的POJO类
登录回复
中进行了说明

登录回复

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.class

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

            }
        }
    });
  }
公共接口服务{
@POST(“用户登录验证”)
调用getResponse(@Body JsonObject Body);
}
MainActivity.class

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

            }
        }
    });
  }
@覆盖
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login=findviewbyd(R.id.login);
email=findviewbyd(R.id.email);
pwd=findViewById(R.id.pwd);
login.setOnClickListener(新视图.OnClickListener(){
@凌驾
公共void onClick(视图v){
final ProgressDialog prg=新建ProgressDialog(MainActivity.this);
prg.可设置可取消(false);
prg.setMessage(“登录…”);
prg.show();
字符串str1=email.getText().toString();
字符串str2=pwd.getText().toString();
if(str1等于(“”){
prg.discouse();
TastyToast.makeText(getApplicationContext(),“输入电子邮件”,TastyToast.LENGTH\u SHORT,
TastyToast.ERROR).show();
}
else if(str2.equals(“”){
prg.discouse();
TastyToast.makeText(getApplicationContext(),“输入密码”,TastyToast.LENGTH\u SHORT,
TastyToast.ERROR).show();
}
否则{
Refundation-Refundation=RefundationClient.getInstance();
ApiService ApiService=reformation.create(ApiService.class);
JsonObject JsonObject=新的JsonObject();
addProperty(“userId”,str1);
addProperty(“密码”,str2);
Call Call=apiService.getResponse(jsonObject);
call.enqueue(新回调(){
@凌驾
公共void onResponse(调用、响应){
试一试{
JSONObject JSONObject 1=新JSONObject(resp);
String str=jsonObject1.getString(“状态”);
如果(str.equals(“Success”)){
prg.discouse();
email.setText(“”);
pwd.setText(“”);
}
}
捕获(JSONException e){
e、 printStackTrace();
}
}
@凌驾
失败时公共无效(调用调用,可丢弃的t){
TastyToast.makeText(getApplicationContext(),t.getMessage(),TastyToast.LENGTH\u SHORT,
TastyToast.ERROR).show();
}
});
}
}
});
}
服务器没有对实现上述代码作出响应。请有人告诉我我做错了什么。如有任何帮助,将不胜感激

感谢您为我们服务

@FormUrlEncoded
@POST("userLoginVerification")
Call<User> getResponse(@FieldMap HashMap<String, String> data);
但在ApiService中,您希望服务器返回一个用户! 您必须更改
getResponse
返回类型以匹配服务器响应格式

另一个问题就在眼前

if (response.equals("Success"))
此条件永远不会为真,因为
response
不是
String
,您不能用这种方式检查相等性。您应该在API服务中使用
response.body()

@FormUrlEncoded
@POST("userLoginVerification")
Call<User> getResponse(@FieldMap HashMap<String, String> data);
但在ApiService中,您希望服务器返回一个用户! 您必须更改
getResponse
返回类型以匹配服务器响应格式

另一个问题就在眼前

if (response.equals("Success"))

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

公共void onResponse(Call-Call,response-response)


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

公共无效onResponse(调用、响应)


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

嘿,我已在上面添加了完整的服务器响应,并相应地更改了我的ApiService returntype。请检查我的代码,让我知道问题所在,因为它仍然没有授权用户。我认为您的数据发送不正确。您应该在
getResponse
方法中使用
@FieldMap
(就像我的答案一样)。如果你想使用
@Body
你应该为
getResponse
Arguments创建一个POJO类。我已经在上面添加了完整的服务器响应,并相应地更改了我的ApiService returntype。请检查我的代码,让我知道是什么问题,因为它仍然没有对用户进行身份验证。我认为你的数据发送不正确。您应该在
getResponse
方法中使用
@FieldMap
(就像我的答案一样)。如果您想使用
@Body
您应该为
getResponse
参数创建一个POJO类,这意味着我应该