Pass a JsonObject in Volley as POST parameter
Asked Answered
H

2

0

I have an API endpoint which needs parameters to be sent as JsonObject in a POST request.

The response which I will get is not Json, but rather a small CSV string.

The Code is

stringRequest = new StringRequest(Request.Method.GET, "http://apitest.eezyrent.com/api/userauthentication/SignUp",
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {
                        // Display the first 500 characters of the response string.
                        Toast.makeText(getApplication(), response.toString(), Toast.LENGTH_SHORT).show();
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                Toast.makeText(getApplication(), error.getMessage(), Toast.LENGTH_SHORT).show();
            }
        }) {

            @Override
            protected Map<String, String> getParams() throws AuthFailureError {
                Map<String, String> jsonParams2 = new HashMap<String, String>();
                jsonParams2.put("vcFName", firstname.getText().toString());
                jsonParams2.put("vcLname", lastname.getText().toString());
                jsonParams2.put("vcMobileNo", phone_no.getText().toString());
                jsonParams2.put("vcGender", gender_short);
                jsonParams2.put("vcEmailAddress", email.getText().toString());
                jsonParams2.put("vcPassword", password.getText().toString());
                jsonParams2.put("vcFBID", "");
                jsonParams2.put("intLoginUserID", "");
                jsonParams2.put("SignUpFrom", "Web");
                jsonParams2.put("intloginid", "");
                jsonParams2.put("AlreadyRegister", "");
                return jsonParams2;
            }

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

The above code is heavily inspired by using answers from this community, but this does not seem to solve the problem for me.

I get Volley Error 405.

 E/Volley﹕ [112549] BasicNetwork.performRequest: Unexpected response code 405 for http://myurl

Infact If I used AsyncTask instead of Volley, With the same json as parameters and the same endpoint url. It works!! The AsyncTask code was found from this question. Java HttpClient changing content-type?

But I want to use Volley, What could be the solution to this?

Sample JSON Object

{"vcFName":"Ron","vcLname":"Weasley","vcMobileNo":"555888999","vcGender":"M","vcEmailAddress":"[email protected]","vcPassword":"123456","vcFBID":"","intLoginUserID":'',"SignUpFrom":"Web","intloginid":"","AlreadyRegister":""}
Hindward answered 7/9, 2015 at 11:35 Comment(4)
you can make JsonObjectRequest see this androidhive.info/2014/09/android-json-parsing-using-volleyPolarity
@KaranMer I have tried that and I get the same error. Also JsonObjectRequest is not usefull for me since the response is CSV string and not JSON string.Hindward
in that case you need to make custom request see this SO thread #30859544Polarity
@KaranMer but again that helper class is for JsonArrayRequest and not StringRequest. Can You modify that helper class since I donot understand it.Hindward
R
0

You need to use JsonObjectRequest class. Pass your params as json object in the 3rd parameter of the JsonObjectRequest class. Below is a small snippet:

 Map<String, String> jsonParams2 = new HashMap<String, String>();
            jsonParams2.put("vcFName", firstname.getText().toString());
            jsonParams2.put("vcLname", lastname.getText().toString());
            jsonParams2.put("vcMobileNo", phone_no.getText().toString());
            jsonParams2.put("vcGender", gender_short);
            jsonParams2.put("vcEmailAddress", email.getText().toString());
            jsonParams2.put("vcPassword", password.getText().toString());
            jsonParams2.put("vcFBID", "");
            jsonParams2.put("intLoginUserID", "");
            jsonParams2.put("SignUpFrom", "Web");
            jsonParams2.put("intloginid", "");
            jsonParams2.put("AlreadyRegister", "");

JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
        YourUrl, new JsonObject(jsonParams2),
        new Response.Listener<JSONObject>() {

            @Override
            public void onResponse(JSONObject response) {

            }
        }, new Response.ErrorListener() {

            @Override
            public void onErrorResponse(VolleyError error) {

            }
        }) {
    @Override
    public Map<String, String> getHeaders() throws AuthFailureError {
        HashMap<String, String> headers = new HashMap<String, String>();
        headers.put("Content-Type", "application/json");
        headers.put( "charset", "utf-8");
        return headers;
    }
};
Rebbecarebbecca answered 7/9, 2015 at 12:14 Comment(1)
As I replied to @KaranMer, I have already tried this. and found the same error. Thanks!Hindward
B
0

Step 1: Make model class parsable/Serializable.
Step 2: Override toString() in model class.
Step 3: Map<String,JSONObject> params = new HashMap<>(); JSONObject object = null; try{ object = new JSONObject(classObject.toString()); }catch (Exception e){ } params.put("key", object); JSONObject objectParams = new JSONObject(params);
Step 4: Send objectParams with volley JSONObject request.
Done!!!

Beacon answered 23/8, 2017 at 19:6 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.