Hello dear Android developers! I am trying to get data from my web service with using retrofit now. But when I send call to my web service Callback function is always showing onFailure.
I carefully create my retrofit classes, but still have problem about getting data. Also, I took Internet permission.
Here you can see my retrofit classes:
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class RetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String baseUrl) {
if (retrofit==null) {
retrofit = new Retrofit.Builder()
.baseUrl(baseUrl)
.addConverterFactory(GsonConverterFactory.create())
.build();
}
return retrofit;
}
}
public class ApiUtils {
public static final String BASE_URL = "http://huseyinozkoc.com/";
//bu base link ana link olmalı alt kolları interface içerisinde belirtilir.
public static RetrofitInterface getRetrofitInterface() {
return RetrofitClient.getClient(BASE_URL).create(RetrofitInterface.class);
}
}
public interface RetrofitInterface {
@GET("turkey/get_All_details.php")
Call<PlaceCevap> getAllPlaces();
}
As I mentioned before, I checked to all retrofit classes but did not see any problem.
Then I carefully try to check my MainActivity but still did not see any unusual things.
Here my mainActivity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.baba);
places = new ArrayList<>();
recyclerView.setLayoutManager(new LinearLayoutManager(this.getApplicationContext()));
recyclerView.setHasFixedSize(true);
placeInterface = ApiUtils.getRetrofitInterface();
placeInterface.getAllPlaces().enqueue(new Callback<PlaceCevap>() {
@Override
public void onResponse(Call<PlaceCevap> call, Response<PlaceCevap> response) {
places = response.body().getPlaces();
adapter= new PlaceAdapter(places);
recyclerView.setAdapter(adapter);
Toast.makeText(MainActivity.this, "got response" , Toast.LENGTH_SHORT).show();
}
@Override
public void onFailure(Call<PlaceCevap> call, Throwable t) {
Toast.makeText(MainActivity.this, "fail" , Toast.LENGTH_SHORT).show();
}
});
As a result, Dear developers Why I am not getting data from web service? What is the reason of it? Please write! Good day everyone and smooth development processes!
CodePudding user response:
I solved problem. Firstly, I took this error "CLEARTEXT communication not supported."
For solution, you should add android:usesCleartextTraffic="true"
into manifest file.
Then, you should add these permissions additionally INTERNET permission for "java.net.SocketException: socket failed: EPERM" error.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Add all of them into the manifest file.
Finally, you can get "fails SSL handshakes" error. You should change your base url like "http" or "https" try both of them.
If you are getting still error build your project and delete your app from emulator and run it again.