我正在处理此代码,使用截击发送HTTP请求,当我收到json
响应时,我将对其进行解析,并取决于它给我的意图,我希望以此开始不同的意图。
一切工作正常,但我无法弄清楚为什么ifelse
语句中如果循环的主要部分不匹配,则无法到达循环的“其他”一侧。
如果主周期满意,代码工作正常,否则我有这个问题。
这里的代码:
public class MainActivity extends AppCompatActivity {
private EditText etUsername;
private EditText etPassword;
private Button btnLogin;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etUsername = findViewById(R.id.etUsername);
etPassword = findViewById(R.id.etPassword);
btnLogin = findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Login();
}
});
}
private void Login(){
String url = "http://myapytestingthisurlisfake/loginuser_module";
RequestQueue requestQueue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d("STATO HTTP", String.valueOf(response));
try {
JSONObject jresponse = new JSONObject(response);
JSONObject state = jresponse.getJSONObject("0");
JSONObject result = state.getJSONObject("Stato Information");
String statoo = result.getString("Stato");
Log.d("RESULT ERROR", statoo);
if (statoo.equals("Error")){ //Here the code work everytime
Toast.makeText(MainActivity.this, "stato è uguale da error", Toast.LENGTH_SHORT).show();
} else {//Here the code work never
Toast.makeText(MainActivity.this, "Statoo è diverso ad error", Toast.LENGTH_SHORT).show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, "La chiamata non è andata a buon fine", Toast.LENGTH_SHORT).show();
error.printStackTrace();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("user","hello");
params.put("password","there");
params.put("secretKey","somerandomcode231231231231");
params.put("username_utente", etUsername.getText().toString().trim());
params.put("password_utente", etPassword.getText().toString().trim());
return params;
}
};
requestQueue.add(stringRequest);
}