Rax
Rax

Reputation: 1567

not getting success response when adding custom headers with Authorization header in get header() volley

I have referred this Example ,I am getting invalid token in response thus my authentication header works perfectly but something wrong in custom headers.

Custom volley request

 public CustomRequest(String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(Method.GET, url, errorListener);
    this.listener = reponseListener;
    this.params = params;


}

public CustomRequest(int method, String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener) {
    super(method, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
}

public CustomRequest(int method, String url, Map<String, String> params,
                     Listener<JSONObject> reponseListener, ErrorListener errorListener, Boolean head, String tok, String id) {
    super(method, url, errorListener);
    this.listener = reponseListener;
    this.params = params;
    headadd = head;
    this.id = id;
    this.token = tok;

}

protected Map<String, String> getParams()
        throws com.android.volley.AuthFailureError {

    return params;
}

adding custom and Authorization header

@Override
public Map<String, String> getHeaders() throws AuthFailureError {
    Map<String, String> head = new HashMap<>();


    head.put(
            "Authorization",
            String.format("Basic %s", Base64.encodeToString(
                    String.format("%s:%s", "xxxxxxx", "xxxxxxx").getBytes(), Base64.DEFAULT)));
    if (headadd) {
        head.put("token", token);
        head.put("client_id", id);
    }
    return head;
}

@Override
protected Response<JSONObject> parseNetworkResponse(NetworkResponse response) {
    try {
        String jsonString = new String(response.data,
                HttpHeaderParser.parseCharset(response.headers));
        return Response.success(new JSONObject(jsonString),
                HttpHeaderParser.parseCacheHeaders(response));
    } catch (UnsupportedEncodingException e) {
        return Response.error(new ParseError(e));
    } catch (JSONException je) {
        return Response.error(new ParseError(je));
    }
}

@Override
protected void deliverResponse(JSONObject response) {
    listener.onResponse(response);
}

@Override
public RetryPolicy getRetryPolicy() {
    RetryPolicy retryPolicy = new DefaultRetryPolicy(
            0,
            DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
            DefaultRetryPolicy.DEFAULT_BACKOFF_MULT
    );
    return retryPolicy;

}

}

Upvotes: 2

Views: 457

Answers (2)

Rax
Rax

Reputation: 1567

getHeaders() method was taking spaces and symbols (quotes) while making requests i correct them and works fine for me.

Upvotes: 0

a person
a person

Reputation: 986

You are using the correct method. You definitely need to override getHeaders(). My guess is that your problem lies somewhere in here:

String.format("Basic %s", Base64.encodeToString(
String.format("%s:%s", "uictester", "?f!T!ziX}.,(").getBytes(), Base64.DEFAULT)));

Maybe you need to convert this to UTF-8? Otherwise, you could pass in the headers map via constructor and see if that helps. It's probably a bit more flexible than hard-coding like that too. It actually looks like you're passing in the params in CustomRequest and not using them?

Upvotes: 2

Related Questions