Reputation: 1332
I am trying to implement tokenization in my Android app, and for that I always need to add the authorization header to my request anytime I make one to the server. The problem is that when I debug the server instance, even if the request is made, there is no Authorization header added in it that
Here is my code
private void syncDatabases()
{
String tag_json_obj = "json_obj_req";
String uri = "random_url";
final ProgressDialog pDialog = new ProgressDialog(this);
pDialog.setMessage("Please wait while syncing");
pDialog.show();
List<Plane> allPlane = service.gettAllPlane();
Gson gson = new Gson();
JSONArray jsArray = new JSONArray();
for (Plane p : allPlane)
{
String jsonString = gson.toJson(p);
try {
JSONObject obj = new JSONObject(jsonString);
obj.remove("ID");
jsArray.put(obj);
} catch (JSONException e) {
e.printStackTrace();
}
}
JsonArrayRequest jsonObjReq = new JsonArrayRequest(Request.Method.POST, uri, jsArray, new Response.Listener<JSONArray>()
{
@Override
public void onResponse(JSONArray response)
{
ArrayList<Plane> newList = new ArrayList<>();
try
{
for(int i=0;i<response.length();i++)
{
String planeName,planeEngine,planeProducer,planeCountry,planeYear,wikiLink,ID;
JSONObject pl = response.getJSONObject(i);
String p1 = pl.optString("ID");
if (pl != null && !p1.isEmpty())
ID = pl.getString("ID");
else
ID = "0";
String p2 = pl.optString("planeName");
if (p2 != null && !p2.isEmpty())
planeName = pl.getString("planeName");
else
planeName = "";
String p3 = pl.optString("planeEngine");
if (p3 != null && !p3.isEmpty())
planeEngine = pl.getString("planeEngine");
else
planeEngine = "";
String p4 = pl.optString("planeProducer");
if (p4 != null && !p4.isEmpty())
planeProducer = pl.getString("planeProducer");
else
planeProducer = "";
String p5 = pl.optString("planeCountry");
if (p5 != null && !p5.isEmpty())
planeCountry = pl.getString("planeCountry");
else
planeCountry = "";
String p6 = pl.optString("planeYear");
if (p6 != null && !p6.isEmpty())
planeYear = pl.getString("planeYear");
else
planeYear = "0";
String p7 = pl.optString("wikiLink");
if (p7 != null && !p7.isEmpty())
wikiLink = pl.getString("wikiLink");
else
wikiLink = "";
Plane plf = new Plane(Integer.parseInt(ID),planeName,planeEngine,planeProducer,planeCountry,Integer.parseInt(planeYear),wikiLink);
newList.add(plf);
}
}
catch (JSONException e)
{
e.printStackTrace();
}
service.deleteAllFromPlane();
for (Plane p : newList)
{
boolean ok = service.addNewPlane(p);
}
pDialog.dismiss();
tab1.onIorU();
}
}, new Response.ErrorListener()
{
@Override
public void onErrorResponse(VolleyError error)
{
Log.e("ERROR", "Error occurred ", error);
pDialog.dismiss();
}
})
{
@Override
public Map<String, String> getHeaders() throws AuthFailureError
{
SharedPreferences settings = PreferenceManager
.getDefaultSharedPreferences(MainActivity.this);
String auth_token_string = settings.getString("token", "");
Map<String, String> params = new HashMap<>();
params.put("Content-Type", "application/json; charset=UTF-8");
params.put("Authorization", auth_token_string);
return params;
}
};
I already verified if auth_token_string is null (it isn't). Here is what I see on the server side (C# Web API 2 server)
And here is how I get the header:
var authorization = request.Headers.Authorization;
In the pic you can see a part of the token I try to send in the headers field though. What am I doing wrong? Thank you.
Upvotes: 0
Views: 217
Reputation: 935
In your authorization token you are missing Type of authorization. I am asuming that you are using Bacis auth, so try using this:
params.put("Authorization", "Basic " + auth_token_string);
Upvotes: 1