Required parameter 'tag' is missing PHP

时间:2015-08-07 01:58:42

标签: php android android-volley

i get this error Required parameter 'tag' is missing on android toast please help ,me i can't find my fault below i have android user code and the php server code above i use volley library for httprequests

StringRequest strReq = new StringRequest(Request.Method.POST,
        AppConfig.URL_REGISTER, new Response.Listener<String>() {

    @Override
    public void onResponse(String response) {
        Log.d(TAG, "Register Response: " + response.toString());
        hideDialog();
        try {
            JSONObject jObj = new JSONObject(response);
            boolean error = jObj.getBoolean("error");
            if (!error) {
                // User successfully stored in MySQL
                // Now store the user in sqlite
                String uid = jObj.getString("uid");

                JSONObject user = jObj.getJSONObject("user");
                String name = user.getString("name");
                String email = user.getString("email");
                String created_at = user
                        .getString("created_at");

                // Inserting row in users table
                db.addUser(name, email, uid, created_at);

                // Launch login activity
                Intent intent = new Intent(
                        RegisterActivity.this,
                        LoginActivity.class);
                startActivity(intent);
                finish();
            } else {

                // Error occurred in registration. Get the error
                // message
                String errorMsg = jObj.getString("error_msg");
                Toast.makeText(getApplicationContext(),
                        errorMsg, Toast.LENGTH_LONG).show();
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }
}, new Response.ErrorListener() {

    @Override
    public void onErrorResponse(VolleyError error) {
        Log.e(TAG, "Registration Error: " + error.getMessage());
        Toast.makeText(getApplicationContext(),
                error.getMessage(), Toast.LENGTH_LONG).show();
        hideDialog();
    }
})
{

    @Override
    protected Map<String, String> getParams() {
        // Posting params to register url
        Map<String, String> params = new HashMap<String, String>();
        params.put("tag", "register");
        params.put("name", name);
        params.put("email", email);
        params.put("password", password);

        return params;
    }

    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json; charset=utf-8");
        return headers;
    }
};

// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);

}

AppcontrollerClass here

public class AppController extends Application {

    public static final String TAG = AppController.class.getSimpleName();

    private RequestQueue mRequestQueue;

    private static AppController mInstance;

    @Override
    public void onCreate() {
        super.onCreate();
        mInstance = this;
    }

    public static synchronized AppController getInstance() {
        return mInstance;
    }

    public RequestQueue getRequestQueue() {
        if (mRequestQueue == null) {
            mRequestQueue = Volley.newRequestQueue(getApplicationContext());
        }

        return mRequestQueue;
    }

    public <T> void addToRequestQueue(Request<T> req, String tag) {
        req.setTag(TextUtils.isEmpty(tag) ? TAG : tag);
        getRequestQueue().add(req);
    }

    public <T> void addToRequestQueue(Request<T> req) {
        req.setTag(TAG);
        getRequestQueue().add(req);
    }

    public void cancelPendingRequests(Object tag) {
        if (mRequestQueue != null) {
            mRequestQueue.cancelAll(tag);
        }
    }
}

this is android log

  08-07 14:35:36.552  19120-20088/com.example.panos.chatsmartapp D/Volley﹕ [7445] BasicNetwork.logSlowRequests: HTTP response for request=<[ ] http://192.168.1.3/android_login_api/ 0x355f917 NORMAL 1> [lifetime=3858], [size=65], [rc=200], [retryCount=0]
08-07 14:35:36.662  19120-19120/com.example.panos.chatsmartapp D/RegisterActivity﹕ Register Response: {"error":true,"error_msg":"Required parameter 'tag' is missing!"}
08-07 14:35:36.702  19120-19120/com.example.panos.chatsmartapp D/Toast﹕ checkMirrorLinkEnabled returns : false
08-07 14:35:36.702  19120-19120/com.example.panos.chatsmartapp D/Toast﹕ showing allowed
08-07 14:35:36.702  19120-19120/com.example.panos.chatsmartapp D/Volley﹕ [1] Request.finish: 4253 ms: [ ] http://192.168.1.3/android_login_api/ 0x355f917 NORMAL 1
08-07 14:35:36.712  19120-19120/com.example.panos.chatsmartapp E/ViewRootImpl﹕ sendUserActionEvent() mView == null

this is php code

<?php
if (isset($_POST['tag']) && $_POST['tag'] != '') {
    // get tag
    $tag = $_POST['tag'];

    // include db handler
    require_once 'include/DB_Functions.php';
    $db = new DB_Functions();

    // response Array
    $response = array("tag" => $tag, "error" => FALSE);

    // check for tag type
    if ($tag == 'login') {
        // Request type is check Login
        $email = $_POST['email'];
        $password = $_POST['password'];

        // check for user
        $user = $db->getUserByEmailAndPassword($email, $password);
        if ($user != false) {
            // user found
            $response["error"] = FALSE;
            $response["uid"] = $user["unique_id"];
            $response["user"]["name"] = $user["name"];
            $response["user"]["email"] = $user["email"];
            $response["user"]["created_at"] = $user["created_at"];
            $response["user"]["updated_at"] = $user["updated_at"];
            echo json_encode($response);
        } else {
            // user not found
            // echo json with error = 1
            $response["error"] = TRUE;
            $response["error_msg"] = "Incorrect email or password!";
            echo json_encode($response);
        }
    } else if ($tag == 'register') {
        // Request type is Register new user
        $name = $_POST['name'];
        $email = $_POST['email'];
        $password = $_POST['password'];

        // check if user is already existed
        if ($db->isUserExisted($email)) {
            // user is already existed - error response
            $response["error"] = TRUE;
            $response["error_msg"] = "User already existed";
            echo json_encode($response);
        } else {
            // store user
            $user = $db->storeUser($name, $email, $password);
            if ($user) {
                // user stored successfully
                $response["error"] = FALSE;
                $response["uid"] = $user["unique_id"];
                $response["user"]["name"] = $user["name"];
                $response["user"]["email"] = $user["email"];
                $response["user"]["created_at"] = $user["created_at"];
                $response["user"]["updated_at"] = $user["updated_at"];
                echo json_encode($response);
            } else {
                // user failed to store
                $response["error"] = TRUE;
                $response["error_msg"] = "Error occured in Registartion";
                echo json_encode($response);
            }
        }
    } else {
        // user failed to store
        $response["error"] = TRUE;
        $response["error_msg"] = "Unknow 'tag' value. It should be either 'login' or 'register'";
        echo json_encode($response);
    }
} 
else {
    $response["error"] = TRUE;
    $response["error_msg"] = "Required parameter 'tag' is missing!";
    echo json_encode($response);
}

?>

Myabe i have problem with utf8 string using json_encode/json_decode I do not know...

2 个答案:

答案 0 :(得分:0)

您可以尝试使用post方法创建一个html表单并尝试点击URL。如果响应正确,则表示您在Android代码中通过POST调用提交的FORM不正确。

<html><head>
</head>
<body>
<form name="form" action="index.php" method="post">
<input name="name" type="tezt" value="Porcupine"/>
<input name="tag" type="hidden" value="register"/>
<input name="email" type="text" value="Porcupine@gmail.com"/>
<input name="password" type="password" value=""/>
<input type="submit" value="send">
</form>
</body>

您可以使用此功能。请相应地替换操作URL

答案 1 :(得分:0)

删除以下代码

 @Override
public Map<String, String> getHeaders() throws AuthFailureError {
    HashMap<String, String> headers = new HashMap<String, String>();
    headers.put("Content-Type", "application/json; charset=utf-8");
    return headers;
}
相关问题