I am making an android application. Basically i am making checkout object. in this object i am sending json object to server. this json object in encoded with utf8.
Encode Object String:
EncodedObject: %7B%22AddressID%22%3A107%2C%22CustomerID%22%3A2%2C%22PaymentTypeID%22%3A3%2C%22ListProducts%22%3A%5B%7B%22ProductID%22%3A23%2C%22Price%22%3A0%2C%22Quantity%22%3A0%2C%22ListAttributes%22%3A%5B%7B%22Name%22%3A%22%22%2C%22ProductID%22%3A23%2C%22AttributeTypeID%22%3A0%2C%22AttributeID%22%3A0%7D%5D%7D%2C%7B%22ProductID%22%3A1904%2C%22Price%22%3A16000%2C%22Quantity%22%3A1%2C%22ListAttributes%22%3A%5B%7B%22Name%22%3A%22%22%2C%22ProductID%22%3A1904%2C%22AttributeTypeID%22%3A0%2C%22AttributeID%22%3A0%7D%5D%7D%2C%7B%22ProductID%22%3A23%2C%22Price%22%3A20378%2C%22Quantity%22%3A1%2C%22ListAttributes%22%3A%5B%7B%22Name%22%3A%22%22%2C%22ProductID%22%3A23%2C%22AttributeTypeID%22%3A0%2C%22AttributeID%22%3A0%7D%5D%7D%5D%7D
here is my volley code:
StringRequest request = new StringRequest(Request.Method.POST, URLs.orderCheckout,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("Response", response);
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put("OrderObject",encodeObject);
Log.e("order",params.toString());
return params;
}
};
VolleySingleton.getInstance(context).addToRequestQueue(request);
}
and here is my encode Object which i am sending to server which is working fine
encodeObject = URLEncoder.encode(object.toString(), "UTF-8");
Encode Object Logcat which i am sending to server
and here is my error Volley Error Unexpected code 500 snapshot.
I don't know where i am doing mistake.
Related
Is okhttp is good for this or do you have any code similar to this ping me
Posting Data To server By Get Method
public void sendingDataToServer(){
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("onResponseSuccess", response.toString() + "check");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("onResponseSuccess", error.toString() + "check");
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json; ");
headers.put("x-oc-Image-url", "Your Image Path Url");
try {
headers.putAll(super.getHeaders());
} catch (AuthFailureError authFailureError) {
authFailureError.printStackTrace();
}
return super.getHeaders();
}
};
Volley.newRequestQueue(mContext).add(stringRequest);
}
If Your Sending Only Url to server there are ways to send Image Url to server
1. Passing through Header
2. Passing through JsonObject
I Suspect their is not posting Image Url path in your code.
In Server Side 500 error means request parameter not receiving to server time you will get this error.
I'm trying to use the speech recognition REST API service from wit.ai
I have used Volley to send a POST request to the URL
https://api.wit.ai/speech
This is what I have currently done:
void makeApiCall(){
StringRequest request = new StringRequest(Request.Method.POST, "https://api.wit.ai/speech", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("wit_response",response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("wit_response",error.toString());
}
}){
#Override
protected Map<String,String> getParams() throws AuthFailureError{
Map<String,String> params = new HashMap<>();
params.put("Authorization","Bearer XXXXXX"); //hidden my token
params.put("Content-Type","audio/mpeg3");
return params;
}
#Override
public byte[] getBody() throws AuthFailureError {
return sendToByte();
}
};
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
queue.add(request);
}
I am receiving an error of com.android.volley.ClientError on the wit_response log key inside onErrorResponse() method
I have not missed the content type and authorization header, and my sendToByte function is succesfully returning an mp3 file converted to byte array.
What is the issue?
I had to use this link https://gist.github.com/anggadarkprince/a7c536da091f4b26bb4abf2f92926594
And use MultiPartRequest class as described in this to upload my file.
Please comment here if you need any assistance (for all future folks)
I am trying to make a POST request to this URL :
"https://api.github.com/search/repositories?q=created:%3E2018-12-29&sort=stars&order=desc"
using the parameters : 'q' , 'sort' and 'order', but I get a message saying the 'q' code is missing.
Request parameters
Response message
It also doesn't work on my android volley using the POST method :
RequestQueue mQueue = Volley.newRequestQueue(this);
StringRequest request = new StringRequest(Request.Method.POST,"https://api.github.com/search/repositories", new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.d("ANANAS","ONRESPONSE");
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.d("ANANAS","ONERROR");
Log.d("ANANAS",error.getMessage());
}
}){
#Override
protected Map<String, String> getParams() {
Map<String,String> params = new HashMap<String,String>();
params.put("q","created:>2018-12-13");
params.put("sort","stars");
params.put("order","desc");
return params;
}
};
mQueue.add(request);
What am I missing ?
Your parameters are fine, but you should use Request.Method.GET since the API will be expecting a GET (see API docs).
StringRequest request = new StringRequest(Request.Method.GET,"https://api.github.com/search/repositories"
My partner made an API.
Then in the given link I'm trying to get the datas in JSON.
In postman it's working fine.
And in postman i just set the Authorization to No Auth and paste the link and its working.
In android its different.
I am receiving Error code 400.
But when i tried it in android studio it's not working.
String REVIEW_URL;
REVIEW_URL ="http://be0658e94af429e2150dd0a3dc1a199a:fdf2a81d6a88e86826b8f9530ea190bf#somesite.esy.es/api.php/reviews/12";
RequestQueue requestQueue = Volley.newRequestQueue(getApplicationContext());
StringRequest request = new StringRequest(Request.Method.GET, REVIEW_URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("RESPONSE:", response);
Toast.makeText(SignUp.this, response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> header = new HashMap<>();
return header;
}
};
requestQueue.add(request);
I am receiving this error.
01-09 01:19:24.828 9109-9210/com.toweelo E/Volley: [245] BasicNetwork.performRequest: Unexpected response code 401 for http://be0658e94af429e2150dd0a3dc1a199a:fdf2a81d6a88e86826b8f9530ea190bf#somesite.esy.es/api.php/reviews/12
Since you are using the GET method, check if REVIEW_URL value is really correct,
A 400 error means that the request was malformed. In other words, the
data stream sent by the client to the server didn't follow the rules.
I'm trying to use Volley library to communicate with my RESTful API.
I have to POST string in the body, when I'm asking for the bearer Token. String should look like this:
grant_type=password&username=Alice&password=password123
And header:
Content-Type: application/x-www-form-urlencoded
More info about WebApi Individual Accounts:
http://www.asp.net/web-api/overview/security/individual-accounts-in-web-api
Unfortunately I can't figure out how can I solve it..
I'm trying something like this:
StringRequest req = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
VolleyLog.v("Response:%n %s", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
}){
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
params.put("username", "User0");
params.put("password", "Password0");
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/x-www-form-urlencoded");
return headers;
}
};
I'm getting 400 Bad Request all the time.
I think that I'm actually sending request like this:
grant_type:password, username:User0, password:Password0
instead of:
grant_type=password&username=Alice&password=password123
I would be very grateful if anyone has any ideas or an advice..
To send a normal POST request (no JSON) with parameters like username and password, you'd usually override getParams() and pass a Map of parameters:
public void HttpPOSTRequestWithParameters() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com/login.asp";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR","error => "+error.toString());
}
}
) {
// this is the relevant method
#Override
protected Map<String, String> getParams()
{
Map<String, String> params = new HashMap<String, String>();
params.put("grant_type", "password");
// volley will escape this for you
params.put("randomFieldFilledWithAwkwardCharacters", "{{%stuffToBe Escaped/");
params.put("username", "Alice");
params.put("password", "password123");
return params;
}
};
queue.add(postRequest);
}
And to send an arbitary string as POST body data in a Volley StringRequest, you override getBody()
public void HttpPOSTRequestWithArbitaryStringBody() {
RequestQueue queue = Volley.newRequestQueue(this);
String url = "http://www.somewebsite.com/login.asp";
StringRequest postRequest = new StringRequest(Request.Method.POST, url,
new Response.Listener<String>()
{
#Override
public void onResponse(String response) {
Log.d("Response", response);
}
},
new Response.ErrorListener()
{
#Override
public void onErrorResponse(VolleyError error) {
Log.d("ERROR","error => "+error.toString());
}
}
) {
// this is the relevant method
#Override
public byte[] getBody() throws AuthFailureError {
String httpPostBody="grant_type=password&username=Alice&password=password123";
// usually you'd have a field with some values you'd want to escape, you need to do it yourself if overriding getBody. here's how you do it
try {
httpPostBody=httpPostBody+"&randomFieldFilledWithAwkwardCharacters="+URLEncoder.encode("{{%stuffToBe Escaped/","UTF-8");
} catch (UnsupportedEncodingException exception) {
Log.e("ERROR", "exception", exception);
// return null and don't pass any POST string if you encounter encoding error
return null;
}
return httpPostBody.getBytes();
}
};
queue.add(postRequest);
}
As an aside, Volley documentation is non-existent and quality of StackOverflow answers is pretty bad. Can't believe an answer with an example like this wasn't here already.
First thing, I advise you to see exactly what you're sending by either printing to the log or using a network sniffer like wireshark or fiddler.
How about trying to put the params in the body? If you still want a StringRequest you'll need to extend it and override the getBody() method (similarly to JsonObjectRequest)
I know this is old, but I ran into this same problem and there is a much cleaner solution imo found here: How to send a POST request using volley with string body?