401 http error only in real android device, 200 success code in genymotion

738 views Asked by At

My problem is, when i am logging in to my webservice, i am getting 401 error code. But i am getting this error code only in real android devices(i have tried this in 2 different phones and networks). But in genymotion i am not getting any error, instead i am getting 200 success code. And my application is working perfectly.

I also would like to inform you that, my code is working fine in other connection parts of my application for both real and virtual devices; such as requesting data etc. which requires authentication

Somewhere in util class

static public AsyncHttpClient setHeader(Context contex,AsyncHttpClient client){

String username =contex.getResources().getString(R.string.url_login_username);
String password = contex.getResources().getString(R.string.url_login_password);

//since its required for this webservice
client.addHeader("Authorization","Basic "+ Base64.encodeToString(
        (username + ":" + password).getBytes(), Base64.NO_WRAP
));

return client;
}

In the login fragment

client = new AsyncHttpClient();
client  = Utils.setHeader(getActivity(), client);

In the callback of client , i have overwritten onFailure function as

@Override
public void onFailure(int arg0, Header[] arg1, byte[] arg2, Throwable arg3)     {
   String string = new String(arg2);
   Log.d("LoginFragment onFailure", "" + arg0 +" "+string);
   showSnackbar(R.string.loginAccessProblem, Color.RED);
}

And i am getting this erorr in logcat

onFailureļ¹• 401 {"status":false,"error":"Not authorized"}

I have also checked this tried code below

client.setBasicAuth(getString(R.string.url_login_username),getString(R.string.url_login_password));

edit : I can say my virtual device and webservice are in the same network, real devices has to connect from outer network

1

There are 1 answers

0
Alp On BEST ANSWER

I have solved this. I am getting my connection urls from xml file. Devices have different languages, genymotion is in english, real devices are in local language. So my application was getting its urls from values-tr xml in another library which my app project has dependency on. So avoid this problem in the future , i have gathered all my url connections in same language xml file(general one) and removed in language xmls.