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)
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 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"
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.
Handle service call using volley library. Its working fine for jsonObject, jsonArray and String request but my application have one service different request and response. In this service have post params are Jsonobject, and return response is String.
I am trying many solution to handle response.but no one working. I am new android application developer.
post param and response:
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, params, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
return getHeader();
}
};
jsonObjectRequest.setRetryPolicy(new DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48, 2, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleyRequest.volleyRequestInstance(mContext).addToRequestQueue(jsonObjectRequest);
Volley Parse error will occur in different scenarios
If you call post method as to get, and also get as post
You have tried to pass different payloads
Return values are different in types that you are expected
Use a custom request to solve this issue in normal cases example here:
https://developer.android.com/training/volley/request-custom
in your code.
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST,url, params, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> myHeader=new HashMap<>();
myHeader.put("Accept","text");
return myHeader;
}
};
jsonObjectRequest.setRetryPolicy(new
DefaultRetryPolicy(DefaultRetryPolicy.DEFAULT_TIMEOUT_MS * 48,
2,DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleyRequest.volleyRequestInstance(mContext).addToRequestQueue
(jsonObjectRequest);
also needs your 'params' value for further clarification
Check on the response the URL returns the problem might be not the code but the URL response.
In my case Volley failed to parse my response because it did not understand the response from the URL.
hope this answer will help someone good luck :)
I'm using Volley as my http client library.
i need to send payload raw data as part of the request with Volley?
there are posts like: How to send Request payload to REST API in java?
but how this can be achieved using Volley?
need to use StringRequest as djodjo mentioned.
also getBody method need to be override - taken from here Android Volley POST string in body
#Override
public byte[] getBody() throws AuthFailureError {
String httpPostBody="your body as string";
// 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();
}
example:
final TextView mTextView = (TextView) findViewById(R.id.text);
...
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
String url ="http://www.google.com";
// Request a string response from the provided URL.
StringRequest stringRequest = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Display the first 500 characters of the response string.
mTextView.setText("Response is: "+ response.substring(0,500));
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
mTextView.setText("That didn't work!");
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
check the source and more info here
**UPDATE: ** If you need to add params you can simply override getParams()
Example:
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put("param1", "val1");
params.put("randomFieldFilledWithAwkwardCharacters","{{%stuffToBe Escaped/");
return params;
}
You don't need to override getBody yourself neither encode special chars as Volley is doing this for you.