Дооснащение не отвечает на onResponse - PullRequest
0 голосов
/ 09 октября 2019

Ошибка: адаптер не подключен;Пропуск метода onResponse макета не выполняется. Несмотря на то, что приложение не аварийно завершает работу, оно не получает никаких данных с URL-адреса «http». Приложение просто загружается с сообщением progressDialog и показывает всплывающее сообщение метода onFailure.

    private static final String TAG = MainActivity.class.getSimpleName();
    private CustomAdapter adapter;
    private RecyclerView recyclerView;
    private LinearLayoutManager layoutManager;
    ProgressDialog progressDialog;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        recyclerView = findViewById(R.id.customRecyclerView);
//        recyclerView.addItemDecoration(new SImpleDividerItemDecoration(this));
//        layoutManager = new LinearLayoutManager(MainActivity.this);
//        recyclerView.setLayoutManager(layoutManager);
//        requestJsonObject();

        progressDialog = new ProgressDialog(MainActivity.this);
        progressDialog.setMessage("Loading...");
        progressDialog.show();

        //Create handle for the RetrofitInstance interface
        GetDataService service = RetrofitClientInstance.getRetrofitInstance().create(GetDataService.class);

        Call<List<Book>> call = service.getBooks();
        call.enqueue(new Callback<List<Book>>() {
            @Override
            public void onResponse(Call<List<Book>> call, Response<List<Book>> response) {
//                progressDialog.dismiss();
//                Log.d(TAG, "Is title getting fetched: " + )
                Toast.makeText(MainActivity.this, "onResponse working!!", Toast.LENGTH_SHORT).show();
                generateDataList(response.body());
            }

            @Override
            public void onFailure(Call<List<Book>> call, Throwable t) {
                progressDialog.dismiss();
                Toast.makeText(MainActivity.this, "Something went wrong...Please try now!", Toast.LENGTH_SHORT).show();
                Log.e(TAG, t.toString());
            }
        });
    }

    //Method to generate List of data using recyclerView with custom adapter
    private void generateDataList(List<Book> bookList) {
        recyclerView = findViewById(R.id.customRecyclerView);
        adapter = new CustomAdapter(this, bookList);
        RecyclerView.LayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
        recyclerView.setLayoutManager(layoutManager);
        recyclerView.setAdapter(adapter);
    }

}

Модернизация API

    private static Retrofit retrofit;
    private static final String BASE_URL = "http://starlord.hackerearth.com";

    public static Retrofit getRetrofitInstance() {
        if (retrofit == null) {
            retrofit = new retrofit2.Retrofit.Builder()
                    .baseUrl(BASE_URL)
                    .addConverterFactory(GsonConverterFactory.create())
                    .build();
        }
        return retrofit;
    }

}

Модификация интерфейса


    @GET("/kickstarter")
    Call<List<Book>> getBooks();
}
...