我正在尝试为我的Android应用程序做响应侦听器。
我试图从我的php代码得到这样的响应:
Response.Listener<String> responseListener = new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
Log.i("tagconvertstr", "["+response+"]");
JSONObject jsonResponse = new JSONObject(response);
JSONArray success = jsonResponse.getJSONArray("success");
JSONArray success2 = jsonResponse.getJSONArray("no");
if (success.toString() == "{"success"}") {
Intent intent = new Intent(EnglishRegisterSite.this, UK.class);
EnglishRegisterSite.this.startActivity(intent);
} else if (success2.toString() == "{"no"}") {
AlertDialog.Builder builder = new AlertDialog.Builder(EnglishRegisterSite.this);
builder.setMessage("You are not 18 years old! kid!")
.setNegativeButton("Retry", null)
.create()
.show();
}
} catch (JSONException e) {
e.printStackTrace();
}
}
};
PHP响应如下:
$response = array();
$response["no"];
echo json_encode($response);
如果我将php数组更改为$response["no"] = true;
并创建一个布尔值,那么我可以让它像这样工作:
boolean success2 = jsonResponse.getboolean("no");
,但这不行,因为那样我只能有两种情况。不是失败就是成功。但我想要实现的是创造更多的场景,比如如果它失败了,那么说明它失败的原因。就像这个例子。如果用户正在尝试注册,但未满18岁,则弹出一条消息,说明存在问题。
因此,我需要将某种JSON编码的消息从PHP发送到Java,以便我在Java中可以检查。有人知道我怎么才能做到这一点吗?
尝试json响应内容:
{
success: true,
msg: "Here can be anything you want to display."
}
如果成功,则注册,否则显示msg内容