使用Retrofit无法从服务器获取数据?

时间:2017-01-27 04:09:05

标签: android retrofit

没有显示任何错误,但数据未从表单服务器获取。

我的主要活动

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

我的吸气鬼和二传手

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

我的API接口

public interface BooksAPI {

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

ShowBookDetails类

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

我的主要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>

我的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>

我无法从服务器获取数据?

1 个答案:

答案 0 :(得分:1)

您在此处添加完整网址var locations = [ [ "BANGALORE", "215 West Girard Avenue 19123", "12.9716", "77.5946" ], [ "JAIPUR", "5360 Old York Road 19141", "26.9124", "75.7873" ], [ "MUMBAI", "1350 W Girard Avenue 19123", "19.0760", "72.8777" ], [ "HYDERABAD", "1950 W Girard Avenue 19123", "17.3850", "78.4867" ], [ "CHENNAI", "1950 W Girard Avenue 19123", "13.0827", "78.4867" ] ]; gmarkers = []; var map = new google.maps.Map(document.getElementById('map'), { zoom: 12, center: new google.maps.LatLng(39.9995601, -75.1395161), mapTypeId: google.maps.MapTypeId.ROADMAP }); var infowindow = new google.maps.InfoWindow(); function createMarker(latlng, html) { var marker = new google.maps.Marker({ position: latlng, map: map }); google.maps.event.addListener(marker, 'click', function() { infowindow.setContent(html); infowindow.open(map, marker); }); return marker; } for (var i = 0; i < locations.length; i++) { gmarkers[locations[i][0]] = createMarker(new google.maps.LatLng(locations[i][2], locations[i][3]), locations[i][0] + "<br>" + locations[i][1]); } 这是不正确的,你只需要在这里添加基本网址。 将此网址修改为

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

同样在你的界面中删除最初的&#34; /&#34; 这个public static final String ROOT_URL = "http://www.myeducationhunt.com/"; 对此... @GET("/api/v1/colleges")

<强> EDIT1: 您使用oncreate方法的问题..使用以下代码删除您的oncreate方法

@GET("api/v1/colleges")