我是android的新手,并尝试编码来显示从API获取的海报,问题是在运行之后然后它只显示一张海报。请给我一些建议
1.poster_image.xml
<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/poster"
android:layout_width="500dp"
android:layout_height="60dp" />
2.MainActivity.java
public class MainActivity extends AppCompatActivity {
ArrayList<String> moviesList = new ArrayList<>();
ImageView mImageView;
String url = "https://api.themoviedb.org/3/movie/popular?api_key=(MY API KEY)";
String image_url = "http://image.tmdb.org/t/p/w185";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.poster_image);
mImageView = (ImageView) findViewById(R.id.poster);
// Fetch Data from API
RequestQueue requestQueue = Volley.newRequestQueue(this);
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("results");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject movies = jsonArray.getJSONObject(i);
String poster = movies.getString("poster_path");
moviesList.add(poster);
Log.i("IS", String.valueOf(moviesList));
}
for (int j = 0; j < moviesList.size(); j++) {
Picasso.with(getApplicationContext())
.load(image_url + moviesList.get(j))
.into(mImageView);
Log.i("I", image_url + moviesList.get(j));
}
} catch (JSONException e) {
e.printStackTrace();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Error", "Wrong");
}
});
requestQueue.add(jsonObjectRequest);
}
}
答案 0 :(得分:0)
您必须制作一个recylerview来显示来自服务器的图像列表。 使用Glide,Picasso和Fresco从服务器加载图像。 浏览此链接。 Show images in recylerview from server