I am using Volley library in order to integrate my SettingsActivity with the server on Android Studio. I'm performing a request of GET method in order to retrieve the values from the server. But as i try to do the same, i get a volley error stating 401 Error.
I tested my url on POSTMAN application too. It showed the same 401 authorization error.
Question 1 : What are the reasons of getting such an error in Volley library for Android platform?
Question 2 : How can it be resolved so that the Volley requests can be performed on Android platform.
Code for Reference :
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setupActionBar();
sharedPreferences = getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE);
final String userid=sharedPreferences.getString(getString(R.string.saved_user_id),"");
Toast.makeText(this, userid, Toast.LENGTH_LONG).show();
//final String userid=NetworkHelper.get().getUserIdFromSharedPreferences();
//sharedPreferences=getSharedPreferences(getString(R.string.preference_file_key),MODE_PRIVATE);
//SharedPreferences.Editor editor=sharedPreferences.edit();
//editor.clear().apply();
//String userid=sharedPreferences.getString(getString(R.string.saved_user_id),"");
requestQueue= Volley.newRequestQueue(this);
String urlset=getString(R.string.url_server_base)+getString(R.string.url_users)+userid;
JsonObjectRequest jsonObjectRequest=new JsonObjectRequest(Request.Method.GET, urlset, null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject jsonObject) {
try {
//JSONArray jsonArray=jsonObject.getJSONArray("settings");
// for (int i=0; i<jsonArray.length();i++)
//{
JSONObject settings=jsonObject.getJSONObject("settings");
//retrieving values from the server
int eventnotifmins=settings.getInt("eventNotificationMins");
int alldaynotiftype=settings.getInt("allDayNotificationType");
int alldaynotiftimehr=settings.getInt("allDayNotificationTimeHr");
int alldaynotiftimemin=settings.getInt("allDayNotificationTimeMin");
// Feeding the values of preferences from server in Shared preferences
sharedPreferences=PreferenceManager.getDefaultSharedPreferences(SettingsActivity.this);
SharedPreferences.Editor editor1=sharedPreferences.edit();
editor1.putString("list_preference_1",Integer.toString(eventnotifmins));
editor1.putString("list_preference_2",Integer.toString(alldaynotiftype));
editor1.putString("list_preference_3",Integer.toString(alldaynotiftimehr));
editor1.apply();
//sBindPreferenceSummaryToValueListener
// this.bindPreferenceSummaryToValue();
//}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError volleyError) {
volleyError.printStackTrace();
Toast.makeText(SettingsActivity.this, "Check your internet connection", Toast.LENGTH_SHORT).show();
}
});
requestQueue.add(jsonObjectRequest);
}
Thanks in advance.