OkHttp:<-HTTP失败:java.net.UnknownServiceException:网络安全策略不允许与10.0.2.2的CLEARTEXT通信 [英] OkHttp: <-- HTTP FAILED: java.net.UnknownServiceException: CLEARTEXT communication to 10.0.2.2 not permitted by network security policy
问题描述
我已成功在Android应用中实现了具有Firebase身份验证的 google登录.
I have implemented google sign-in with Firebase authentication in my Android app succesfully.
如您所见,我已经用我的帐户登录,该帐户显示在Firebase控制台上.
As you see, I have logged-in with my account and it appears on the Firebase console.
firebaseAuthWithGoogle 功能在用户使用Google登录名登录后使用Firebase对其进行身份验证:
The function firebaseAuthWithGoogle authenticates the user with Firebase after he logs-in with Google Sign-in:
private void firebaseAuthWithGoogle(GoogleSignInAccount acct) {
Log.d(TAG, "firebaseAuthWithGoogle:" + acct.getId());
AuthCredential credential = GoogleAuthProvider.getCredential(acct.getIdToken(), null);
mFirebaseAuth.signInWithCredential(credential)
.addOnCompleteListener(this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if (task.isSuccessful()) {
// Sign in success, update UI with the signed-in user's information
Log.d(TAG, "signInWithCredential:success");
final FirebaseUser user = mFirebaseAuth.getCurrentUser();
//This is to connect to the http server and save the user data in my MySql database
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(LoginActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String userToken = instanceIdResult.getToken();
String uid = user.getUid();
String name = user.getDisplayName();
String email = user.getEmail();
String profileUrl = user.getPhotoUrl().toString();
String coverUrl = "";
UserInterface userInterface = ApiClient.getApiClient().create(UserInterface.class);
Call<Integer> call = userInterface.signin(new LoginActivity.UserInfo(uid,name,email,profileUrl,coverUrl,userToken));
call.enqueue(new Callback<Integer>() {
@Override
public void onResponse(Call<Integer> call, Response<Integer> response) {
progressDialog.dismiss();
Toast.makeText(LoginActivity.this,"Login succesfull AFTER API CALL",Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this,MainActivity.class));
finish();
}
@Override
public void onFailure(Call<Integer> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(LoginActivity.this,"Login failed AFTER API CALL",Toast.LENGTH_SHORT).show();
}
});
}
});
} else {
// If sign in fails, display a message to the user.
Log.w(TAG, "signInWithCredential:failure", task.getException());
}
// ...
}
});
}
函数 firebaseAuthWithGoogle 的另一件事是:连接到http Apache服务器,并将用户信息保存在 MySQL用户表中> ,通过以下代码段:
Another thing the function firebaseAuthWithGoogle does is: Connect to the http Apache server and save the user information in a MySQL users table through this code snippet:
//This is to connect to the http server and save the user data in my MySql database
FirebaseInstanceId.getInstance().getInstanceId().addOnSuccessListener(LoginActivity.this, new OnSuccessListener<InstanceIdResult>() {
@Override
public void onSuccess(InstanceIdResult instanceIdResult) {
String userToken = instanceIdResult.getToken();
String uid = user.getUid();
String name = user.getDisplayName();
String email = user.getEmail();
String profileUrl = user.getPhotoUrl().toString();
String coverUrl = "";
UserInterface userInterface = ApiClient.getApiClient().create(UserInterface.class);
Call<Integer> call = userInterface.signin(new LoginActivity.UserInfo(uid,name,email,profileUrl,coverUrl,userToken));
call.enqueue(new Callback<Integer>() {
@Override
public void onResponse(Call<Integer> call, Response<Integer> response) {
progressDialog.dismiss();
Toast.makeText(LoginActivity.this,"Login succesfull AFTER API CALL",Toast.LENGTH_SHORT).show();
startActivity(new Intent(LoginActivity.this,MainActivity.class));
finish();
}
@Override
public void onFailure(Call<Integer> call, Throwable t) {
progressDialog.dismiss();
Toast.makeText(LoginActivity.this,"Login failed AFTER API CALL",Toast.LENGTH_SHORT).show();
}
});
}
});
}
因此,在用户成功通过身份验证之后,对服务器的调用将失败,并且显然将执行以下行:
So after the user has been succesfully authenticated, the call to the server fails and this line is obviously executed:
Toast.makeText(LoginActivity.this,"Login failed AFTER API CALL",Toast.LENGTH_SHORT).show();
我将 Retrofit 用作 http客户端,并将 okhttp 用作 http正文拦截器 ,以 记录http请求 ,如下所示:
I am using Retrofit as an http client along with okhttp as an http body interceptor to log the http requests as follows:
ApiClient.java
ApiClient.java
public static Retrofit getApiClient(){
HttpLoggingInterceptor httpLoggingInterceptor = new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient httpClient = new OkHttpClient.Builder()
.addInterceptor(httpLoggingInterceptor)
.build();
if(retrofit==null){
retrofit = new Retrofit.Builder().baseUrl(BASE_URL)
.client(httpClient)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
因此,在Toast Text "API API CALL之后登录失败" 出现在模拟器上之后,我在日志上显示了此信息:
So after the Toast Text "Login failed AFTER API CALL" appears on the emulator, I have this on the log:
D/OkHttp: --> POST http://10.0.2.2/friendster/public/app/login
Content-Type: application/json; charset=UTF-8
Content-Length: 413
D/OkHttp: {"CoverUrl":"","email":"MyEmail@gmail.com","name":"Ahmed Ghrib","profileUrl":"https://lh6.googleusercontent.com/-S8l_5gZaXJ8/AAAAAAI/AAAAAAAAAAA/ACHi3rfObo6-Ta-wxrMUvcAZ8Yg/s96-c/photo.jpg","uid":"YACACYYDcGVr26N8OHuTuQlQqvU2","userToken":"ecxdtFaKldI:APA91bHb1PAA5hU6i1oMqnSsDXXkAaXNb6dynyaYmhU_soHTWmLXud6REjCpqTjsGpgdBh1NMYUqAr3SaTUWapN4v73zkvyYD2f3yegUP3H38eeU_JtH7NOSMKbF4U"}
D/OkHttp: --> END POST (413-byte body)
W/e.myapplicatio: Verification of okhttp3.internal.http.ExchangeCodec okhttp3.internal.connection.RealConnection.newCodec$okhttp(okhttp3.OkHttpClient, okhttp3.Interceptor$Chain) took 134.353ms
D/OkHttp: <-- HTTP FAILED: java.net.UnknownServiceException: CLEARTEXT communication to 10.0.2.2 not permitted by network security policy
所以我发现这是导致错误的原因:
So I found-out that this is causing the error:
与网络安全不允许的CLEARTEXT与10.0.2.2的通信 政策
CLEARTEXT communication to 10.0.2.2 not permitted by network security policy
经过StackOverflow之后,我发现需要创建此文件:
After going through StackOverflow, I found-out that I needed to create this file:
src/main/res/xml/network_security_config.xml
src/main/res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<domain-config cleartextTrafficPermitted="true">
<domain includeSubdomains="true">api.example.com(to be adjusted)</domain>
</domain-config>
</network-security-config>
并将其添加到清单中:
<application
---
android:networkSecurityConfig="@xml/network_security_config">
已经用Postman测试了我的数据库,我确定问题出在我的Android项目中.
我认为这应该已经解决了问题.但是,我仍然有完全相同的问题.我仍然在日志中收到此错误:
Having tested my database with Postman, I am sure the problem lies in my Android project.
I thought that this should've solved the issue. But, I still have the exact same issue. I still get this error in the log:
与网络安全不允许的CLEARTEXT与10.0.2.2的通信 政策
CLEARTEXT communication to 10.0.2.2 not permitted by network security policy
推荐答案
使用它代替安全配置文件:
Use this instead for the security configuration file:
src/main/res/xml/network_security_config.xml
src/main/res/xml/network_security_config.xml
<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
<!--Set application-wide security config using base-config tag.-->
<base-config cleartextTrafficPermitted="true"/>
</network-security-config>
它解决了我的问题:)
这篇关于OkHttp:<-HTTP失败:java.net.UnknownServiceException:网络安全策略不允许与10.0.2.2的CLEARTEXT通信的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!