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();
}
}