R Boughan R Boughan - 1 month ago 24
Android Question

How to send String Array as param to srver and Getting error while uploading data on server --onErrorResponse (VolleyError error)

i am sending some data to server in order to save it in server database. But i keep on getting error. I used the same method in some other cases also and its working fine. But i don't know why in this case its not working.please tell me the correct way to upload data and also how to send data as String Array to server. Because as u can see i keep 'category' and 'subcategory' as null because the data of these two are in array(multiple choices selected by the user)... Please Help :)
Here is My uploadData method:

private void uploadData(){
StringRequest stringRequest = new StringRequest(Request.Method.POST,UPLOAD_URL,new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d("sub response", response);
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
String er=error.getMessage();
Log.d("error is",er);
Toast.makeText(UpdateUserProfile.this,er,Toast.LENGTH_LONG).show();
//Log.d("","error message"+er);
}
}){

@Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("user_id",user_id);
params.put("first_name",f_name.getText().toString());
params.put("last_name",l_name.getText().toString());
params.put("mobile_number",contact.getText().toString());
params.put("zip_code",z_code.getText().toString());
params.put("state",state_id_only);
params.put("region",region_id_only);
params.put("address",user_address.getText().toString());
params.put("category",null);
params.put("sub_category",null);
params.put("advertise_image",image);

return params;
}

};


RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
} //end =================purpose==========================


and each time i click on the button to upload the data i get this exception:

java.lang.NullPointerException: Attempt to invoke virtual method 'int java.lang.String.length()' on a null object reference
at libcore.net.UriCodec.encode(UriCodec.java:132)
at java.net.URLEncoder.encode(URLEncoder.java:57)
at com.android.volley.Request.encodeParameters(Request.java:450)
at com.android.volley.Request.getBody(Request.java:436)
at com.android.volley.toolbox.HurlStack.addBodyIfExists(HurlStack.java:260)
at com.android.volley.toolbox.HurlStack.setConnectionParametersForRequest(HurlStack.java:234)
at com.android.volley.toolbox.HurlStack.performRequest(HurlStack.java:107)
at com.android.volley.toolbox.BasicNetwork.performRequest(BasicNetwork.java:96)
at com.android.volley.NetworkDispatcher.run(NetworkDispatcher.java:112)

Answer

In the getParams()

params.put("category",null);
params.put("sub_category",null);

Don't pass null for values, instead of that pass ""

params.put("category","");
params.put("sub_category","");

may be thats why its getting error.

About sending an array of data: You can try with JSONArray to send array of data.