Skip to content Skip to sidebar Skip to footer

Android Volley Getparams() Method Not Getting Called For Jsonobjectrequest

I have overrided getParams(), and mEmail, mUsername etc. are globally declared. @Override public Map getParams() throws AuthFailureError { Map

Solution 1:

use this Custom volley request class

import com.android.volley.NetworkResponse;
import com.android.volley.ParseError;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.UnsupportedEncodingException;
import java.util.Map;

publicclassCustomJsonRequestextendsRequest<JSONObject> {

    privateListener<JSONObject> listener;
    privateMap<String, String> params;

    publicCustomJsonRequest(String url, Map<String, String> params,
                             Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(Method.GET, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    publicCustomJsonRequest(int method, String url, Map<String, String> params,
                             Listener<JSONObject> reponseListener, ErrorListener errorListener) {
        super(method, url, errorListener);
        this.listener = reponseListener;
        this.params = params;
    }

    @OverrideprotectedMap<String, String> getParams() throws com.android.volley.AuthFailureError {
        return params;
    }

    ;

    @OverrideprotectedResponse<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = newString(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            returnResponse.success(newJSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            returnResponse.error(newParseError(e));
        } catch (JSONException je) {
            returnResponse.error(newParseError(je));
        }
    }

    @OverrideprotectedvoiddeliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

you can use it with

Map<String,String> params = newHashtable<String, String>();

params.put("Email", mEmail.getText().toString().trim());
params.put("Username",mUsername.getText().toString().trim());
params.put("Password",mPassword.getText().toString().trim());
params.put("BirthDay",mBirthday.getText().toString().replaceAll("\\s","-"));
params.put("Sex",SelectedRadio());
params.put("Bitmap",getEncodedBitmap());



CustomJsonRequest request = newCustomJsonRequest(Request.Method.POST, url, params,
        newResponse.Listener<JSONObject>() {
            @OverridepublicvoidonResponse(JSONObject response) {

            }
        }, newResponse.ErrorListener() {
    @OverridepublicvoidonErrorResponse(VolleyError error) {

    }
});

request.setRetryPolicy(newDefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 2, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
AppController.getInstance().getRequestQueue().add(request);

Actually this a issue with sending parameters with volley. Check this answer https://stackoverflow.com/a/27091088/1320616

EDIT: Explanation for this behaviour

your JsonObjectRequest class extends from JsonRequest which extends from Request.

inside your Request class there is a method

/**
     * Returns the raw POST or PUT body to be sent.
     *
     * <p>By default, the body consists of the request parameters in
     * application/x-www-form-urlencoded format. When overriding this method, consider overriding
     * {@link #getBodyContentType()} as well to match the new body format.
     *
     * @throws AuthFailureError in the event of auth failure
     */publicbyte[] getBody() throws AuthFailureError {
        Map<String, String> params = getParams();
        if (params != null && params.size() > 0) {
            return encodeParameters(params, getParamsEncoding());
        }
        returnnull;
    }

notice that this method is calling getParams() method. It is the same method that you are overriding while making the call.

But if you look inside JsonRequest class, there is a method

@Override
    publicbyte[] getBody() {
        try {
            return mRequestBody == null ? null : mRequestBody.getBytes(PROTOCOL_CHARSET);
        } catch (UnsupportedEncodingException uee) {
            VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s",
                    mRequestBody, PROTOCOL_CHARSET);
            returnnull;
        }
    }

it means the getBody() of Request has been overridden by getBody() of JsonRequest class which means your getParams() will never get called. So you need a custom class which directly extends Request class and doesn't override the getBody() method of Request class.

Post a Comment for "Android Volley Getparams() Method Not Getting Called For Jsonobjectrequest"