1

i am trying to get volley to work with a php script, to basically submit some data via volley to the php then select from database and return result, however i am struggling to even get the variables from android to submit to the php. The errors i am getting signal that the variables are null when the php is run, so they are not getting submitted properly Here is the php error

PHP Notice:  Undefined index: name

And when i try to return the variable name it returns as null

Here is the 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;

Here is the relevant 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);
VinceStyling
  • 3,707
  • 3
  • 29
  • 44
Al Hennessey
  • 2,395
  • 8
  • 39
  • 63

1 Answers1

1

The JsonObjectRequest won't submit your parameters to the server, which cause your problem. This sort of questions has been asked by many guys, my another answer can make you more clear about why.

Community
  • 1
  • 1
VinceStyling
  • 3,707
  • 3
  • 29
  • 44
  • Ok that looks great, i am going to give that a try, just out of curiosity do i set the params with hashmap and pass them into the custom request or do i just add them to a jsonobject. Also where do i conduct all the onResponse code? Thanks for the help – Al Hennessey Dec 04 '14 at 22:51
  • you can trace the source code, then you can figure out that how the `JsonRequest` dealing with your parameters **HashMap**, its code were not large so quite easy to understand. – VinceStyling Dec 05 '14 at 02:52
  • ok cool, i got there in the end, when i added the code from the link you gave me, it allowed me to create the onresponse methods, and the hashmap worked great. so thanks again – Al Hennessey Dec 05 '14 at 22:10