Android Api (Okhttps) 在 android 9(pie) 及更高版本中未被调用

Pri*_*hal 1 api android asynchronous android-asynctask okhttp

我以与其他版本的手机相同的方式调用 android API,并且在 Oreo 版本即 8 之前工作正常。但在 android 9 即派版本及更高版本中不会调用该 API。如果馅饼有任何变化,请告诉我。提前致谢。

 private void getLoginAPI(String username, String password, String compnaycoce) {

    if (NetworkStatus.isNetworkConnected(this)) {
        LoginReqBean bean = new LoginReqBean();
        bean.UserId = username;
        bean.Password = password;
        bean.Company = compnaycoce;
        NetworkService serviceCall = new NetworkService(Constants.loginPost(), Constants.TAG_POST, this);
        serviceCall.call(bean);
    } else
        Toast.makeText(this, "Please check Internet connection", Toast.LENGTH_SHORT).show();
}

@Override
public void onNetworkCallInitiated(String service) {
    progressDialog = ProgressDialog.show(LoginActivity.this, "Info", "Validating Credentials, Please wait...");
    progressDialog.show();

}

 @Override
  public void onNetworkCallCompleted(String service, String response) {
    Log.e("LOGIN JSON ", "login " + response);

    if (progressDialog != null && progressDialog.isShowing())
        progressDialog.dismiss();

    LoginParentBean parentBean = LoginParentBean.fromJson(response);

    if (parentBean != null && parentBean.status) {
        LoginBean loginBean = parentBean.result;
        Toast.makeText(getApplicationContext(), "You Are logged in Successfully!", Toast.LENGTH_LONG).show();
        AppPreferences.INSTANCE.setUserID(loginBean.user_id);
        AppPreferences.INSTANCE.setUserRole(loginBean.userRole);
        AppPreferences.INSTANCE.setUserLocation(loginBean.location);
        AppPreferences.INSTANCE.setUserLocationID(loginBean.locationId);
        AppPreferences.INSTANCE.setIsPostGres(loginBean.isPostgres);
        AppPreferences.INSTANCE.setUserName(loginBean.username);
        AppPreferences.INSTANCE.setAccessToken(loginBean.tokenValue);
        AppPreferences.INSTANCE.setLogin(true);

        Intent intent = new Intent(getApplicationContext(), DashBoardActivity.class);
        startActivity(intent);
        finish();
    } else
        Toast.makeText(getApplicationContext(), "Please check your username and password again!", Toast.LENGTH_LONG).show();


}

@Override
public void onNetworkCallError(String service, String errorMessage) {
    if (progressDialog != null && progressDialog.isShowing())
        progressDialog.dismiss();

    MessageDialog msg = new MessageDialog(LoginActivity.this);
    msg.setButtonText("Ok");
    msg.show(getResources().getString(R.string.error_somethingwent), getResources().getString(R.string.app_name));
}
}
Run Code Online (Sandbox Code Playgroud)

Bas*_*asi 5

Android 6.0在android 清单中的 application 元素下引入了useCleartextTraffic属性。Android P中的默认值为\xe2\x80\x9cfalse\xe2\x80\x9d。将此设置为 true 表示应用程序打算使用清晰的网络流量。

\n\n
<application\n    android:usesCleartextTraffic="true"\n\n</application>\n
Run Code Online (Sandbox Code Playgroud)\n\n

然而,这似乎解决了问题,但却对数据完整性造成了威胁。Android 7.0通过网络安全配置文件提供了更好的解决方案

\n

  • @DPrince 检查[此](https://developer.android.com/training/articles/security-config#CleartextTrafficPermited)官方Android文档。“从 Android 9(API 级别 28)开始,默认情况下禁用明文支持” (2认同)