I'm having some trouble getting JSON response from Microsoft custom vision API (Optical Character Recognition API) when using Android Volley request.
I have used this approach with other API's without any problems, but for this API I cant get it to work.
String URL = "https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Getting License plate...");
pDialog.setCancelable(false);
pDialog.show();
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
JSONObject jsonBody = new JSONObject();
jsonBody.put("url", "https://upload.wikimedia.org/wikipedia/commons/thumb/a/af/Atomist_quote_from_Democritus.png/338px-Atomist_quote_from_Democritus.png");
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
pDialog.hide();
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VOLLEY", error.toString());
}
}) {
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", requestBody, "utf-8");
return null;
}
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Content-Type", "application/json");
headers.put("Ocp-Apim-Subscription-Key", "123124123123123123213");
return headers;
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
I'm getting this response back:
E/Volley: [2163] BasicNetwork.performRequest: Unexpected response code 400 for https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr
When using postman I'm not getting any errors.
So hope you can see what I'm doing wrong.
Let me know if you want me to elaborate on anything.
Thanks!
You have to replace StringRequest with JsonObjectRequest and get rid of the Content-type header.
This works for me:
final JSONObject jsonBody = new JSONObject();
jsonBody.put("url", "https://pbs.twimg.com/profile_images/808958766628605952/yB14UlXl_400x400.jpg");
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.POST, URL, jsonBody,
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
System.out.println(response.toString());
}
},
new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
String readableError = "";
try {
readableError = new String(error.networkResponse.data, "utf-8");
System.out.println(readableError);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<>();
headers.put("Ocp-Apim-Subscription-Key", "XXXXXXXX");
return headers;
}
};
note we had problems on older devices weirdly.
Was caused by the uploaded image being too big.
Api just returns 400 with no explanation.
ImageHelper class has code to resize image
Related
I am trying to learn Volley library for posting data into webservices. I need to implement user registration form, following is the image of postman with parameters and header...
now problem is, i am getting below error
com.android.volley.ServerError
this is my code for volley post method.
public void postNewComment(){
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://myurl/api/users";
JSONObject jsonBody = new JSONObject();
jsonBody.put("email", "test1#gmail.com");
jsonBody.put("user_type", "C");
jsonBody.put("company_id", "0");
jsonBody.put("status", "A");
jsonBody.put("password", "123456");
final String requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("VOLLEY", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Log.e("VOLLEY", error.toString());
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic " + "My_auth_key");
headers.put("Content-Type", "application/json");
return headers;
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
// can get more details such as response.headers
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
}
please suggest where am i getting wrong. URL is working correct with postman, also as you can see i need to set 2 headers. I also tried this Url post method with AsyncTask and its working good. Now i need to implement this using volley library. kindly suggest. thank you.
this is my logcat error:
E/Volley: [81910] BasicNetwork.performRequest: Unexpected response code 405 for "Myurl"
**Try this one **
private void sendWorkPostRequest() {
try {
String URL = "";
JSONObject jsonBody = new JSONObject();
jsonBody.put("email", "abc#abc.com");
jsonBody.put("password", "");
jsonBody.put("user_type", "");
jsonBody.put("company_id", "");
jsonBody.put("status", "");
JsonObjectRequest jsonOblect = new JsonObjectRequest(Request.Method.POST, URL, jsonBody, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
Toast.makeText(getApplicationContext(), "Response: " + response.toString(), Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
onBackPressed();
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
final Map<String, String> headers = new HashMap<>();
headers.put("Authorization", "Basic " + "c2FnYXJAa2FydHBheS5jb206cnMwM2UxQUp5RnQzNkQ5NDBxbjNmUDgzNVE3STAyNzI=");//put your token here
return headers;
}
};
VolleyApplication.getInstance().addToRequestQueue(jsonOblect);
} catch (JSONException e) {
e.printStackTrace();
}
// Toast.makeText(getApplicationContext(), "done", Toast.LENGTH_LONG).show();
}
}
I have an alternative answer that works pretty well for Android Volley+ library by dworks and Google: See HERE
How to pass these parameter into POST method using Volley library.
API link: http://api.wego.com/flights/api/k/2/searches?api_key=12345&ts_code=123
Screenshot of JSON structure
I tried this but again facing error.
StringEntity params= new StringEntity ("{\"trip\":\"[\"{\"departure_code\":\","
+departure,"arrival_code\":\"+"+arrival+","+"outbound_date\":\","
+outbound,"inbound_date\":\","+inbound+"}\"]\"}");
request.addHeader("content-type", "application/json");
request.addHeader("Accept","application/json");
Please visit here for the details of API.
Usual way is to use a HashMap with Key-value pair as request parameters with Volley
Similar to the below example, you need to customize for your specific requirement.
Option 1:
final String URL = "URL";
// Post params to be sent to the server
HashMap<String, String> params = new HashMap<String, String>();
params.put("token", "token_value");
params.put("login_id", "login_id_value");
params.put("UN", "username");
params.put("PW", "password");
JsonObjectRequest request_json = new JsonObjectRequest(URL, new JSONObject(params),
new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
//Process os success response
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
}
});
// add the request object to the queue to be executed
ApplicationController.getInstance().addToRequestQueue(request_json);
NOTE: A HashMap can have custom objects as value
Option 2:
Directly using JSON in request body
try {
RequestQueue requestQueue = Volley.newRequestQueue(this);
String URL = "http://...";
JSONObject jsonBody = new JSONObject();
jsonBody.put("firstkey", "firstvalue");
jsonBody.put("secondkey", "secondobject");
final String mRequestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i("LOG_VOLLEY", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("LOG_VOLLEY", error.toString());
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return mRequestBody == null ? null : mRequestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
VolleyLog.wtf("Unsupported Encoding while trying to get the bytes of %s using %s", mRequestBody, "utf-8");
return null;
}
}
#Override
protected Response<String> parseNetworkResponse(NetworkResponse response) {
String responseString = "";
if (response != null) {
responseString = String.valueOf(response.statusCode);
}
return Response.success(responseString, HttpHeaderParser.parseCacheHeaders(response));
}
};
requestQueue.add(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
this is an example uses StringRequest
StringRequest stringRequest = new StringRequest(Method.POST, url, listener, errorListener) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("api_key", "12345");
map.put("ts_code", "12345");
return map;
}
};
OkHttpClient okHttpClient = new OkHttpClient();
ContentValues values = new ContentValues();
values.put(parameter1Name, parameter1Value);
values.put(parameter2Name, parameter2Value);
RequestBody requestBody = null;
if (values != null && values.size() > 0) {
FormEncodingBuilder formEncoding = new FormEncodingBuilder();
Set<String> keySet = values.keySet();
for (String key : keySet) {
try {
values.getAsString(key);
formEncoding.add(key, values.getAsString(key));
} catch (Exception ex) {
Logger.log(Logger.LEVEL_ERROR, CLASS_NAME, "getRequestBodyFromParameters", "Error while adding Post parameter. Skipping this parameter." + ex.getLocalizedMessage());
}
}
requestBody = formEncoding.build();
}
String URL = "http://example.com";
Request.Builder builder = new Request.Builder();
builder.url(URL);
builder.post(requestBody);
Request request = builder.build();
Response response = okHttpClient.newCall(request).execute();
I work with KSOAP2 but i wish usign Volley and i can't connect a web service with Authentication.
I have tried overwriting the method getHeader but in the log shows the following.
E/Volley: [593] BasicNetwork.performRequest: Unexpected response code 500 for http://myservice/register.asmx
this is my code:
public void peticion() {
final RequestQueue queue = Volley.newRequestQueue(this);
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("response", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e("VolleyError", error.getMessage());
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headers = new HashMap<String, String>();
headers.put("UsuarioName", "user123");
headers.put("UsuarioPass", "123456789");
return headers;
}
};
queue.add(stringRequest);
}
EDIT
Fragment the my WebService
<?xml version="1.0" encoding="utf-8"?><soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/MLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Header> <Autenticacion xmlns="http://tempuri.org/"> <suarioPass>string</UsuarioPass> <UsuarioName>string</UsuarioName> </Autenticacion> </soap12:Header>
with ksoap work but with volley no.
-The data are correct
-The service work
I dont understand why not work with volley
Thank you for your help
I think you can refer to the following code (I have tested with ASP.Net Web API), then use its logic for your app:
StringRequest stringRequest = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.i(LOG_TAG, response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Log.e(LOG_TAG, error.toString());
}
}) {
#Override
public byte[] getBody() throws AuthFailureError {
String paramsEncoding = "UTF-8";
Map<String, String> params = new HashMap<>();
params.put("grant_type", "password");
params.put("username", "user");
params.put("password", "pass");
StringBuilder encodedParams = new StringBuilder();
try {
for (Map.Entry<String, String> entry : params.entrySet()) {
encodedParams.append(URLEncoder.encode(entry.getKey(), paramsEncoding));
encodedParams.append('=');
encodedParams.append(URLEncoder.encode(entry.getValue(), paramsEncoding));
encodedParams.append('&');
}
return encodedParams.toString().getBytes(paramsEncoding);
} catch (UnsupportedEncodingException uee) {
throw new RuntimeException("Encoding not supported: " + paramsEncoding, uee);
}
}
#Override
protected VolleyError parseNetworkError(VolleyError volleyError) {
String json;
if (volleyError.networkResponse != null && volleyError.networkResponse.data != null) {
try {
json = new String(volleyError.networkResponse.data,
HttpHeaderParser.parseCharset(volleyError.networkResponse.headers));
} catch (UnsupportedEncodingException e) {
return new VolleyError(e.getMessage());
}
return new VolleyError(json);
}
return volleyError;
}
};
queue.add(stringRequest);
Volley don't support Protocol SOAP
I have not used volley library much. i have read the tutorials. i want to send data to a url which which will enter that data in database. i have tried the following code but its not working. data is not entered in the database.
String url = "http://tipseducation.com/system/eadmin/insertschedule/";
StringRequest sr = new StringRequest(Request.Method.POST, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//Valid Response
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
//error message
}
}) {
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("appt_name", ed_name);
params.put("appt_email", ed_email);
params.put("appt_contact", ed_contact);
params.put("appt_date", ed_date);
params.put("appt_time", ed_time);
params.put("appt_service", ed_spinner);
return params;
}
};
can anyone please help me. iam new to this
Instead of getHeaders, you should use getBody for your POST request.
You can refer to my following working sample code (replace my JSONObject and Url by yours). Hope this helps!
...
try {
RequestQueue queue = Volley.newRequestQueue(this);
jsonBody = new JSONObject();
jsonBody.put("Title", "Android Volley POST DATA Demo");
jsonBody.put("Author", "BNK");
jsonBody.put("Date", "2015/09/17");
requestBody = jsonBody.toString();
StringRequest stringRequest = new StringRequest(1, url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// do something...
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// do something...
}
}) {
#Override
public String getBodyContentType() {
return "application/json; charset=utf-8";
}
#Override
public byte[] getBody() throws AuthFailureError {
try {
return requestBody == null ? null : requestBody.getBytes("utf-8");
} catch (UnsupportedEncodingException uee) {
e.printStackTrace();
return null;
}
}
};
queue.addToRequestQueue(stringRequest);
} catch (JSONException e) {
e.printStackTrace();
}
...
I am trying to connectt to Disqus with their api
(Specifically I am calling to POST https://disqus.com/api/oauth/2.0/access_token/ )
I am using JsonObjectRequest with Volley library for the network calls.
My response is consistently an error with code 400:
11-19 12:48:17.119: E/Volley(16124): [107902] BasicNetwork.performRequest: Unexpected response code 400 for http://disqus.com/api/oauth/2.0/access_token/
I have tried to proxy the request in Charles to see more info, and got this as the error message:
And this is my request as it was recorded by Charles (As you can see I did add the parameter grant_type):
I am calling the same request in the iOS version of my app, and am using the same keys and information, and everything works there. This is why I assume the problem is somewhere in my code, perhaps in the way I add/encode/not encode my parameters... This is the code I use to send the request:
String url = "http://disqus.com/api/oauth/2.0/access_token/";
JSONObject jsonObj = new JSONObject();
try {
jsonObj.put("grant_type", "authorization_code");
jsonObj.put("client_id", publicKey());
jsonObj.put("client_secret", SecretKey());
jsonObj.put("redirect_uri", redirectUrl());
jsonObj.put("code", code);
} catch (JSONException e1) {
e1.printStackTrace();
}
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.POST,
url, jsonObj, new Response.Listener<JSONObject>() {
#Override
public void onResponse(JSONObject response) {
try {
String refreshToekn = (String) response.get("refresh_token");
String accessToekn = (String) response.get("access_token");
String stam = refreshToekn+accessToekn;
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("Error: " + error.getMessage());
}
});
VolleyController.getInstance().addToRequestQueue(jsonObjReq);
The call always falls into onErrorResponse
I have also tried to use StringRequest and add the parameters in the url with no successes
So I managed to get it to work, but I would love if someone could explain why my solution works.
Basically, instead of using JSONObject and passing it to JsonObjectRequest I used a StringRequest and overrode getParams.
Was I wrong trying to send a POST request with JsonObjectRequest?
This is the entire solution:
String url = "http://disqus.com/api/oauth/2.0/access_token/";
StringRequest stringReq = new StringRequest(Method.POST,
url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
String string = response;
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d("error: " + error.getMessage());
}
}) {
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("grant_type", "authorization_code");
params.put("client_id", publicKey());
params.put("client_secret", SecretKey());
params.put("redirect_uri",redirectUrl());
params.put("code", code);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String,String> params = new HashMap<String, String>();
params.put("Content-Type","application/x-www-form-urlencoded");
return params;
}
};
VolleyController.getInstance().addToRequestQueue(stringReq);