我创建了一个API,用于从数据库中检索数据。现在我能够从API检索状态,消息和数据,无论我输入的凭据是正确还是不正确。有两个版本的输出。我使用POSTMan进行测试,看起来有效,但是当我尝试使用Android时,成功消息是可以的,但不是错误消息。
@POST
@Path("/appdatas")
public Response getSAppData(AppDataRequest adr) {
Response data = ads.getSAppData(adr.getId(), adr.getEmail(), adr.getPassword());
return data;
}
@SuppressWarnings("unchecked")
public Response getSAppData(int id, String email, String password){
Map<String, AppData> AppDataHM = new HashMap<String, AppData>();
Map<String, Data> DataHM1 = new HashMap<String, Data>();
Map<String, List<String>> DataHM2 = new HashMap<String, List<String>>();
Map ADHMDHM = new HashMap<>();
Data data = DataHM.get(new AppDataRequest (id, email, password));
List<String> message = new ArrayList<>();
List<String> data2 = new ArrayList<>();
if(data != null){
message.add("");
AppDataHM.put("AppData", new AppData("success", message));
DataHM1.put("Data", data);
ADHMDHM.putAll(AppDataHM);
ADHMDHM.putAll(DataHM1);
String ADHMDHM1 = new Gson().toJson(ADHMDHM);
return Response.status(200).entity(ADHMDHM1).build();
}
else{
message.add("Your login information is invalid. Please try with the correct information");
AppDataHM.put("AppData", new AppData("error", message));
DataHM2.put("Data", data2);
ADHMDHM.putAll(AppDataHM);
ADHMDHM.putAll(DataHM2);
String ADHMDHM2 = new Gson().toJson(ADHMDHM);
return Response.status(500).entity(ADHMDHM2).build();
}
}
当我使用POSTMan时,我能够检索两个输出。
{
"AppData": {
"status": "success",
"message": [
""
]
},
"Data": {
"token": "token1"
}
}
{
"AppData": {
"status": "error",
"message": [
"Your login information is invalid. Please try with the correct information"
]
},
"Data": []
}
当我在Android上应用以下代码时,我能够检索成功的数据而不是错误的数据。
private void makeJsonObjectRequest(){
showpDialog();
String id1 = mEditTextID.getText().toString();
String email1 = mEditTextEmail.getText().toString().trim();
String password1 = mEditTextPassword.getText().toString();
HashMap<String, String> params = new HashMap<String, String>();
params.put("id", id1);
params.put("email", email1);
params.put("password", password1);
JsonObjectRequest request = new JsonObjectRequest(url, new JSONObject(params), new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject AppData = response.getJSONObject("AppData");
String status = AppData.getString("status");
String message = AppData.getString("message");
//JSONObject Data = response.getJSONObject("Data");
//String token = Data.getString("token");
jsonResponse = "";
jsonResponse += "Status: " + status + "\n";
jsonResponse += "\n";
jsonResponse += "Message: " + message + "\n";
//jsonResponse += "\n";
//jsonResponse += "Token: " + token + "\n";
mTextViewMain.setText(jsonResponse);
hidepDialog();
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getActivity(),
"Error: " + e.getMessage(),
Toast.LENGTH_SHORT).show();
}
hidepDialog();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error1: " + error.getMessage());
Toast.makeText(getActivity(),
"Error2: " + error.getMessage(), Toast.LENGTH_SHORT).show();
hidepDialog();
}
});
AppController.getInstance().addToRequestQueue(request);
}
如何才能获得输出,如'错误'输出所示并显示在TextView中?
感谢所有人查看此问题。
答案 0 :(得分:1)
为什么要在脚本中返回状态500? Volley将代码500视为错误
return Response.status(500).entity(ADHMDHM2).build();
尝试将其更改为200并检查
答案 1 :(得分:0)
答案 2 :(得分:0)
Message
试试这个:
JSONArray messageArray = AppData.getJSONArray("message");
String message = messageArray.toString();