我在Android工作室使用GSON和Retrofit 1,这是稳定的。我需要找出为什么没有显示信息,GSON应该将JSON信息放在getter / setter的模型类中。我正在使用imdb api创建一个电影应用程序,如果你想整个项目审查我可以在这里分享我的代码,但下面是一些代码:
这是我试图填充的详细项目xml,然后放入主xml的列表中。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingRight="0dp"
android:paddingBottom="0dp"
android:paddingTop="0dp"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.evanglazer.moviezone.MainActivity"
android:id="@+id/top">
<ImageView
android:layout_width="200dp"
android:layout_height="200dp"
android:id="@+id/movieView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="IMDB Rating:"
android:id="@+id/imdbText"
android:padding="10dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/movieView"
android:layout_toEndOf="@+id/movieView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/imdbRatingText"
android:padding="10dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/imdbText"
android:layout_toEndOf="@+id/imdbText"
android:singleLine="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="UserBase Vote:"
android:id="@+id/userText"
android:padding="10dp"
android:layout_centerInParent="@+id/imdbText"
android:layout_below="@+id/imdbText"
android:layout_alignRight="@+id/releaseDateText"
android:layout_alignEnd="@+id/releaseDateText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/userRatingText"
android:padding="10dp"
android:layout_below="@+id/imdbText"
android:layout_toRightOf="@+id/imdbText"
android:layout_toEndOf="@+id/imdbText"
android:singleLine="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Release Date:"
android:id="@+id/releaseText"
android:padding="10dp"
android:layout_below="@+id/userText"
android:layout_toLeftOf="@+id/userRatingText"
android:layout_toStartOf="@+id/userRatingText"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="@+id/releaseDateText"
android:layout_below="@+id/releaseText"
android:layout_alignRight="@+id/releaseText"
android:layout_alignEnd="@+id/releaseText" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/middle">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trailer 1"
android:id="@+id/trailer1"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Trailer 2"
android:id="@+id/trailer2"
android:layout_below="@+id/trailer1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="100dp"
android:id="@+id/editText"
android:layout_below="@+id/trailer2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
Here is the detail main:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="@+id/main2"
android:layout_height="match_parent">
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true">
</ListView>
</RelativeLayout>
以下是详细活动:
public class DetailActivity extends ListActivity{
public static final String URL_IMAGE_ENDPOINT = "http://image.tmdb.org";
public static final String URL_API_ENDPOINT = "http://api.themoviedb.org";
FragmentManager fm = getFragmentManager();
List<MovieDetail> details;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_main);
//fm.beginTransaction().replace(R.id.main2, new Detail()).commit();
//fm.beginTransaction().replace(R.id.main2, new NavBar()).commit();
if (isOnline()) {
requestData();
} else {
Toast.makeText(this, "Network isn't available", Toast.LENGTH_LONG).show();
}
}
protected boolean isOnline() {
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo netInfo = cm.getActiveNetworkInfo();
if (netInfo != null && netInfo.isConnectedOrConnecting()) {
return true;
} else {
return false;
}
}
private void requestData()
{
RestAdapter adapter = new RestAdapter.Builder()
.setEndpoint(URL_API_ENDPOINT)
.build();
MovieAPI api = adapter.create(MovieAPI.class);
api.getMovieDetails(new Callback<List<MovieDetail>>() {
@Override
public void success(List<MovieDetail> movieDetails, Response response) {
details = movieDetails;
// update display
updateDisplay();
}
@Override
public void failure(RetrofitError arg0) {
// TODO Auto-generated method stub
}
});
}
protected void updateDisplay() {
//Use FlowerAdapter to display data
MovieDetailAdapter adapter = new MovieDetailAdapter(this, R.layout.movie_detail_fragment, details);
setListAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Intent intent = new Intent(this,SettingActivity.class);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
}
以下是通过改造我的api:
public interface MovieAPI {
@GET("/3/movie/273248?api_key=ea8f68dc2c7b43a3df248b9a638f5fb4")
void getMovieDetails(Callback<List<MovieDetail>> callback);
@GET("3/movie/5493/movie/549")
void getPopularMovies(Callback<List<MovieDetail>> response);
}
以下是我试图从回调中读取的对象的模型:
public class MovieDetail {
private String poster_path;
private String release_date;
private int id;
private String original_title;
private double vote_average;
private int gridPos;
private Bitmap bitmap;
public int getGridPos() {
return gridPos;
}
public void setGridPos(int gridPos) {
this.gridPos = gridPos;
}
public String getPoster_path() {
return poster_path;
}
public void setPoster_path(String poster_path) {
this.poster_path = poster_path;
}
public String getRelease_date() {
return release_date;
}
public void setRelease_date(String release_date) {
this.release_date = release_date;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getOriginal_title() {
return original_title;
}
public void setOriginal_title(String original_title) {
this.original_title = original_title;
}
public double getVote_average() {
return vote_average;
}
public void setVote_average(double vote_average) {
this.vote_average = vote_average;
}
public Bitmap getBitmap() {
return bitmap;
}
public void setBitmap(Bitmap bitmap) {
this.bitmap = bitmap;
}