【问题标题】:Can not get response from 000webhost using Retrofit in Android Studio在 Android Studio 中使用 Retrofit 无法从 000webhost 获得响应
【发布时间】:2021-11-23 08:55:28
【问题描述】:

我是 Android 新手,我有一个名为“https://codyderunner.000webhostapp.com/Server/songbannner.php”的文件,响应 JSON 格式如下:Json response from my file

我想在 Android Studio 中使用 Retrofit 获取这些 JSON。

我有一个DataService接口:

public interface DataService {
    @GET("songbannner.php")
    Call<List<Ads>> GetDataBanner();
}

广告是我想要接收的数据的模型类。

我的APIService.java文件代码:

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

}

我的 API.RetrofitClient 代码:

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

在我的主类中,我有一个从该文件中获取数据的函数:

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) {

        }
    });
}

现在,问题是,我无法从文件中收到任何响应,我认为这可能是因为当我尝试获取文件响应的数据时进行了身份验证。当我用浏览器打开链接时它很好,因为它记住了我的密码,但是当我尝试从 Android Studio 读取文件的 JSON 时,它似乎没有希望。我还尝试阅读 Postman 上的链接,它发布了“未经授权”,所以我认为关键是如何将我的用户名和密码放入 Retrofit 中,以便文件会像在浏览器中一样响应。

有人遇到同样的问题吗?帮助我,我尝试了我所知道的一切。非常感谢。

【问题讨论】:

    标签: java android api retrofit2 okhttp


    【解决方案1】:

    好的,最后,我发现我存储.php文件的网站需要用户名和密码,只需将其添加到Okhttpclient中即可。

    首先,我创建了一个名为BasicAuthInterceptor的java类:

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

    并将密码和用户名添加到您的 OkhttpClient 中:

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

    使用您用于登录网站的用户名和密码。我花了 2 天时间才发现,见鬼。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-01-15
      • 1970-01-01
      相关资源
      最近更新 更多