【发布时间】:2020-10-12 17:17:13
【问题描述】:
我正在尝试使用来自原始 json 的 retrofit2 将数据发布到服务器,但服务器没有响应。进度条继续加载,但服务器没有响应。
我的服务器正在发送下面的 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.class
public interface ApiService {
@POST("userLoginVerification")
Call<LoginResponse> getResponse(@Body JsonObject body);
}
MainActivity.class
@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();
}
});
}
}
});
}
执行上述代码时服务器没有响应。请有人告诉我我做错了什么。任何帮助将不胜感激。
谢谢
【问题讨论】:
标签: java android retrofit2 rx-java2