I am new to Android, i have a file named "https://codyderunner.000webhostapp.com/Server/songbannner.php" which response JSON like: Json response from my file
I want to get those JSON in Android studio using Retrofit.
I have a DataService interface:
public interface DataService {
@GET("songbannner.php")
Call<List<Ads>> GetDataBanner();
}
Ads is a model class of the Data I want to Receive.
My APIService.java file code:
public class APIService {
private static String base_url = "https://codyderunner.000webhostapp.com/Server/";
public static DataService getService(){
return APIRetrofitClient.getClient(base_url).create(DataService.class);
};
}
My API.RetrofitClient code:
public class APIRetrofitClient {
private static Retrofit retrofit = null;
public static Retrofit getClient(String base_url){
OkHttpClient okHttpClient = new OkHttpClient.Builder()
.readTimeout(10000, TimeUnit.MILLISECONDS)
.writeTimeout(10000, TimeUnit.MILLISECONDS)
.connectTimeout(10000, TimeUnit.MILLISECONDS)
.retryOnConnectionFailure(true)
.protocols(Arrays.asList(Protocol.HTTP_1_1))
.build();
Gson gson = new GsonBuilder().setLenient().create();
retrofit = new Retrofit.Builder()
.baseUrl(base_url)
.client(okHttpClient)
.addConverterFactory(GsonConverterFactory.create(gson))
.build();
return retrofit;
};
In my main class, i have a function to get data from that file:
private void GetData(){
DataService dataService = APIService.getService();
Call<List<Ads>> callback = dataService.GetDataBanner();
callback.enqueue(new Callback<List<Ads>>() {
@Override
public void onResponse(Call<List<Ads>> call, Response<List<Ads>> response) {
ArrayList<Ads> banner = (ArrayList<Ads>) response.body();
Log.d("AAAA", banner.get(0).getNameSong());
}
@Override
public void onFailure(Call<List<Ads>> call, Throwable t) {
}
});
}
Now, the thing is, i can not receive any response from the file, I think it may because of the authentication when I try to get the data which the file response. Its fine when I open the link with the browser, cause it remember my password, but when I try to read the JSON of the File from Android Studio, Its seem hopeless. I also try to read the link on Postman, its released "Unauthorized", so I think the point is how can I put my user-name and password in Retrofit so the file will response as it did in the browser.
Is there anyone meet the same problem? Help me, I tried Everything I know. Many thanks.