簡體   English   中英

每當我使用改造從網絡訪問數據時,它工作正常,但第二次無法使用改造通過網絡找到數據

[英]whenever i acces data from network using retrofit it works fine but in 2nd time can't find data through network using retrofit

我正在嘗試使用改造從網絡訪問數據(數據采用 gson 的形式,我使用的是 WordPress rest API)但無法訪問。 它向我顯示了一個錯誤,如 data is null 看起來像改造找不到數據但一切都很好......看起來代碼很好,我不知道如何解決這個問題。 請幫助我我是一個新的開發人員..這需要我的 3 天

每當我調用 getRetrofit() 方法時它都可以正常工作......但是當我調用 getImageRetrofit() 然后看起來這個方法不起作用......這個方法返回空值,如 logcat 所示:ImageInfo: info: null

   private void getRetrofit() {
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();
    RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
    Call<List<WPPost>> call = service.getPostInfo();
    call.enqueue(new Callback<List<WPPost>>() {
        @Override
        public void onResponse(Call<List<WPPost>> call, Response<List<WPPost>> response) {
            Log.e("Latest","response: "+response.body());
            for (int i=0; i<response.body().size(); i++)
            {
                Log.e("main ","title "+response.body().get(i).getTitle().getRendered() + " " +
                        response.body().get(i).getId() );
                String tempDate = response.body().get(i).getDate();
                tempDate = tempDate.replace("T"," ");

                String tempImageHref = response.body().get(i).getLinks().getWpFeaturedmedia().get(0).getHref();
                Log.e("Href", "onResponse: "+tempImageHref);
                String link = response.body().get(i).getLink();
                Log.e("PostLink",link);

                getImageRetrofit(tempImageHref);


                list.add(new LatestModel(
                        response.body().get(i).getTitle().getRendered(),
                        tempDate,
                        tempImageHref,
                        LatestModel.IMAGE_TYPE,
                        response.body().get(i).getLink()
                        )
                );
            }
            adapter.notifyDataSetChanged();
        }

        @Override
        public void onFailure(Call<List<WPPost>> call, Throwable t) {
            t.printStackTrace();
        }
    });

}



private void getImageRetrofit(String ImageHref) {
    Log.e("getImageRetrofit","called "+ImageHref);
    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(baseUrl)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    RetrofitArrayApi service = retrofit.create(RetrofitArrayApi.class);
    Call<List<WPPostImage>> callImage = service.getImageInfo(ImageHref);
    callImage.enqueue(new Callback<List<WPPostImage>>() {
        @Override
        public void onResponse(Call<List<WPPostImage>> call, Response<List<WPPostImage>> response) {

            Log.e("ImageInfo","info: "+response.body());
        }

        @Override
        public void onFailure(Call<List<WPPostImage>> call, Throwable t) {
        Log.e("Link Failed: ",": t.printStackTrace()" );
        }
    });


}

這是我的 RetrofitArrayApi 接口。:

public interface RetrofitArrayApi {
@GET("wp-json/wp/v2/posts?per_page=4")
Call<List<WPPost>> getPostInfo();

@GET("{id}")
Call<List<WPPostImage>> getImageInfo(@Path("id") String ImageHref);     }

你在評論中說temImageHref: mubashirsaddique.com/wp-json/wp/v2/media/1780並且你的基本網址是baseUrl = "mubashirsaddique.com" 因此,您在調用getImageInfo時向此地址mubashirsaddique.com/mubashirsaddique.com/wp-json/wp/v2/media/1780發送請求。

更改您的getPostInfo服務。 它應該只返回 id(在您的情況下為1780 )作為 href 值並修改RetrofitArrayApi

@GET("wp-json/wp/v2/media/{id}")
Call<List<WPPostImage>> getImageInfo(@Path("id") String ImageHref); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM