Android Volley POST send options are always null

I am new to android.Now, I am making one application. To do this, I need to send data to the server. Now I am using the postley post method. But the parameters always show null when I send data to the server using the volley. If I have attached the code, please check it. Here I use fragments.

Code Section

String url = "http://192.168.1.182:8084/name/registration.jsp"; final ProgressDialog pDialog = new ProgressDialog(this.getActivity()); pDialog.setMessage("Loading..."); pDialog.show(); RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext()); JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, url, null, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d(TAG, response.toString()); // pDialog.hide(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); //pDialog.hide(); } }) { @Override protected Map<String, String> getParams() { Map<String, String> params = new HashMap<String, String>(); params.put("name", "Ajay KK"); params.put("mailid", "ajaykk50@gmail.com"); params.put("phone", "8086327023"); params.put("place", "Calicut"); params.put("longitude","44444.3333"); params.put("latitude","666666.3333"); params.put("wheel", "1"); params.put("type", "owner"); return params; } }; // Adding request to request queue rq.add(jsonObjReq); 
+3
android post android-volley
source share
4 answers

Do not override getParams() . JsonObjectRequest uses the third argument in the constructor to retrieve the message parameters. Below is the documentation contained in the volleyball code

 /** * Creates a new request. * @param method the HTTP method to use * @param url URL to fetch the JSON from * @param jsonRequest A {@link JSONObject} to post with the request. Null is allowed and * indicates no parameters will be posted along with request. * @param listener Listener to receive the JSON response * @param errorListener Error listener, or null to ignore errors. */ public JsonObjectRequest(int method, String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener) { super(method, url, (jsonRequest == null) ? null : jsonRequest.toString(), listener, errorListener); } 

used like that.

 String url = "http://192.168.1.182:8084/name/registration.jsp"; final ProgressDialog pDialog = new ProgressDialog(this.getActivity()); pDialog.setMessage("Loading..."); pDialog.show(); RequestQueue rq = Volley.newRequestQueue(getActivity().getApplicationContext()); JSONObject params = new JSONObject(); try { params.put("name", "Ajay KK"); params.put("mailid", "ajaykk50@gmail.com"); params.put("phone", "8086327023"); params.put("place", "Calicut"); params.put("longitude","44444.3333"); params.put("latitude","666666.3333"); params.put("wheel", "1"); params.put("type", "owner"); } catch (JSONException e) { e.printStackTrace(); } JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST, url, params, //Not null. new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d(TAG, response.toString()); // pDialog.hide(); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d(TAG, "Error: " + error.getMessage()); //pDialog.hide(); } }); // Adding request to request queue rq.add(jsonObjReq); 
+7
source share
 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; 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); } } 

After that, call this class from your activity ... LIKE THIS

  RequestQueue requestQueue = VolleySingleton.getInstance().getRequestQueue(); CustomRequest jsObjRequest = new CustomRequest(Request.Method.POST, LOGIN_URL, params, new Response.Listener<JSONObject>() { @Override public void onResponse(JSONObject response) { Log.d("RESPONSE", response.toString()); } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { VolleyLog.d("RESPONSE ERROR", "Error: " + error.getMessage()); } }); requestQueue.add(jsObjRequest); 

You can create parameters as

  Map<String, String> params = new HashMap<String, String>(); params.put("email", "yourEmail"); params.put("password", "yourPassword"); 

Other classes you need are VolleySingleton and RetriveMyApplicationContext

  import android.graphics.Bitmap; import android.util.LruCache; import com.android.volley.RequestQueue; import com.android.volley.toolbox.ImageLoader; import com.android.volley.toolbox.Volley; public class VolleySingleton { private static VolleySingleton sInstance = null; private RequestQueue mRequestQueue; private ImageLoader imageLoader; private VolleySingleton() { mRequestQueue = Volley.newRequestQueue(RetriveMyApplicationContext.getAppContext()); imageLoader = new ImageLoader(mRequestQueue, new ImageLoader.ImageCache() { private LruCache<String, Bitmap> cache = new LruCache<>((int) (Runtime.getRuntime().maxMemory() / 1024 / 8)); @Override public Bitmap getBitmap(String url) { return cache.get(url); } @Override public void putBitmap(String url, Bitmap bitmap) { cache.put(url, bitmap); } }); } public static VolleySingleton getInstance() { if (sInstance == null) { sInstance = new VolleySingleton(); } return sInstance; } public RequestQueue getRequestQueue() { return mRequestQueue; } public ImageLoader getImageLoader() { return imageLoader; } } 

RetriveMyApplicationContext Class

 public class RetriveMyApplicationContext extends Application { //Don't forget to mention RetriveMyApplicationContext Class in Manifests File otherwise it will throw NullPointer Exception // <application // android:name=".volley.RetriveMyApplicationContext" private static RetriveMyApplicationContext mRetriveMyApplicationContext; @Override public void onCreate() { super.onCreate(); mRetriveMyApplicationContext = this; } public static RetriveMyApplicationContext getInstance() { return mRetriveMyApplicationContext; } public static Context getAppContext() { return mRetriveMyApplicationContext.getApplicationContext(); } } 
+1
source share
 public class CustomJsonRequest extends Request { Map<String, String> params; private Response.Listener listener; public CustomJsonRequest(int requestMethod, String url, Map<String, String> params, Response.Listener responseListener, Response.ErrorListener errorListener) { super(requestMethod, url, errorListener); this.params = params; this.listener = responseListener; } @Override protected void deliverResponse(Object response) { listener.onResponse(response); } @Override public Map<String, String> getParams() throws AuthFailureError { return params; } @Override protected Response 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)); } } } 
-one
source share

I also came across such an error that I wasted for half a day to solve this problem. I finally decided.

This is not a problem with Android code, check the parameters that you send to the server and check the columns in the database. If the columns are not found in the database, we get this error.

-2
source share

All Articles