1

I dont know why the getParams() is not working in my method?

The System.out.println works fine under getHeaders but not under getParams?

    //---------------------------------------POST request with headers---------------------------------------
public void post(String url, final String param1, final String param2, String source) {

    // Request a string response from the provided URL.
    JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url,
            getReponse(source), createMyReqErrorListener()) {

        @Override
        protected Map<String, String> getParams() throws AuthFailureError {
            HashMap<String, String> params = new HashMap<>();
            params.put("loginAlias", "username");
            params.put("loginPassword", "12345");
            System.out.println("Params are: " + params.toString());
            return params;
        }

        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<>();
            headers.put(ACCEPT, ACCEPT_JSON); //Accepting JSON
            headers.put(AUTH_ID, AUTH_ID_VALUE);  
            headers.put(PLATFORM, PLATFORM_VALUE); 
            headers.put(CID, ""); 
            headers.put(DEVICE_TYPE_MOBILE, DEVICE_TYPE_MOBILE_VALUE); 
            System.out.println("Headers are: " + headers.toString());
            return headers;
        }
    };
// Add the request to the RequestQueue.
    queue.add(request);
}

I have followed all the Google Volley documentation and tried a couple options on SO but for some reason, this does not work?

Thanks

Stillie
  • 2,647
  • 6
  • 28
  • 50

1 Answers1

1

also faced the same issue, Finally I found that there is some issue with volley - JSONObject request. (after few googling!)

the getParams() doesn't invoke because JsonObjectRequest extended JsonRequest which invoke getBody() directly to encoding the constructor second parameter(call requestBody) as contentType, that's why it ignore your getParam() method.

try this solution, it resolved my problem.

import java.io.UnsupportedEncodingException;
import java.util.Map;    
import org.json.JSONException;
import org.json.JSONObject;    
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;

public class CustomRequest extends Request<JSONObject> {

    private Listener<JSONObject> listener;
    private Map<String, String> params;

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

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

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

    @Override
    protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
        try {
            String jsonString = new String(response.data,
                    HttpHeaderParser.parseCharset(response.headers));
            return Response.success(new JSONObject(jsonString),
                    HttpHeaderParser.parseCacheHeaders(response));
        } catch (UnsupportedEncodingException e) {
            return Response.error(new ParseError(e));
        } catch (JSONException je) {
            return Response.error(new ParseError(je));
        }
    }

    @Override
    protected void deliverResponse(JSONObject response) {
        // TODO Auto-generated method stub
        listener.onResponse(response);
    }
}

In activity/fragment do use this

RequestQueue requestQueue = Volley.newRequestQueue(getActivity());
CustomRequest jsObjRequest = new CustomRequest(Method.POST, url, params, this.createRequestSuccessListener(), this.createRequestErrorListener());

requestQueue.add(jsObjRequest);

Volley JsonObjectRequest Post request not working

Community
  • 1
  • 1
Blue_Alien
  • 2,148
  • 2
  • 25
  • 29
  • 1
    Ah amazing!!!! time to refactor my code! can remove all my different methods with the different request types ie POST, GET, PUT, DELETE etc! amazing!! – Stillie Aug 11 '15 at 12:48
  • I think this has fixed my Caching issue that i also had?? nothing was Caching at all now it seems to be? am i right that it should be doing it? – Stillie Aug 11 '15 at 12:53
  • yeah.., the line HttpHeaderParser.parseCacheHeaders(response)); will manage it. :-) – Blue_Alien Aug 11 '15 at 13:16