401 http 错误仅在真实 android 设备中,genymotion 中为 200 成功代码
401 http error only in real android device, 200 success code in genymotion
我的问题是,当我登录 webservice
时,我收到 401
错误代码。但我只在真正的 android 设备中收到此错误代码(我已在 2 种不同的手机和网络中尝试过)。
但是在 genymotion 中我没有收到任何错误,而是收到 200
成功代码。我的应用程序运行良好。
我还想通知您,我的代码在我的应用程序的其他连接部分中对于真实设备和虚拟设备都运行良好;比如请求数据等需要认证的
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;
}
在login fragment
client = new AsyncHttpClient();
client = Utils.setHeader(getActivity(), client);
在客户端的回调中,我将onFailure
函数改写为
@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);
}
我在 logcat
中遇到了这个错误
onFailure﹕ 401 {"status":false,"error":"Not authorized"}
我也检查了 this 尝试了下面的代码
client.setBasicAuth(getString(R.string.url_login_username),getString(R.string.url_login_password));
编辑:我可以说我的虚拟设备和网络服务在同一个网络中,真实设备必须从外部网络连接
我已经解决了这个问题。我正在从 xml 文件获取连接 urls。设备有不同的语言,genymotion 是英文的,真实设备是本地语言。所以我的应用程序从我的应用程序项目依赖的另一个库中的 values-tr xml 获取它的 urls。
所以以后要避免这个问题,我已经用相同的语言 xml 文件(一般的)收集了我所有的 url 连接,并在语言 xmls.
中删除了
我的问题是,当我登录 webservice
时,我收到 401
错误代码。但我只在真正的 android 设备中收到此错误代码(我已在 2 种不同的手机和网络中尝试过)。
但是在 genymotion 中我没有收到任何错误,而是收到 200
成功代码。我的应用程序运行良好。
我还想通知您,我的代码在我的应用程序的其他连接部分中对于真实设备和虚拟设备都运行良好;比如请求数据等需要认证的
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;
}
在login fragment
client = new AsyncHttpClient();
client = Utils.setHeader(getActivity(), client);
在客户端的回调中,我将onFailure
函数改写为
@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);
}
我在 logcat
onFailure﹕ 401 {"status":false,"error":"Not authorized"}
我也检查了 this 尝试了下面的代码
client.setBasicAuth(getString(R.string.url_login_username),getString(R.string.url_login_password));
编辑:我可以说我的虚拟设备和网络服务在同一个网络中,真实设备必须从外部网络连接
我已经解决了这个问题。我正在从 xml 文件获取连接 urls。设备有不同的语言,genymotion 是英文的,真实设备是本地语言。所以我的应用程序从我的应用程序项目依赖的另一个库中的 values-tr xml 获取它的 urls。 所以以后要避免这个问题,我已经用相同的语言 xml 文件(一般的)收集了我所有的 url 连接,并在语言 xmls.
中删除了