用SpringBoot设计的后台要求Authorization带Token访问,用xUtils怎么访问呢:
public void clickCardList(View view) {
RequestParams params =new RequestParams("http://192.168.1.89:8085/cart/list");
params.setConnectTimeout(30000);
params.setMaxRetryCount(3);
SharedPreferences sp = getSharedPreferences("config" , 0);
String token = sp.getString("token", "");
params.addHeader("Authorization", "Bearer " + token);
params.setAsJsonContent(false);
x.http().get(params, new Callback.CommonCallback() {
@Override
public void onSuccess(String result) {
System.out.println("返回结果 == " + result);
JSONObject jsonObject =null;
try {
jsonObject =new JSONObject(result);
}catch (JSONException e) {
e.printStackTrace();
}
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
if (ex.getMessage().toString().equals("Network is unreachable")) {
}
System.out.println("onError" + ex.toString());
}
@Override
public void onCancelled(CancelledException cex) {
System.out.println("onCancelled");
}
@Override
public void onFinished() {
System.out.println("onFinished");
}
});
}
网友评论