コード例 #1
0
  // This method uses RestAdapter and retrofit Interface to fetch and parse the JSON file.
  // Progress bar becomes visible in the beginning of the method and is gone once the is done.
  public void requestData() {

    progressBar.setVisibility(ProgressBar.VISIBLE);

    RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ENDPOINT).build();

    retrofitInterface retrofit = adapter.create(retrofitInterface.class);
    retrofit.getData(
        new Callback<List<Book>>() {

          @Override
          public void success(List<Book> books, Response response) {
            bookList = books;
            progressBar.setVisibility(ProgressBar.GONE);
            updateDisplay();
          }

          @Override
          public void failure(RetrofitError retrofitError) {
            progressBar.setVisibility(ProgressBar.GONE);
            Toast.makeText(getActivity(), R.string.unable_to_fetch, Toast.LENGTH_LONG).show();
          }
        });
  }