6

i must do i request with Volley Framework. This is a POST request with JSONObject.

I must pass one string and one JSONArray..but how i can?

I start with this:

 private String mUrl;
 private ArrayList<String> mUrlDove;
 HashMap<String, String> params = new HashMap<String, String>();
 params.put("url", mUrl);
 params.put("urlDove", mUrlDove); ---> Wrong because mUrlDove is not a String
 mUrl = app.getInstance().getmUrlRestWS() + getString(R.string.path);
 JsonObjectRequest mRequest = new JsonObjectRequest(
 mUrl, new JSONObject(params),
 createMyReqSuccessListener(),
 createMyReqErrorListener()) {
 @Override
 public Map<String, String> getHeaders() throws AuthFailureError {
 return app.getInstance().createBasicAuthHeader();
 }
 };

If i try with Browser i must set this:

{
 "url": "www.secret.com",
 "urlDove" : [ "www.google.com","www.yahoo.com"]
}
asked Apr 21, 2015 at 17:40

3 Answers 3

6

you need to make a JSON Array first and then store that

private String mUrl;
private ArrayList<String> mUrlDove;
HashMap<String, String> params = new HashMap<String, String>();
 params.put("url", mUrl);
 JSONArray jsArray = new JSONArray(mUrlDove);
 params.put("urlDove", jsArray.toString()); 
 mUrl = app.getInstance().getmUrlRestWS() + getString(R.string.path);
 JsonObjectRequest mRequest = new JsonObjectRequest(
 mUrl, new JSONObject(params),
 createMyReqSuccessListener(),
 createMyReqErrorListener()) {
 @Override
 public Map<String, String> getHeaders() throws AuthFailureError {
 return app.getInstance().createBasicAuthHeader();
 }
 };
answered Apr 21, 2015 at 18:08
Sign up to request clarification or add additional context in comments.

2 Comments

com.android.volley.ParseError: org.json.JSONException: Value Created of type java.lang.String cannot be converted to JSONObject
5

try passing JSONObject instead of hashmap

JSONObject params = new JSONObject();
params.put("url", "www.secret.com");
JSONArray urlDove = new JSONArray();
urlDove.put("www.google.com");
urlDove.put("www.yahoo.com");
params.put("urlDove", urlDove);
JsonObjectRequest mRequest = new JsonObjectRequest(
 mUrl, params,
 createMyReqSuccessListener(),
 createMyReqErrorListener()) {
 @Override
 public Map<String, String> getHeaders() throws AuthFailureError {
 return app.getInstance().createBasicAuthHeader();
 }
 };

for reference in parsing json https://stackoverflow.com/a/17810270/4810752

answered Apr 21, 2015 at 18:10

Comments

2

JSONObject can take in Java Objects, try using

Map<String,Object> 

something like this:

 String mUrl; //initialized somewhere else
 ArrayList<String> mUrlDove; //initialized somewhere else
 Map<String, Object> jsonParams = new HashMap<>();
 jsonParams.put("url", mUrl);
 jsonParams.put("urlDove", mUrlDove);
 JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, url, new JSONObject(jsonParams),
 new Response.Listener<JSONObject>()
 {
 @Override
 public void onResponse(JSONObject response)
 {
 Log.d("Volley Response: ", response.toString()); 
 //do the other stuff you need...
 }
 },
 new Response.ErrorListener()
 {
 @Override
 public void onErrorResponse(VolleyError error)
 {
 if (null != error.networkResponse)
 {
 Log.d(" Volley Error Code: ", "" + error.networkResponse.statusCode);
 //probably throw an Exception or some MessageEvent
 }
 }
 });
 requestQueue.add(request);

this works for me with complex objects like

 Map<String,<List<Map<String,Object>>>

with the most inner objects being Strings and Integers, and the List being initialized as a new ArrayList.

Hope this Helps!

answered Apr 22, 2015 at 8:36

2 Comments

@chris still relevant?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.