Volley没有向PHP发送变量

时间:2014-12-04 00:25:17

标签: java php android android-volley

我正在尝试使用PHP脚本进行排球,基本上通过凌空提交一些数据到php然后从数据库中选择并返回结果,但是我很难从android获取变量提交到php 。我得到的错误表明,当php运行时变量为null,因此它们无法正确提交 这是php错误

PHP Notice:  Undefined index: name

当我尝试返回变量name时,它返回null

这是php

$iName = $_POST['name'];
$iEmail = $_POST['email'];
$iPassword = $_POST['password'];
$iVerifyPassword = $_POST['verify_password'];


$response = array('code' => "1", 'message' => $iName);

echo json_encode($response);

exit;

这是相关的android

HashMap<String, String> params = new HashMap<String, String>();
params.put("name", sName);
params.put("email", sEmail);
params.put("password", sPassword);
params.put("verify_password", sVerifyPassword);

JsonObjectRequest req = new JsonObjectRequest(
    jsonUrl, new JSONObject(params),
    new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            try {
                VolleyLog.v("Response:%n %s", response.toString(4));
                int code = response.getInt("code");
                String message = response.getString("message");

                if (code == 1) {
                    //Success go to verify
                    Toast.makeText(getActivity().getApplicationContext(),
                            message,
                            Toast.LENGTH_LONG).show();

                } else if (code == 2) {

                    Toast.makeText(getActivity().getApplicationContext(),
                            message,
                            Toast.LENGTH_LONG).show();
                } else {
                    Toast.makeText(getActivity().getApplicationContext(),
                            "Network Error",
                            Toast.LENGTH_LONG).show();

                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        }
    },
    new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.e("Error: ", error.getMessage());
        }
    }
);

// add the request object to the queue to be executed
AppController.getInstance().addToRequestQueue(req, tag_json_obj);

1 个答案:

答案 0 :(得分:1)

JsonObjectRequest不会将您的参数提交给服务器,这会导致您的问题。很多人都问过这类问题,我的另一个answer可以让你更清楚为什么。