Can not get response from 000webhost using Retrofit in Android Studio

Viewed 199

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.

1 Answers

OK, finally, I find out that the website which I store my .php file require user name and password, just add it into the Okhttpclient.

First, I created a java class called BasicAuthInterceptor:

import java.io.IOException;
import okhttp3.Credentials;
import okhttp3.Interceptor;
import okhttp3.Request;
import okhttp3.Response;

public class BasicAuthInterceptor implements Interceptor {

    private String credentials;

    public BasicAuthInterceptor(String user, String password) {
        this.credentials = Credentials.basic(user, password);
    }

    @Override
    public Response intercept(Chain chain) throws IOException {
        Request request = chain.request();
        Request authenticatedRequest = request.newBuilder()
                .header("Authorization", credentials).build();
        return chain.proceed(authenticatedRequest);
    }
}

And add it with password and username into your OkhttpClient:

OkHttpClient okHttpClient = new OkHttpClient.Builder()
            .addInterceptor(new BasicAuthInterceptor(username, password))
            .build();

with your username and password which you use to login into the website. It took me 2 days finding out, hell ya.

Related