Volley, Not Passing Any Parameters In Jsonarrayrequest
Solution 1:
Day's before I had Faced the same problem. it's not the problem with your code. Actually JsonArrayRequest class extends JsonRequest which extends Request. In JsonRequest class getBody() method has been overrided and it looks so
@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;
}
}
If you look closely getParams()
method is not called.
So what I did was to create a new CLASS
which extends Request<JsonArrayRequest>
like this
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<JSONArray> {
privateListener<JSONArray> 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<JSONArray> 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<JSONArray> 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(JSONArray response) {
// TODO Auto-generated method stub
listener.onResponse(response);
}
}
You can override the getParams() method in this class an use this for Your Request like this.
CustomJsonRequest request = newCustomJsonRequest(Request.Method.POST, url, params,
newResponse.Listener<JSONObject>() {
@OverridepublicvoidonResponse(JSONArray response) {
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
}
});
I hope this is helpful. ThankYou
Solution 2:
I had this problem last week and it had something to do with Volley not setting POST parameters in a correct way. Instead of fixing it i just went with a GET request and it looks like you could do the same since the parameters don't seem to be so very sensitive.
If you want to try a GET request you could do like this. Of course you have to switch from POST to GET in PHP as well!
The URL is created like this:
URLurl=null;
Stringquery="?from=" + textfrom_btn +
"&to=" + textto_btn +
"&seat=" + totalpassenger;
try {
url = newURL("http", "www.Foo.com", "Bar.php" + query);
}catch (MalformedURLException ex){
ex.printStackTrace();
}
Request looks like this:
JsonArrayRequest taxiReq = newJsonArrayRequest(Request.Method.GET, url.toString(), null, newResponse.Listener<JSONArray>(){
@OverridepublicvoidonResponse(JSONArray response){
for(int i = 0; i < response.length(); i++){
try{
JSONObject obj = response.getJSONObject(i);
Taxi taxi = newTaxi();
taxi.settaxiname(obj.getString("taxiname"));
taxi.setThumbnailUrl(obj.getString("image"));
taxi.setdeparture(obj.getString("departure"));
taxi.setarrive(obj.getString("arrive"));
taxi.setseat(obj.getInt("seat"));
taxi.setcost(obj.getInt("cost"));
// adding taxi to taxi array
taxiList.add(taxi);
}catch(JSONException e){
e.printStackTrace();
}
}
adapter.notifyDataSetChanged();
}
}, newResponse.ErrorListener(){
@OverridepublicvoidonErrorResponse(VolleyError error){
error.printStackTrace();
}
});
MyApplication.getInstance().addToRequestQueue(taxiReq);
}
Post a Comment for "Volley, Not Passing Any Parameters In Jsonarrayrequest"