我假设您正在从端点返回 JSON。如果是这种情况,那么您可以简单地指向此端点并解析 JSON 以获取最终可以在视图上呈现的相应 url。 (参考下面的例子)。
您可以使用 Volley 库或 Retrofit 库来处理 HTTP 请求。
//--------------------------------------------- --------------------------
private static final String endpoint = "http://api.youtube.********/json/videos.json";
//*************************************
//** JSON Video Output from Endpoint
//*************************************
[{
"name": "Rocky V",
"url": {
"small": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
"medium": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
"large": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
},
"timestamp": "February 1, 2012"
},
{
"name": "Rambo",
"url": {
"small": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
"medium": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player",
"large": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
},
"timestamp": "March 5, 2014"
}]
//*************************************
//** Parse JSON to Fetch Videos
//*************************************
private void fetchVideos() {
JsonArrayRequest req = new JsonArrayRequest(endpoint,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
videos.clear();
for (int i = 0; i < response.length(); i++) {
try {
JSONObject object = response.getJSONObject(i);
Video video = new Video();
video.setName(object.getString("name"));
JSONObject url = object.getJSONObject("url");
video.setSmall(url.getString("small"));
video.setMedium(url.getString("medium"));
video.setLarge(url.getString("large"));
video.setTimestamp(object.getString("timestamp"));
videos.add(video);
} catch (JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
}
}
mAdapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
//pDialog.hide();
}
});
// Adding request to request queue
Application.getInstance().addToRequestQueue(req);
}
//-------------------------------------------------------------------