Not Able to Fetch Data from server using Retrofit?

ε祈祈猫儿з 提交于 2019-12-25 09:19:21

问题


Not any errors are showing but data are not fetching form server.

my Main Activity

public class RetroMain extends AppCompatActivity implements ListView.OnItemClickListener {

    public static final String ROOT_URL = "http://www.myeducationhunt.com/api/v1/colleges";

    //Strings to bind with intent will be used to send data to other activity
    public static final String KEY_BOOK_ID = "id";
    public static final String KEY_BOOK_NAME = "name";
    public static final String KEY_BOOK_ADDRESS = "address";
    public static final String KEY_BOOK_DISTRICT = "district";
    private ListView listView;

    //List of type books this list will store type Book which is our data model
    private List<Book> books;


    @Override
    public void onCreate(Bundle savedInstanceState, PersistableBundle persistentState) {
        super.onCreate(savedInstanceState, persistentState);
        setContentView(R.layout.activity_retro_main);
        listView = (ListView) findViewById(R.id.listViewBooks);
        getBooks();
        listView.setOnItemClickListener(this);

    }


    private void getBooks() {
        //While the app fetched data we are displaying a progress dialog
        final ProgressDialog loading = ProgressDialog.show(this, "Fetching Data", "Please wait...", false, false);

        //Creating a rest adapter
        RestAdapter adapter = new RestAdapter.Builder().setEndpoint(ROOT_URL).build();

        //Creating an object of our api interface
        BooksAPI api = adapter.create(BooksAPI.class);

        //Defining the method
        api.getBooks(new Callback<List<Book>>() {
            @Override
            public void success(List<Book> list, Response response) {
                //Dismissing the loading progressbar
                loading.dismiss();

                //Storing the data in our list
                books = list;

                //Calling a method to show the list
                showList();
            }

            @Override
            public void failure(RetrofitError error) {
                //you can handle the errors here
            }
        });
    }

    //Our method to show list
    private void showList() {
        //String array to store all the book names
        String[] items = new String[books.size()];

        //Traversing through the whole list to get all the names
        for (int i = 0; i < books.size(); i++) {
            //Storing names to string array
            items[i] = books.get(i).getName();
        }

        //Creating an array adapter for list view
        ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.simple_list, items);

        //Setting adapter to listview
        listView.setAdapter(adapter);
    }


    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

//Creating an intent
        Intent intent = new Intent(this, ShowBookDetails.class);

        //Getting the requested book from the list
        Book book = books.get(position);

        //Adding book details to intent
        intent.putExtra(KEY_BOOK_ID, book.getId());
        intent.putExtra(KEY_BOOK_NAME, book.getName());
        intent.putExtra(KEY_BOOK_ADDRESS, book.getAddress());
        intent.putExtra(KEY_BOOK_DISTRICT, book.getDistrict());

        //Starting another activity to show book details
        startActivity(intent);
    }
}

my getter and setters

public class Book {
    private int id;
    private String name;
    private String address;
    private String district;

    public int getId() {
        return id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getAddress() {
        return address;
    }

    public String getDistrict() {
        return district;
    }

    public void setDistrict(String district) {
        this.district = district;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public void setId(int id) {
        this.id = id;
    }
}

my API Interface

public interface BooksAPI {

    @GET("/api/v1/colleges")
    public void getBooks(Callback<List<Book>> response);
}

ShowBookDetails class

public class ShowBookDetails extends AppCompatActivity {

    //Defining views
    private TextView id;
    private TextView name;
    private TextView address;
    private TextView district;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show_book_details);

        //Initializing Views
        id = (TextView) findViewById(R.id.id);
        name = (TextView) findViewById(R.id.name);
        address = (TextView) findViewById(R.id.address);
        district = (TextView) findViewById(R.id.district);

        //Getting intent
        Intent intent = getIntent();

        //Displaying values by fetching from intent
        id.setText(String.valueOf(intent.getIntExtra(RetroMain.KEY_BOOK_ID, 0)));
        name.setText(intent.getStringExtra(RetroMain.KEY_BOOK_NAME));
        address.setText(intent.getStringExtra(RetroMain.KEY_BOOK_ADDRESS));
        district.setText(String.valueOf(intent.getIntExtra(RetroMain.KEY_BOOK_DISTRICT, 0)));
    }
}

my main xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Mystic.activity.RetroMain">
    <ListView
        android:id="@+id/listViewBooks"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"></ListView>
</RelativeLayout>

my activity_show_book_detail

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin">

    <TextView
        android:id="@+id/id"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24dp" />

    <TextView
        android:id="@+id/name"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24dp" />

    <TextView
        android:id="@+id/address"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24dp" />

    <TextView
        android:id="@+id/district"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24dp" />

</LinearLayout>

simple_list

<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:padding="10dp"
    android:textSize="20sp">

</TextView>

i am not able to fetch data from server?


回答1:


You are adding full url here "public static final String ROOT_URL = "http://www.myeducationhunt.com/api/v1/colleges";" which is not correct you just needs to add the base url here. Modify this url to

public static final String ROOT_URL = "http://www.myeducationhunt.com/";

Also in your interface remove the initial "/" This @GET("/api/v1/colleges") to this ... @GET("api/v1/colleges")

Edit1: Your problem with oncreate method.. remove your oncreate method with the following code

@Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        listView = (ListView) findViewById(R.id.listViewBooks);
        getBooks();
        listView.setOnItemClickListener(this);
    }


来源:https://stackoverflow.com/questions/41886821/not-able-to-fetch-data-from-server-using-retrofit

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!