String Request BasicNetwork.performRequest: Unexpected response code 401 - android

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.

Related

Volley POST Request: 'com.android.volley.ClientError'

I am making a Video Player App and want to use 'open subtitles api' to download the subtitles from within the app same like the 'VLC media player' does. When triggering the download API, Volley is giving this error when giving a POST request with three headers and one parameters. The API is working properly and it is getting triggered when I run my app. I have tried and tested on postman and it is working fine and giving this response however I am not getting this response in android app instead "com.android.volley.ClientError" and I am stuck here.
This is my code:
public void sendDownloadRequest() {
StringRequest downloadRequest = new StringRequest(Request.Method.POST, downloadurl, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(context, "Response: " + response, Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
Toast.makeText(context, "Error from Download: " + error.toString(), Toast.LENGTH_LONG).show();
}
}) {
#Nullable
#Override
protected Map<String, String> getParams() {
Map<String, String> bodyParams = new HashMap<>();
bodyParams.put("file_name", "batman");
return bodyParams;
}
#Nullable
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> headerParams = new HashMap<>();
headerParams.put("Authorization", "My Auth Key");
headerParams.put("Api-Key", "My Api Key");
headerParams.put("Content-Type", "application/x-www-form-urlencoded");
return headerParams;
}
};
requestQueue.add(downloadRequest);
This is the request body of postman
I thought that it could be the error of the parameters but I am already giving the right params
Any help would be appreciated.
Try overriding the getBodyContentType() as well,
...
public String getBodyContentType() {
return "application/x-www-form-urlencoded; charset=UTF-8";
}
protected Map<String, String> getParams() {
...
and remove the Content-Type header.

BasicNetwork.performRequest: Unexpected response code 500 in android using volley

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.

Getting 400 error while trying to get access token through url in android

I am working with oauth2 and using Volley networking library in android. I am trying to get access token by using below code. But I getting 400 Bad request as my response. What is wrong in my request ? I even tried putting content type as application/x-www-form-urlencoded but still no luck. What can be the main cause for error ?
StringRequest accessTokenRequest = new StringRequest(Request.Method.POST, oauthConfig.getTokenEndPointUrl(), new Response.Listener<String>() {
#Override
public void onResponse(String response) {
// Log.e("AccessTokenFromLogin", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
}) {
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String,String> params = new HashMap<>();
params.put(OAuthConstants.GRANT_TYPE, "password");
params.put(OAuthConstants.USERNAME, userEmail);
params.put(OAuthConstants.PASSWORD, userPassword);
return params;
}
#Override
public Map<String, String> getHeaders() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/x-www-form-urlencoded");
if (isValid(clientId))
params.put(OAuthConstants.CLIENT_ID, clientId);
if (isValid(clientSecret))
params.put(OAuthConstants.CLIENT_SECRET, clientSecret);
if (isValid(scope))
params.put(OAuthConstants.SCOPE, scope);
Log.e("LoginActivity",clientId+" "+clientSecret+" "+scope);
return params;
}
};
Volley.newRequestQueue(this, hurlStack).add(accessTokenRequest);

Mailchimp error 400 request, can't send users to list

I'm working with Mailchimp and I want to send an email address to my list, I have done this so far with Volley:
public void suscribeMailChamp(){
String listid = "listID";
String url = "https://us16.api.mailchimp.com/3.0/lists/" + listid + "/members/";
// Instantiate the RequestQueue.
RequestQueue queue = Volley.newRequestQueue(this);
StringRequest sr = new StringRequest(Request.Method.POST,url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
//
Toast.makeText(MainActivity.this , "success" , Toast.LENGTH_SHORT).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this , error.toString() , Toast.LENGTH_SHORT).show();
}
}){
#Override
protected Map<String,String> getParams(){
Map<String,String> params = new HashMap<String, String>();
params.put("email_address","testmailchimp#gmail.com");
params.put("status","unsubscribed");
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");
params.put("Authorization" , "apikey <here my api key>");
return params;
}
};
queue.add(sr);
}
but I get error 400:
Unexpected response code 400 for
https://us16.api.mailchimp.com/3.0/lists/b9a5943047/members/
Here is a link for troubleshooting but I can't get the error:
http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/
Did you check the error in the link? It says
{
"type":"http://developer.mailchimp.com/documentation/mailchimp/guides/error-glossary/",
"title":"API Key Missing",
"status":401,
"detail":"Your request did not include an API key.",
"instance":"924c81cc-90e9-498d-b0fd-c7b54cba207f"
}
which means you are not (or correctly) sending the API key for mail chimp in the request. Just add the mailChimp API key in the params for Volley request
So the way you would send your API key is this
params.put("Authorization", "Basic "+Base64.encodeToString(("apikey:"+apiKey).getBytes("UTF-8"),Base64.DEFAULT))
I got stuck at this too. Mailchimp documentation is very poor.
I found the solution from github
Just send 'Authorization': 'apikey myapikey' in your header

Volley Post method gives me unexpected response code 400?

Here am new for android development am trying to post json value to server using gson lib i have converted into json but when i try to send to server is throws 400 unexpected response code here let me post my code:
listobj = account_sf_db.toServer();
Gson gson = new Gson();
RequestQueue queue = Volley.newRequestQueue(getBaseContext());
final String yog = gson.toJson(listobj);
String URL = "http://xxx.xx.xx.xxx/xxx/CRM/AcoountCreatePageLoad.svc/xxt/xxxt/ " +yog ;
StringRequest stringRequest = new StringRequest(Request.Method.POST, URL,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
String resp = response.toString();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
})
{
#Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("god", yog);
return params;
} #Override
public Map<String, String> getHeaders() throws AuthFailureError {
HashMap<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "application/json");
headers.put("Accept","application/json");
headers.put("User-agent", "My useragent");
return headers;
}
};
queue.add(stringRequest);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
}
400 status code is for Bad Request, it means server is not expecting this type of request.
The request could not be understood by the server due to malformed syntax. The client SHOULD NOT repeat the request without modifications.
Please check headers(content-type, user agent,etc.) you are adding whether it is required or not. It might happen that server is expecting a simple request and not json request.
Also try to read out error you get in onErrorResponse. Server might have responded you the expected parameter and request type, and also discuss the type of API developed by the web developer.
You have a space in your url after the /xxt/xxxt/
String URL = "http://xxx.xx.xx.xxx/xxx/CRM/AcoountCreatePageLoad.svc/xxt/xxxt/ " +yog ;
Remove it and I think you'll be fine.
P.S 400 generally means there is something wrong with the Url or parameters.

Categories

Resources