简体   繁体   English

PHP json_encoded 消息与 java json 对象匹配

[英]PHP json_encoded message match with java json object

I'm trying to make response listener for my Android app.我正在尝试为我的 Android 应用程序制作响应侦听器。

I'm trying to get a response from my php code likes this:我试图从我的 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();
        }
    }   
};

The PHP response looks like this: PHP 响应如下所示:

$response = array();
$response["no"];  
echo json_encode($response);

If I change the php array to $response["no"] = true;如果我将 php 数组更改为$response["no"] = true; and make a boolean then I can get it to work like this:并创建一个布尔值然后我可以让它像这样工作:

 boolean success2 = jsonResponse.getboolean("no");

but this won't do because then I can only have two scenarios.但这不行,因为那样我只能有两种情况。 Either failed or succeeded.要么失败,要么成功。 But what I am trying to achieve is creating more scenarios stating like if it fails then specify why it fails.但是我想要实现的是创建更多的场景,说明如果失败然后指定失败的原因。 Like in this example.就像在这个例子中一样。 If the user is trying to register and is not 18 years old yet then pop up a message stating that is the problem.如果用户正在尝试注册并且尚未年满 18 岁,则会弹出一条消息,说明这是问题所在。

Therefor I need to send some kind of JSON encoded message from PHP to Java that I in Java can check up on.因此,我需要将某种 JSON 编码的消息从 PHP 发送到 Java,我可以在 Java 中进行检查。

Does anyone know how I can achieve this?有谁知道我如何实现这一目标?

Try json response content like this:尝试这样的 json 响应内容:

{
    success: true,
    msg: "Here can be anything you want to display."
}

If success is true, then register, else display msg content.如果成功,则注册,否则显示msg内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM