I'm trying to make the following request in Volley:
The request is being called twice, and I get a 401 error, which is the server authentication error, like if the token was invalid (I already checked and the token is valid):
I've tried:
- Making a JsonRequest, with either JsonObjectRequest and JsonArrayRequest, both give the same error.
- Tried with accept and content type, and also without those headers, getting same error.
- And the last try is with the following code, a StringRequest, with Content-Type header:
public void getUserFriends(final API.OnAPIResponse listener) {
FriendsRequest request = FriendsRequest.newInstance(listener);
//added this to see if it stopped being called twice
request.setRetryPolicy(new DefaultRetryPolicy(0, DefaultRetryPolicy.DEFAULT_MAX_RETRIES, DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
VolleySingleton.getInstance().addToRequestQueue(request);
}
private static class FriendsRequest extends StringRequest {
public static FriendsRequest newInstance(final API.OnAPIResponse listener) {
String serviceURL = API.API_URL + FRIENDS_PATH;
return new FriendsRequest(Request.Method.GET, serviceURL, new Response.Listener < String > () {
@Override
public void onResponse(String response) {
APIResponse apiResponse = null;
if (listener != null) {
listener.onResponse(apiResponse);
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
APIResponse apiResponse = getErrorResponse(error);
if (listener != null) {
listener.onResponse(apiResponse);
}
}
});
}
public FriendsRequest(int method, String url, Response.Listener < String > listener, Response.ErrorListener errorListener) {
super(method, url, listener, errorListener);
}
public FriendsRequest(String url, Response.Listener < String > listener, Response.ErrorListener errorListener) {
super(url, listener, errorListener);
}
@Override
public Map < String, String > getHeaders() throws AuthFailureError {
HashMap < String, String > headers = new API().getTokenHeader();
headers.put("Content-Type", "application/json; charset=utf-8");
Log.d("Network", headers.toString());
return headers;
}
}
I did manage to make a request to that API, that doesn't require the token header.
UPDATE: I'm still trying; using EasyVolley, I'm getting the same error:
EasyVolley.withGlobalQueue()
.load(API_URL + FRIENDS_PATH)
.addHeader("token", TokenGenerator.getToken())
.addHeader("Content-Type", "application/json; charset=utf-8")
.asJsonObject()
.setSubscriber(mainActivity)
.setCallback(new ASFRequestListener<JsonObject>() {
@Override
public void onSuccess(JsonObject response) {
Log.d("response", response.toString());
}
@Override
public void onFailure(Exception e) {
e.printStackTrace();
}
})
.start();
Getting same error, but at least it's called once:
UPDATE: tested with OkHttp, don't know what else to try, I've checked multiple times, the token works in the RESTClient.
OkHttpClient client = new OkHttpClient();
okhttp3.Request request = new
okhttp3.Request.Builder()
.header("token", TokenGenerator.getToken())
.url(API_URL + FRIENDS_PATH)
.build();
// Get a handler that can be used to post to the main thread
client.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
e.printStackTrace();
}
@Override
public void onResponse(Call call, final okhttp3.Response response) throws IOException {
response.headers();
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
}
});