如何解决如何在Android Studio中使用Retrofit 2.0在标头中发送身份验证令牌
我想将代理ID和身份验证令牌发送到API并获取一些相关数据,但是它显示了我未经授权的错误。我还测试了邮递员的API及其正常工作。
Api is working fine in Postman
return response in the android studio
Web服务代码:
@GET("customerform/showcustomer/{id}")
Call<GetAllApplicantFormDetailWrapper> GetAllApplicantFormDetail(
@Header("Authorization") String access_token,@Path("id") String agent_id);
在Activity Api调用代码中:
private void GetAllApplicanntFormList() {
progressBar.setVisibility(View.VISIBLE);
Log.e("TAG_onCheck",agent_id);
Log.e("TAG_onCheck",sessionManager.GetAgentToken());
WebServiceFactory.getInstance().GetAllApplicantFormDetail(sessionManager.GetAgentToken().trim(),agent_id)
.enqueue(new Callback<GetAllApplicantFormDetailWrapper>() {
@Override
public void onResponse(Call<GetAllApplicantFormDetailWrapper> call,Response<GetAllApplicantFormDetailWrapper> response) {
if (response.body() != null) {
// JSONObject object = new JSONObject(new Gson().toJson(response.body()));
if (response.body().getStatus().equals("1")) {
// JSONObject responseobj = object.getJSONObject("response");
allapplicantlist.addAll(response.body().getData());
// allapplicantlist.notify();
applicantFormsAdapter.notifyDataSetChanged();
progressBar.setVisibility(View.GONE);
Toast.makeText(getContext(),response.body().getMessage(),Toast.LENGTH_SHORT).show();
} else {
progressBar.setVisibility(View.GONE);
Toast.makeText(getContext(),Toast.LENGTH_SHORT).show();
// Snackbar.make(getView(),response.body().getError(),Snackbar.LENGTH_LONG).show();
}
} else {
progressBar.setVisibility(View.GONE);
}
if(applicantFormsAdapter.getItemCount()==0){
tv_no_record_found.setVisibility(View.VISIBLE);
}
}
@Override
public void onFailure(Call<GetAllApplicantFormDetailWrapper> call,Throwable t) {
Toast.makeText(getContext(),"Something went wrong!",Toast.LENGTH_SHORT).show();
Log.e("TAG_onFailure",t.toString());
progressBar.setVisibility(View.GONE);
}
});
}
解决方法
尝试更换
WebServiceFactory.getInstance().GetAllApplicantFormDetail(sessionManager.GetAgentToken().trim(),agent_id)
.enqueue(new Callback<GetAllApplicantFormDetailWrapper>()
使用
WebServiceFactory.getInstance().GetAllApplicantFormDetail("Bearer "+sessionManager.GetAgentToken().trim(),agent_id)
.enqueue(new Callback<GetAllApplicantFormDetailWrapper>()
这样,您可以在改造中传递Api Bearer授权令牌。让我知道这是否适合您,我刚刚完成了"Bearer " +key
,而不仅仅是key
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。