简体   繁体   English

为什么android studio不跟踪响应侦听器?

[英]Why doesn't android studio follow through the response listener?

I have a simple register script in Android studio.我在 Android Studio 中有一个简单的注册脚本。 I have the php script set to send a response when the code ran correctly.我将 php 脚本设置为在代码正确运行时发送响应。 Android studio SHOULD receive a response, but doesn't. Android studio 应该收到响应,但没有。

here is the PHP chunk in question;这是有问题的 PHP 块;

$response = array();
    $response["success"] = true;  

    echo $response;

And here is the CreateUser chunk in question;这是有问题的 CreateUser 块;

createuser.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                final String username = username1.getText().toString();
                final String password = password1.getText().toString();
                final String isadmin = isAdmin.getText().toString();
                Response.Listener<String> responseListener = new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        Log.d("Response Value: ", response);
                            if (response.equals("success")){
                                Intent intent = new Intent(CreateUser.this, MainActivity.class);
                                CreateUser.this.startActivity(intent);
                            }else{
                                AlertDialog.Builder builder = new AlertDialog.Builder(CreateUser.this);
                                builder.setMessage("Register Failed")
                                        .setNegativeButton("Retry",null)
                                        .create()
                                        .show();
                        }
                    }
                };
                RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener);
                RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
                queue.add(registerRequest);
            }
        });
    }

and here is the registerrequest;这是注册请求;

public class RegisterRequest extends StringRequest {

    private static final String REGISTER_REQUEST_URL = "http://192.168.*.*:80/phptesting/Register.php";
    private Map<String, String> params;
    public RegisterRequest(String username, String password,String isAdmin, Response.Listener<String> listener){
        super(Method.POST, REGISTER_REQUEST_URL,listener,null);
        params = new HashMap<>();
        params.put("username",username);
        params.put("password",password);
        params.put("isAdmin",isAdmin+"");
    }

You cannot echo an array in PHP.您不能在 PHP 中回显数组。 When you do that, the output will be当你这样做时,输出将是

PHP Notice: Array to string conversion in php shell code on line 1 PHP 注意:第 1 行 php shell 代码中数组到字符串的转换
Array大批

You would need to send the string "success" directly, eg您需要直接发送字符串“成功”,例如

echo "success";

Or serialize the array to JSON, but then you will have to change your ResponseListener so that it can understand that, probably with Jackson or whatever is available in Android.或者将数组序列化为 JSON,但随后您将不得不更改您的ResponseListener以便它可以理解这一点,可能使用 Jackson 或 Android 中可用的任何内容。

A better alternative would be not to read the actual response body but just set the appropriate HTTP status code to indicate success or failure.更好的替代方法是不读取实际的响应正文,而只是设置适当的 HTTP 状态代码来指示成功或失败。

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

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