How To Post Request Parameters When Using Jsonarrayrequest In Volley
I am newbie to Json parsing. I am trying to read a json data using JsonArrayRequest but I am little confused in sending parameters and use POST method.In case of JsonObjectRequest
Solution 1:
This solved my problem to pass params when JsonArrayRequest and POST method is used.
Volley.newRequestQueue(getActivity())
.add(newJsonRequest<JSONArray>(Request.Method.POST,
MySingleton.getInstance().getDOWNLOAD_SERVICES_URL(),
jsonobj.toString(),
newResponse.Listener<JSONArray>() {
@OverridepublicvoidonResponse(JSONArray jsonArray) {
Log.d("response", "res-rec is" + jsonArray);
if (jsonArray == null) {
pDialog.dismiss();
Snackbar.make(myview, "No services found", Snackbar.LENGTH_LONG).show();
} else {
for (int i = 0; i < jsonArray.length(); i++) {
try {
pDialog.dismiss();
JSONObject jsonObject = jsonArray.getJSONObject(i);
String desc = jsonObject.getString("SvcTypeDsc");
String image_url = jsonObject.getString("ThumbnailUrl");
// al_ImageUrls.add(image_url);
al_list_of_services.add(desc);
ad_servicesadapter = newArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, al_list_of_services);
lv_webservicesList.setAdapter(ad_servicesadapter);
Log.d("imageurls", "imagesurl " + image_url);
Log.d("services-list", "list is " + desc + " " + i);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError volleyError) {
VolleyLog.d("Login request", "Error: " + volleyError.getMessage());
Log.d("Volley Error:", "Volley Error:" + volleyError.getMessage());
Toast.makeText(getActivity(), "Unable to connect to server, try again later", Toast.LENGTH_LONG).show();
pDialog.dismiss();
}
})
{
@OverrideprotectedMap<String, String> getParams() throws AuthFailureError {
Map<String, String> params = newHashMap<String, String>();
// params.put("uniquesessiontokenid", "39676161-b890-4d10-8c96-7aa3d9724119");
params.put("uniquesessiontokenid", userDet.getSessionToken());
params.put("said", userDet.getSAID());
params.put("SOId", "23295");
returnsuper.getParams();
}
@OverrideprotectedResponse<JSONArray> parseNetworkResponse(NetworkResponse networkResponse) {
try {
String jsonString = newString(networkResponse.data,
HttpHeaderParser
.parseCharset(networkResponse.headers));
returnResponse.success(newJSONArray(jsonString),
HttpHeaderParser
.parseCacheHeaders(networkResponse));
} catch (UnsupportedEncodingException e) {
returnResponse.error(newParseError(e));
} catch (JSONException je) {
returnResponse.error(newParseError(je));
}
// return null;
}
}
);
Solution 2:
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;
publicclassCustomRequestextendsRequest<JSONObject> {
privateListener<JSONObject> listener;
privateMap<String, String> params;
publicCustomRequest(String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(Method.GET, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
publicCustomRequest(int method, String url, Map<String, String> params,
Listener<JSONObject> reponseListener, ErrorListener errorListener) {
super(method, url, errorListener);
this.listener = reponseListener;
this.params = params;
}
protectedMap<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);
}
}
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);
Answer get form here. https://stackoverflow.com/a/19945676/1641556
Refer these articles also.
Solution 3:
If you are using 'com.android.volley:volley:1.0.0'
you should override getParams()
method like this:
public synchronized voidaddJsonArrayRequest(int method, String url, final JSONObject jsonRequest, Response.Listener<JSONArray> responseListener, Response.ErrorListener errorListener) {
JsonArrayRequest request = newJsonArrayRequest(method, url, null, responseListener, errorListener) {
@OverrideprotectedMap<String, String> getParams() throws AuthFailureError {
HashMap<String, String> params = newHashMap<>();
try {
params.put("key1", jsonRequest.getString("key1"));
params.put("key2", Integer.toString(jsonRequest.getInt("key2")));
params.put("key3", Boolean.toString(jsonRequest.getBoolean("key3")));
params.put("key4", jsonRequest.getJSONArray("key4").toString());
} catch (JSONException e) {
e.printStackTrace();
}
return params;
}
};
addToRequestQueue(request);
}
If you are using 'com.mcxiaoke.volley:library:1.0.19' you can simply put JSONObject
:
publicsynchronizedvoidaddJsonArrayRequest(int method, String url, JSONObject jsonRequest, Response.Listener<JSONArray> responseListener, Response.ErrorListener errorListener) {
JsonArrayRequestrequest=newJsonArrayRequest(method, url, jsonRequest, responseListener, errorListener);
addToRequestQueue(request);
}
Solution 4:
here is an example to post the request to the server using volly
You can add the parameters to a HashMap and then pass that into the request you are creating;
EDITED:
HashMap<String, String> mRequestParams = newHashMap<String, String>();
mRequestParams.put("username","abcd");
mRequestParams.put("password", "123456");
publicvoidvollyStringRequestForPost() {
JsonArrayRequest req = newJsonArrayRequest(Request.Method.POST, strURL, newJSONObject(mRequestParams),
newResponse.Listener<JSONArray>() {
@OverridepublicvoidonResponse(JSONArray response) {
try {
//Here you will receive your response
} catch (JSONException e) {
e.printStackTrace();
}
}
}, newResponse.ErrorListener() {
@OverridepublicvoidonErrorResponse(VolleyError error) {
//Do what you want to do on error
}
});
mRequestQueue.add(req);
}
}
Solution 5:
Here you go..
final HashMap<String, String> params = newHashMap<String, String>();
params.put("email", userName);
params.put("password", password);
final JSONObject jsonObject = newJSONObject(params);
JsonArrayRequest req = newJsonArrayRequest(Request.Method.POST, url, jsonObject,
newResponse.Listener<JSONArray>() {
@OverridepublicvoidonResponse(JSONArray response) {
try {
//process response
}
catch(JSONEXception e){}
And on PHP side you can get these params like this
$json=file_get_contents('php://input');
$data = json_decode($json);
$email=$data->{'email'};
$pass=$data->{'password'};
Post a Comment for "How To Post Request Parameters When Using Jsonarrayrequest In Volley"