【问题标题】:Why doesn't android studio follow through the response listener?为什么android studio不跟随响应监听器?
【发布时间】:2018-11-06 14:56:54
【问题描述】:

我在 Android Studio 中有一个简单的注册脚本。我将 php 脚本设置为在代码正确运行时发送响应。 Android Studio 应该收到响应,但没有。

这里是有问题的 PHP 块;

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

    echo $response;

这里是有问题的 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);
            }
        });
    }

这里是注册请求;

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+"");
    }

【问题讨论】:

    标签: java php android android-studio


    【解决方案1】:

    您不能在 PHP 中回显数组。当你这样做时,输出将是

    PHP 注意:第 1 行 php shell 代码中数组到字符串的转换
    数组

    您需要直接发送字符串“success”,例如

    echo "success";
    

    或将数组序列化为 JSON,但随后您必须更改您的 ResponseListener 以便它可以理解这一点,可能使用 Jackson 或 Android 中可用的任何东西。

    更好的选择是不读取实际的响应正文,而只需设置 appropriate HTTP status code 以指示成功或失败。

    【讨论】:

    • 谢谢,但另一个错误是我的脚本无论如何都没有激活 OnResponse 方法覆盖。这会解决它吗?
    • @AlecHarvey 我不熟悉 Android 编程,所以我不知道。
    猜你喜欢
    • 2011-08-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-26
    • 1970-01-01
    相关资源
    最近更新 更多