I am trying to POST a JSON body using Volley in Android and after spending many hours with no success I am writing this question.
Following is mine code snippet
StringRequest residentSyncRequest = new StringRequest(Request.Method.POST, Commons.URL,this,this,ADD_REQ){
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/json");
return params;
}
#Override
public byte[] getBody() throws AuthFailureError {
JSONArray jsonArray = new JSONArray();
JSONObject jsonObject = new JSONObject();
SharedPreferences sharedPreferences = getActivity().getSharedPreferences(Commons.PREF_NAME, Context.MODE_PRIVATE);
try {
jsonObject.put("RowID","0");
jsonObject.put("LocalID","100");
jsonObject.put("LoginUserID",sharedPreferences.getString(Commons.pref_loginUserId,""));
jsonObject.put("AppToken",sharedPreferences.getString(Commons.pref_appToken,""));
} catch (JSONException e) {
e.printStackTrace();
}
jsonArray.put(jsonObject);
return jsonArray.toString().getBytes();
}
};
VollyRequest.getRequestQueue(getActivity()).add(residentSyncRequest);
and I am getting following response
E/Volley﹕ [255] BasicNetwork.performRequest: Unexpected response code 400 for ...
I tried to call the same web service using postman chrome extension and the service is working just fine.
Not sure if I need to do any encoding before returning byte[].
Please help.
Thanks in advance.
Volley doesn't support JsonArray request directly like JsonObject request.Hope they can add this function in next version because there are a lot of api only provide JsonArray.
EDIT:
Actually there is a solution here.
Volley by default puts all post parameters as JSON Values, so you just need to use a Request and not StringRequest and just add all JSON Values you want to post as regular post values.
Related
Peeps, I have some problem in understanding the working of volley library, so answer with proper material which can guide me to unobserved aspects of volley is what I am looking forward to.
How does my POST parameters are bind in network request. When I send params after overriding getParams() and by sending jsonObject directly in network request I don't receive any response but some server errors.
Since I am testing my backend on Postman, what postman actually does(my Observation) is it bind the params in url itself. When I copy paste the url in my android code it responds positively.
So, should I code to append my params to url or there's another way round?
I have alredy tried making changes to getHeaders() but it doesn't respond too!
You should use a JsonObjectRequest with a jsonObject containing all your params.
HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "8327483274823");
JSONObject jsonObject = new JSONObject(params);
JsonObjectRequest req = new JsonObjectRequest(URL,jsonObject ,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
//Do stuff here
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//Handle Errors here
}
});
I have teaching myself how to develop Android applications and I recently started using the Volley library for networking. I have been able to send basic requests with it. I was experimenting with Pocket's API to see if my app could fetch the items. Their documentation page mentions that I have to send a JSON request like so
POST /v3/oauth/request HTTP/1.1
Host: getpocket.com
Content-Type: application/json; charset=UTF-8
X-Accept: application/json
{"consumer_key":"1234-abcd1234abcd1234abcd1234",
"redirect_uri":"pocketapp1234:authorizationFinished"}
So I made a JSONObject in my app, added the key consumer_key with the value of my consumer key, then added in the key redirect_uri with its respective value. I sent this JSONObject as a request to the required URL as a POST request using Volley. The response code I received was 403, which is caused by a bad consumer key. I have double-checked the consumer key, so the problem lies with my request. How should I go about making the JSON request? Do I have to add the extra data, like Content-Type? If so, how?
If it is not too much, could you point me to a beginner-friendly resource on JSON, since I do not have much knowledge about web development?
Thank you.
What request methode do you use on your code? I am facing issue when using StringRequest method. Its work when use JsonObjectRequest method.
Here is how to create request besides on my experience.
Create Header Parameter
final Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("X-Accept", "application/json");
headers.put("consumer_key", "your-consumer-key");
headers.put("redirect_uri", "https://kamus.nusagates.com");
Create JSONObject From headers
JSONObject obj = new JSONObject(headers);
Create JsonObjectRequest
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest("https://getpocket.com/v3/oauth/request", obj, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//get all response data
Log.d("respon", String.valueOf(response));
try {
//get code from response
Log.d("respon code", response.getString("code"));
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return headers;
}
#Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
String server = String.valueOf(response.headers);
Log.d("header", server);
return super.parseNetworkResponse(response);
}
};
Add the request to The Queue
RequestQueue queue = Volley.newRequestQueue(MainActivity.this);
queue.add(jsonObjectRequest);
May this could help you solve your problem.
Cheers
I am doing an update on the old project & I don't have much knowledge of Android as of now. In project we have Comments section on product.
For comment after sending earlier we had return as 0 (some error) & 1 (success).
Below is the code we were using.
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Method.POST,
act.getString(R.string.CommentForUserURL),
null, new Response.Listener<JSONObject>() {
#Override
public void onResponse(
JSONObject response) {
Log.d("response done", "done===" + response);
mloading.setVisibility(View.GONE);
if (response != null) {
Comment obj = new Comment();
JSONObject jsonObject = response;
try {
obj.setComment(jsonObject
.getString("Comment"));
Now we have changed the return object from 0/1 to user object.
Does this need need to update JsonObjectRequest to GJSON request? Or object will also get parsed with JsonObjectRequest?
I am asking because when I execute above, I get error as below.
01-25 12:30:21.754: E/Volley(16487): [10114] BasicNetwork.performRequest:
Unexpected response code 500 for
http://new.souqalharim.com/add/CommentForMerchant
Any idea why I am getting this error?
Note: This URL is working fine for iPhone application.
Edit 1
This is post method, so full url is not there. There are few more parameters to add like ?comment=MyComment&userId=123&productId=234. As it is post I am not adding parameters in actual url.
I have those in another methods
#Override
protected Map<String, String> getParams()
throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("productId", productId.toString());
params.put("userId",
mSessionManager.getUserCode().toString());
params.put("comment", GlobalFunctions
.EncodeParameter(med_comments
.getText().toString()));
return params;
}
Full url is as below.
http://new.souqalharim.com/add/CommentForUser?productId=325&userId=5&comment=abcd
I tested this in Mozilla RESTClient and it works fine.
Edit 2
After checking further I found protected Map<String, String> getParams() throws AuthFailureError { is not getting called. Any idea why this is happening?
The problem is below.
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Method.POST,
act.getString(R.string.CommentForUserURL),
null, new Response.Listener<JSONObject>() {
^^^^
It should be
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(
Method.POST,
act.getString(R.string.CommentForUserURL),
new JSONObject(params), new Response.Listener<JSONObject>() {
^^^^^^^^^^^^^^^^^^^^^^
Copy code from protected Map<String, String> getParams() before final JsonObjectRequest.
That's it!!!
Reason is as below.
The JsonObjectRequest is extended JsonRequest which override getBody() method directly, so your getParam() would never invoke, I recommend you extend StringRequest instead of JsonObjectRequest.
your can check this answer for more details.
Actually 500 means Internal server error and this is caused by the Rest-api you are calling and not due to Volley,so check the back-end code.
BasicNetwork.performRequest: Unexpected response code 500
The server receives the correct response, but detects an error, this error is due to the PHP connection file with the database, this code must have the function implemented to hide the values of the query to avoid sql insertion
use this sample code:
<?php
$con = new mysqli('localhost', 'u648230499_wololo', '*********', 'u648230899_ejexample');
if ($con->connect_error) {
echo 'error connect database: ', $con->connect_error;
exit();
}
$participantId = $_POST['participantId'];
$name = $_POST['name'];
$sql = $con->prepare('INSERT INTO participant VALUES (?,?)');
$sql->bind_param('is', $participantId, $name);
$sql->execute();
echo 'OK\n';
$con->close();
?>
I'm trying to send a JSON as string (not as object) to the server (in this case it's a WebAPI). I always get a error code 500.
I succeeded to get response from the server when the request was GET and without sending data to the server. this achieved by JsonObjectRequest.
Now, I trying to send a POST request with JSON as string. For that I try
JsonObjectRequest
StringRequest
GsonRequest
JsonRequest - here I supplied my json in the requestBody
Before using volley, I used other methods to send request to server which require to simply build an object, serialized to json (string) and pass via StringEntity.
I can't understand where should I pass the json in the request. or what I'm doing wrong.
I don't exactly understand why do you want to send the JSON as a string and not as an object. Nevertheless, in your WebAPI endpoint you should put a breakpoint in the Post method of the ApiController and see whether the request gets there or not.
Probably, you're mixing the content-types of the request. If you want to send a simple string request from Volley, you should just use the StringRequest and send there the JSON text. Thus, in the WebAPI POST method you must get the string without being deserialized to JSON. I answered once a similar question of how this string request should be made here.
However, as I said before I would suggest using always JSON requests which includes the contentType:"application/json" header, and receive requests in the WebAPI deserialized.
url = "yoururl"; StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
// response
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
// error
Log.d("Error.Response", response);
}
} ) {
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("your_field", youJSONObject.toString());
return params;
} }; queue.add(postRequest);
Try in this way to make the post (it should work with json obj or array)
I am working on volley library: http://developer.android.com/training/volley/index.html
Get and 'Post methods without parameters' working fine. But when parameters are given, volley does not execute the form, and acts like form itself is a jsonObject:
com.android.volley.ParseError: org.json.JSONException: Value Login< of type java.lang.String cannot be converted to JSONObject
I have tried both overriding getParams() method:
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("username", username);
params.put("password", password);
return params;
}
And instantiating the object with parameter:
Map<String, String> params2 = new HashMap<String, String>();
params2.put("username", username);
params2.put("password", password);
JsonObjectRequest jsonObjectRequest1 = new JsonObjectRequest(Request.Method.POST, LOGIN_URL, new JSONObject(params2), new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
//onResponse
}
});
None of them worked. I am guessing my problem is about the content types. Volley library uses application/json while my php codes use name-value pairs.
I have seen these two questions but sadly they did not solve my case:
Google Volley ignores POST-Parameter
Volley Post JsonObjectRequest ignoring parameters while using getHeader and getParams
When you use JsonObjectRequest, you are saying that the content you are posting is a JSON Object and the response you expect back will also be a JSONObject. If neither of these are true, you need to build your own Request<T> and set the values you need.
The error you are seeing is because the response from the server is not a valid JSON response.