Anwar Elsayed
Anwar Elsayed

Reputation: 493

Android - Volley fails to send my Params to the server

I am trying send data to server using Volley library but it gives me an error

"end of input at character 0 of "

and here is my code

public void postPrams(View view) {
        String tag_json_obj = "json_obj_req";

        String url = "http://Urlhere.com/register.php";

        final ProgressDialog pDialog = new ProgressDialog(this);
        pDialog.setMessage("Loading...");
        pDialog.show();

        JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
                url, null,
                new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        text.setText("done Post : "+response);
                        pDialog.hide();
                        Toast.makeText(getApplication(),"Done",Toast.LENGTH_LONG).show();

                    }
                }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {
                VolleyLog.d("erorr", "Error: " + error.getMessage());
                Toast.makeText(getApplication(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
                pDialog.hide();
            }
        }) {

            @Override
            protected Map<String, String> getParams() {
                Map<String, String> params = new HashMap<String, String>();
                params.put("first_name","Anwar");
                params.put("last_name","Samir");
                params.put("age", "1000");
                params.put("country", "egypt");
                params.put("city","le");
                params.put("street", "10sq");
                params.put("mobile_no", "0100000");
                params.put("login_name", "Asi");
                params.put("password", "123qwe");
                return params;
            }
        };

        AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
    }

Please help me why this is happening.

Upvotes: 3

Views: 175

Answers (4)

W.K.S
W.K.S

Reputation: 10095

Have a look at Volley's source

public JsonObjectRequest(int method, String url, JSONObject jsonRequest,Listener<JSONObject> listener, ErrorListener errorListener)

You're passing null in the place of jsonRequest meaning that in fact you are not passing any data with the POST request. Hence the error: "end of input at character 0 of. Try changing your code to

public void postPrams(View view) {

    String tag_json_obj = "json_obj_req";

    String url = "http://Urlhere.com/register.php";

    final ProgressDialog pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();

    JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
            url, getParams(),
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    text.setText("done Post : "+response);
                    pDialog.hide();
                    Toast.makeText(getApplication(),"Done",Toast.LENGTH_LONG).show();

                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
            VolleyLog.d("erorr", "Error: " + error.getMessage());
            Toast.makeText(getApplication(),error.getMessage().toString(),Toast.LENGTH_LONG).show();
            pDialog.hide();
        }
    });

    AppController.getInstance().addToRequestQueue(jsonObjReq, tag_json_obj);
}

private JSONObject getParams(){
    JSONObject params = new JSONObject();
    params.put("first_name","Anwar");
    params.put("last_name","Samir");
    params.put("age", "1000");
    params.put("country", "egypt");
    params.put("city","le");
    params.put("street", "10sq");
    params.put("mobile_no", "0100000");
    params.put("login_name", "Asi");
    params.put("password", "123qwe");
    return params;
}

Upvotes: 1

Rohit Patil
Rohit Patil

Reputation: 1068

try this

public void postDataVolley(Context context,String url,JSONObject sendObj){
try {
    RequestQueue queue = Volley.newRequestQueue(context);

    JsonObjectRequest jsonObj = new JsonObjectRequest(url,sendObj, new Response.Listener<JSONObject>() {
        @Override
        public void onResponse(JSONObject response) {
            Log.d("Volley", "Volley JSON post" + response);
        }
    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            Log.d("Volley", "Volley JSON post" + "That didn't work!");
        }
    });

    queue.add(jsonObj);

}catch(Exception e){

}
}

Upvotes: 0

Ravi Theja
Ravi Theja

Reputation: 3401

 JSONObject params = new JSONObject();
 params.put("first_name","Anwar");
 params.put("last_name","Samir");
 params.put("age", "1000");
 params.put("country", "egypt");
 params.put("city","le");
 params.put("street", "10sq");
 params.put("mobile_no", "0100000");
 params.put("login_name", "Asi");
 params.put("password", "123qwe");

replace null with params. You are not able to receive at server side since you are not sending any body.The Map in which you placed your params is for headers not the body.You have to send the body in

JsonObjectRequest request = newJsonObjectRequest(method,url,body,listener){
   ....headers here
};

Upvotes: 0

Joan Sanchez
Joan Sanchez

Reputation: 331

Look a the request method. Post or get?? Are you returning a vallue? or blank?.

In you server side try to do echo to parameters.

Upvotes: 0

Related Questions