1

I'm using Retrofit 2.7.1 but can't connect!

Here is the error message I am receiving:

java.net.UnknownServiceException: CLEARTEXT communication to www.posh24.se not permitted by network security policy

And here is the code implementation:

1- I implemented bellow lines in Gradle.

implementation 'com.squareup.retrofit2:retrofit:2.7.1'
implementation 'com.google.code.gson:gson:2.8.6'
implementation 'com.squareup.retrofit2:converter-gson:2.7.1'

2- I added these lines in Manifest.

    android:usesCleartextTraffic="true"
    android:networkSecurityConfig="@xml/network_security_config"

3- And for the second line in the previous step, created XML directory and network_security_config.xml containing the bellow script.

<?xml version="1.0" encoding="utf-8"?>
<network-security-config>
    <domain-config cleartextTrafficPermitted="true">
        <domain includeSubdomains="true">www.posh24.se</domain>
    </domain-config>
</network-security-config>

4- I used a simple call but it failed! this is my code:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl("http://www.posh24.se/")
                .addConverterFactory(GsonConverterFactory.create())
                .build();
        ApiServiceByRetrofit apiServiceByRetrofit = retrofit.create(ApiServiceByRetrofit.class);
        Call<String> call = apiServiceByRetrofit.getsomething();

        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
            }
        });
    }
    public interface ApiServiceByRetrofit {
        @GET("kandisar")
        Call<String> getsomething();
    }
}
Stefanija
  • 1,388
  • 2
  • 12
  • 25
  • Does this answer your question? [Android 8: Cleartext HTTP traffic not permitted](https://stackoverflow.com/questions/45940861/android-8-cleartext-http-traffic-not-permitted) – Kapta Feb 21 '20 at 09:23
  • your answer is [here](https://stackoverflow.com/a/50834600/4936833) – Kapta Feb 21 '20 at 09:23
  • I think you don't need both `android:usesCleartextTraffic="true"` and `android:networkSecurityConfig="@xml/network_security_config"`. Try by removing one of these. – mixin27 Feb 21 '20 at 09:30

1 Answers1

0
<uses-permission android:name="android.permission.INTERNET"

From what I can see you are missing the internet connection permission in the Android Manifest. Also is your device connected to the internet?

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.retrofitandroid">

    **<uses-permission android:name="android.permission.INTERNET"/>**
    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

EDIT

Try adding https instead http in the URL https://www.posh24.se

Stefanija
  • 1,388
  • 2
  • 12
  • 25
  • Yes, I added Permission already. I'm so sorry that I forgot to include the error! this is the error: "java.net.UnknownServiceException: CLEARTEXT communication to www.posh24.se not permitted by network security policy" – Navid Elyasi Feb 20 '20 at 19:43