anyone know how can I send a JSON param to Volley ? Seeing the library, I watch the methods getParams that return a Map , but I need to send a Json with the form
{"medias" : [1,2,3,4,5] }
but if I send it with the getParams method, my server receive some like this : "medias" : "[1,2,3,4,5]" (the second part as a string)
any help?
A JsonObjectRequest in Volley allows you to pass your JSONObject into the constructor. Just create a JSONObject, add your JSONArray and use it to create your JsonObjectRequest.
Here's an example of how to create a JSONArray:
https://stackoverflow.com/a/13468303/736496
Related
I am using Retrofit for server calls. I need to send a JSONArray to the server. My JSONArray looks like -
[{"callName”:”xxx”},{“inputData":{"deviceImei”:”xxxx”,”appVersionUser”:”x”,”osVersion”:”x”,”osType”:”x”,”deviceToken”:”xxxx”}}]
I am using RetroFit version 1.9.0 . I tried to use #BODY. But I am getting this error -
retrofit.RetrofitError: APIClass.GetClientAuthentication: #Body parameters cannot be used with form or multi-part encoding"
My Api function declaration is,
#FormUrlEncoded
#POST("/XYZ")
void GetClientAuthentication(#Body JSONArray jArray,
Callback<AuthenticationCallBack> aPOJOCallback);
Can anybody help?
Thanks in advance.
Hey i solved it finally
At the receiving part i took response in JSONElement and converted it to POJO class like below. AuthenticationErrorJsonResponse.java is my POJO class.
ArrayList<AuthenticationErrorJsonResponse> yourArray = new Gson().fromJson(authenticationJsonResponse.toString(),
new TypeToken<List<AuthenticationErrorJsonResponse>>(){}.getType());
I am a little confused as to which I am supposed to use.
The API is an trying to hit accepts regular text post parameters although its response is a JSON String.
Which should I use ?
You Can use JSON Object Request for this.
JsonObjectRequest and StringRequest is different in their parent class and their response. You could find it out if you dig into volley's source code.
JsonObjectRequest extends JsonRequest<JSONObject>
StringRequest extends Request<String>
So, if the response is a JSON String, then you could just use JsonObjectRequest for convenience, since Volley has wrapped the response to be a JSONObject.
StringRequest - when you want to send api parameter (getParams()) and fetch json response, you can use StringRequest.
JsonObjectRequest - When you only want to fetch the json response and not pass an api parameter, you can use JsonObjectRequest.
JsonObjectRequest can override the same set of methods (getHeaders(), getBodyContentType(), getBody(), getMethod()) like the StringRequest except getParams().
I am using retrofit an get Bad Request , I would want to know if there is a place in this library where builds the full JSON in string format before sending it.
If it's about inspecting the JSON at runtime for debugging purposes, you can call setLogLevel(LogLevel.FULL) on your RestAdapter.Builder.
FULL logs the headers, body and metadata for both requests and responses to logcat.
new String(((TypedByteArray) request.getBody()).getBytes());
In order to build a JSON formatted body, create an object with a class whose properties are the same that you want to send to the server. The GSON Library set up (or whichever library you are using) with the RestAdapter should send the request with the body in JSON format.
Also ensure that the call is #POST annotated and the parameter annotd with #Body Below is an example:
#POST("/login")
User login(#Body LoginUser loginUser);
I'm putting the volley requests to an array and then i' using for statement for adding request to volley queue like this (pseudo code) :
ArrayList<Requst> array = new ArrayList<Requst>()
volleyRequest req = //some code
volleyRequest req2 = //some code
array.add(req)
array.add(req2)
And on another class i'm using :
for(Requst r : array )
{
volley.newRequestQueue.add(req, tag);
}
But this loop did not continue until first request does not finished!
What should i do?
Is there any way for adding array of requests to volley without loop?
Use the singleton pattern to add multiple request in volley library as specified in the link
Check that out, you need to create a RequestQueue:
https://developer.android.com/training/volley/requestqueue.html
I'm trying to POST a JSONObject using the Retrofit library, but when I see the request at the receiving end, the content-length is 0.
In the RestService interface:
#Headers({
"Content-type: application/json"
})
#POST("/api/v1/user/controller")
void registerController(
#Body JSONObject registrationBundle,
#Header("x-company-device-token") String companyDeviceToken,
#Header("x-company-device-guid") String companyDeviceGuid,
Callback<JSONObject> cb);
And it gets called with,
mRestService.registerController(
registrationBundle,
mApplication.mSession.getCredentials().getDeviceToken(),
mApplication.mSession.getCredentials().getDeviceGuid(),
new Callback<JSONObject>() {
// ...
}
)
And I'm certain that the registrationBundle, which is a JSONObject isn't null or empty (the other fields are certainly fine). At the moment the request is made, it logs out as: {"zip":19312,"useAccountZip":false,"controllerName":"mine","registrationCode":"GLD94Q"}.
On the receiving end of the request, I see that the request has Content-type: application/json but has Content-length: 0.
Is there any reason why sending JSON in the body like this isn't working? Am I missing something simple in using Retrofit?
By default, you don't need to set any headers if you want a JSON request body. Whenever you test Retrofit code, I recommend setting .setLogLevel(RestAdapter.LogLevel.FULL) on your instance of RestAdapter. This will show you the full request headers and body as well as the full response headers and body.
What's occurring is that you are setting the Content-type twice. Then you're passing a JSONObject, which is being passed through the GsonConverter and mangled to look like {"nameValuePairs":YOURJSONSTRING} where YOURJSONSTRING contains your complete, intended JSON output. For obvious reasons, this won't work well with most REST APIs.
You should skip messing with the Content-type header which is already being set to JSON with UTF-8 by default. Also, don't pass a JSONObject to GSON. Pass a Java object for GSON to convert.
Try this if you're using callbacks:
#POST("/api/v1/user/controller")
void registerController(
#Body MyBundleObject registrationBundle,
#Header("x-company-device-token") String companyDeviceToken,
#Header("x-company-device-guid") String companyDeviceGuid,
Callback<ResponseObject> cb);
I haven't tested this exact syntax.
Synchronous example:
#POST("/api/v1/user/controller")
ResponseObject registerController(
#Body MyBundleObject registrationBundle,
#Header("x-company-device-token") String companyDeviceToken,
#Header("x-company-device-guid") String companyDeviceGuid);