Android Studio无法与我的php脚本通信 [英] Android studio isn't communicating to my php script

查看:71
本文介绍了Android Studio无法与我的php脚本通信的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个Java脚本,应该转到 http://localhost/phptesting/Register.php .我的脚本的URL链接了它,但是当我发送注册请求时,什么也没有发生.日志中什么也没有发生.

I have a 2 java scripts that should go to a php file located on http://localhost/phptesting/Register.php. My script's URL links it, but when I send a register request, nothing happens. Nothing even happens in the logs.

这是我的注册请求:

    public class RegisterRequest extends StringRequest {

    private static final String REGISTER_REQUEST_URL = "http://localhost/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+"");
    }

    public Map<String, String> getparams() {
        return params;
    }
}

这是我的注册活动脚本:

and here is my register activity script:

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) {
                        try {
                            JSONObject jsonResponse = new JSONObject(response);
                            boolean success = jsonResponse.getBoolean("success");
                            if (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();


                            }
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                };
                RegisterRequest registerRequest = new RegisterRequest(username,password,isadmin,responseListener);
                RequestQueue queue = Volley.newRequestQueue(CreateUser.this);
                queue.add(registerRequest);
            }
        });

我的php脚本是这个;

my php script is this;

<?php
    $db_host = 'localhost:3306';
    $db_user = 'root';
    $db_pass = '';
    $db_name = 'test';

    $con = mysqli_connect($db_host,'user',$db_pass,$db_name);
    if($con){
        echo "connection successful";
    }else{
        echo "connection failed";
    }

    $age = $_POST["isAdmin"];
    $username = $_POST["username"];
    $password = $_POST["password"];
    $statement = mysqli_prepare($con, "INSERT INTO user (username,password,isAdmin) VALUES (?, ?, ?)");
    mysqli_stmt_bind_param($statement, "siss",$username,$password,$isAdmin);
    mysqli_stmt_execute($statement);

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

    echo json_encode($response);
?>

感谢所有帮助.

推荐答案

我在此代码上看到至少两个错误:

I see at least two errors on this code:

private static final String REGISTER_REQUEST_URL = "http://localhost/phptesting/Register.php";

Android不喜欢 localhost ,您可以在此处阅读.

Android doesn't like localhost you can read it here.

最好的选择是执行一个 ifconfig 并获取您的 ip地址,而不是 localhost 放置您的 ip地址

Best option is to do an ifconfig and get your ip address and instead of localhost put your ip address like

http://192.168.1.158/phptesting/Register.php

您的本地主机也应该有一个端口,对吗?因此,请确保在 BASE_URL 中添加以防万一,以便它看起来像这样

Also your localhost should have a port, right? So make sure you add it just in case in your BASE_URL, so it should look like this

http://192.168.1.158:8080/phptestig/Register.php

然后通话应该可以正常工作.

Then the call should work.

然后我看到了

mysqli_stmt_bind_param($statement, "siss",$username,$password,$isAdmin);

您正在将4个值传递给需要3的插入.

You are passing 4 values to an insert that requires 3.

我建议您逐步进行操作,首先确保您的api调用正在注册某些内容(您可以使用PostMan或ARC进行注册),然后转到Android.

I recommend you to go step by step, first make sure your api call is registering something (you can use PostMan or ARC to do it) and then go to Android side.

这篇关于Android Studio无法与我的php脚本通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆