使用翻新版,我将我的联系人列表发布到手机上而未进行身份验证。我需要进行基本身份验证的改装,但是据我从互联网上看不到的信息。你能帮我吗?
这是我的代码:
public void GetContactsIntoArrayList(){
mAPIService = ApiUtils.getAPIService();
final User user = new User();
final Post post = new Post();
cursor = getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null, null, null);
while (cursor.moveToNext()) {
int i = 0;
name = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
phonenumber = cursor.getString(cursor.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
StoreContacts.add(name + " " + ":" + " " + phonenumber);
user.setphoneNumber(phonenumber.toString().trim());
user.setname(name.toString().trim());
List<User> phoneList = new ArrayList<>();
phoneList.add(user);
post.setUsers(phoneList);
sendPost(post);
}
cursor.close();
}
public void sendPost(Post post){
mAPIService.savePost(post).enqueue(new Callback<Post>() {
@Override
public void onResponse(Call<Post> call, Response<Post> response) {
Log.d("requestError", "onResponse: "+ call.request().body().toString());
if(response.isSuccessful()) {
Log.i("Is Ok?","OK :)");
}
}
@Override
public void onFailure(Call<Post> call, Throwable t) {
}
});
}
答案 0 :(得分:0)
这是迄今为止我尝试过的最简单的“基本身份验证”方法。
使用以下代码生成auth标头(API /存储库类)
var basic = Credentials.basic("YOUR_USERNAME", "YOUR_PASSWORD")
将此作为标头传递到Web服务调用(API /存储库类)
var retrofitCall = myWebservice.getNewsFeed(basic)
添加基本标头作为参数(Retrofit Web服务接口类)
@GET("newsfeed/daily")
fun getNewsFeed(@Header("Authorization") h1:String):Call<NewsFeedResponse>
我的代码在Kotlin中,以防万一您正在寻找Java。但是可以轻松转换为Java。
参考:https://mobikul.com/basic-authentication-retrofit-android/
答案 1 :(得分:0)
您可以在请求的标头中添加auth值,例如,如果您想发送auth令牌,请按照以下步骤操作:
@POST("/auth/update-contactno")
@Headers(
"Content-Type: application/json",
Constants.headerApp,
Constants.headerLanguage,
Constants.headerPlatform,
Constants.headerVersion
)
fun updateMobileNumber(@Header(Constants.authorization) token: String?, @Body verifyForgotPasswordOTPInput: VerifyForgotPasswordOTPInput): Call<JsonObjectResponse<UserModel>>
然后像这样呼叫updateMobileNumber()
:
apiService.updateMobileNumber(token, verifyForgotPasswordOTPInput)