无法使用齐射发布到服务器

时间:2019-04-24 14:41:15

标签: java php android android-volley

我已经尝试了很多教程来发布到服务器,但是什么也没有发生,当我在邮递员中尝试时,它运行良好,但是当我在android中进行测试时,它却没有效果。

这是我的java方法

private void checkLogin(final String username, final String password) {
        // Tag used to cancel the request
        String tag_string_req = "req_login";

        progressDialog.setMessage("Logging in ...");
        showDialog();

        StringRequest stringRequest = new StringRequest(Request.Method.POST, ApiService.login, response -> {
            Log.d(TAG, "Login Response: " + response);
            hideDialog();
            try {
                JSONObject jsonObject = new JSONObject(response);
                boolean error = jsonObject.getBoolean("error");
                // Check for error node in json
                if (!error) {
                    session.setLogin(true);

                    String uid = jsonObject.getString("uid");
                    JSONObject user = jsonObject.getJSONObject("user");
                    String username1 = user.getString("username");
                    String email = user.getString("email");
                    String city = user.getString("city");
                    String subdistrict = user.getString("subdistrict");
                    String name = user.getString("name");
                    String nickname = user.getString("nickname");
                    String address = user.getString("address");
                    String phone = user.getString("phone");
                    String birth_date = user.getString("birth_date");
                    String gender = user.getString("gender");
                    String created_at = user.getString("created_at");
                    String updated_at = user.getString("updated_at");
                    String weight = user.getString("weight");
                    String height = user.getString("height");
                    String prohibition = user.getString("prohibition");

                    Toast.makeText(Login.this, "Successfully logged in, congrats!", Toast.LENGTH_SHORT).show();

                    // Inserting row in users table
                    db.addUser(uid, username1, email, city, subdistrict, name, nickname, address, phone, birth_date, gender, created_at, updated_at);
                    db.updateMedicalInfo(weight, height, prohibition, updated_at);
                    // Launch main activity
                    Intent intent = new Intent(Login.this, Home.class);
                    startActivity(intent);
                    finish();
                } else {
                    // Error in login_view. Get the error message
                    String errorMsg = jsonObject.getString("error_msg");
                    Toast.makeText(getApplicationContext(), errorMsg, Toast.LENGTH_LONG).show();
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }

        }, error -> {
            Log.e(TAG, "Login Error: " + error.getMessage());
            Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show();
            hideDialog();
        }) {
            @Override
            public String getBodyContentType() {
                return "application/x-www-form-urlencoded; charset=UTF-8";
            }

            @Override
            protected Map<String, String> getParams() {
                // Posting parameters to login_view url
                Map<String, String> params = new HashMap<>();
                params.put("tag", "login");
                params.put("username", username);
                params.put("password", password);
                return params;
            }
        };
        // Adding request to request queue
        AppController.getInstance().addToRequestQueue(stringRequest, tag_string_req);
    }

这是我的php代码

<?php
// Request type is check Login
define('root', $_SERVER['DOCUMENT_ROOT']); 
    require_once(root.'\dion\api\function\AuthFunctions.php'); 
    $db = new AuthFunctions();
    // response Array
    $response = array("tag" => $tag, "error" => FALSE);
        if (isset($_REQUEST['username']) && isset($_REQUEST['password'])) {
            // receiving the post params
            // menangkap data yang dikirimkan sebelumnya -> POST
            $username = $_REQUEST['username'];
            $password = $_REQUEST['password'];
            // get the user by email and password
            // menangkap data yang dikirimkan sebelumnya -> POST
            $user = $db->getUserByUsernameAndPassword($username, $password);
            if ($user != false) {
                // use is found
                // jika user telah ditemukan dan cocok pada database, maka akan dimunculkan data user
                $response["error"] = FALSE;
                $response["uid"] = $user["unique_id"];
                $response["user"]["username"] = $user["username"];
                $response["user"]["email"] = $user["email"];
                $response["user"]["city"] = $user["city"];
                $response["user"]["subdistrict"] = $user["subdistrict"];
                $response["user"]["name"] = $user["name"];
                $response["user"]["nickname"] = $user["nickname"];
                $response["user"]["address"] = $user["address"];
                $response["user"]["phone"] = $user["phone"];
                $response["user"]["birth_date"] = $user["birth_date"];
                $response["user"]["gender"] = $user["gender"];
                $response["user"]["weight"] = $user["weight"];
                $response["user"]["height"] = $user["height"];
                $response['user']['prohibition'] = $user['prohibition'];
                $response["user"]["created_at"] = $user["created_at"];
                $response["user"]["updated_at"] = $user["updated_at"];
                // ditampilkan dalam bentuk json 
                echo json_encode($response);
            } else {
                // user is not found with the credentials
                // jika user tidak ditemukan maka akan muncul pesan 
                $response["error"] = TRUE;
                $response["error_msg"] = "Login not matched. Please try again!";
                echo json_encode($response);
            }
        } else {
            // required post params is missing
            // jika tidak ada inputan untuk login
            $response["error"] = TRUE;
            $response["error_msg"] = "Required parameters email or password is missing!";
            echo json_encode($response);
        }
        ?>

当我使用邮递员时,我会收到这样的json响应Postman result 但是当我在android中尝试时,它说没有发送参数。

0 个答案:

没有答案