2017-01-27 4 views
0

Не отображаются какие-либо ошибки, но данные не формируют сервер формы.Не удалось получить данные с сервера с помощью «Дооснащения»?

мой Основная деятельность

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> 

я не смог получить данные с сервера?

+0

Какая версия модификации вы используете? – AnixPasBesoin

+0

Ваш базовый url должен: 'http: // www.myeducationhunt.com'. И проверить журналы правильно или нет –

+0

Я использую 1.9.0 retrofit – seon

ответ

1

Вы добавляете сюда полный URL-адрес "public static final String ROOT_URL = "http://www.myeducationhunt.com/api/v1/colleges";" , который не подходит, вам просто нужно добавить базовый URL-адрес здесь. Изменить этот адрес для

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

Также в интерфейсе удалить начальный "/" Это @GET("/api/v1/colleges") к этому ... @GET("api/v1/colleges")

edit1: вашей проблемы с OnCreate методами .. удалить OnCreate метод со следующим кодом

@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); 
    } 
+0

https://www.simplifiedcoding.net/retrofit-android-tutorial-to-get-json-from-server/ Я следил за этим уроком – seon

+0

@seon Okey, что учебник в порядке.Проверьте Root_url в этом учебнике, что они просто добавляют домен. Вы добавляете полный url в место root_url – Amsheer

+0

i ничего не изменил. Happens.sir – seon

Смежные вопросы