1

I am calling the android API in the same way as for other versions of phone and its working fine till Oreo version i.e. 8. But the API is not getting called in android 9 i.e. pie version and above. If there are some changes in pie, do let me know. Thanks in advance.

 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));
}
}
  • are you getting error like `java.io.IOException: Cleartext HTTP traffic to * not permitted` ?? – Basi May 10 '19 at 04:13
  • https://stackoverflow.com/questions/53368470/api-call-in-android-9-configuration-devices-and-emulator-shows-connection-error – Athira May 10 '19 at 04:57

2 Answers2

5

Android 6.0 introduced the useCleartextTraffic attribute under the application element in the android manifest. The default value in Android P is “false”. Setting this to true indicates that the app intends to use clear network traffic.

<application
    android:usesCleartextTraffic="true"

</application>

However, this may appear to fix the problem but it opens a threat to data integrity. A better solution is offered in Android 7.0 through network security configuration file

Basi
  • 3,009
  • 23
  • 28
  • 1
    It worked. You saved my day. Thanks. – Priyanka Singhal May 10 '19 at 04:27
  • @PriyankaSinghal hehe – Basi May 10 '19 at 04:28
  • This is not a perfect solution, Because by default value of android:usesClearTextTraffic is TRUE. make sure and check it on an official Android document. – Prince Dholakiya May 10 '19 at 05:37
  • 2
    @DPrince check [this](https://developer.android.com/training/articles/security-config#CleartextTrafficPermitted) official Android document. `Starting with Android 9 (API level 28), cleartext support is disabled by default` – Basi May 10 '19 at 08:50
0

Add below lines in your project's AndroidManifest.xml file

<application
    android:usesCleartextTraffic="true">
            ...
</application>
Prince Dholakiya
  • 3,255
  • 27
  • 43
Mehul Kabaria
  • 6,404
  • 4
  • 25
  • 50