这是json:https://api.jamendo.com/v3.0/tracks/?client_id=70c68f47&format=jsonpretty&limit=200&search=disco
我的问题是如何使用此解决方案使解析方法正确无误。我没有任何额外的休息适配器,凌空应该从这个API获得JSON。
有人会帮我重新制作第一个代码,使用这个API吗?
这是请求解析此API时要更改的内容:
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
JSONArray jsonArray = obj.getJSONArray("results");
for (int i2 = 0; i2 < jsonArray.length(); i2++) {
JSONObject jsonObject3 = jsonArray.getJSONObject(i2);
DataModelList movie = new DataModelList();
movie.setTitle(obj.getString("name"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("id"))
.doubleValue());
movie.setYear(obj.getInt("releasedate"));
movieList.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
数据模型:
public class DataModelList {
private String title, thumbnailUrl;
private int year;
private double rating;
private ArrayList<String> genre;
public DataModelList() {
}
public DataModelList(String name, String thumbnailUrl, int year, double rating,
ArrayList<String> genre) {
this.title = name;
this.thumbnailUrl = thumbnailUrl;
this.year = year;
this.rating = rating;
this.genre = genre;
}
public String getTitle() {
return title;
}
public void setTitle(String name) {
this.title = name;
}
public String getThumbnailUrl() {
return thumbnailUrl;
}
public void setThumbnailUrl(String thumbnailUrl) {
this.thumbnailUrl = thumbnailUrl;
}
public int getYear() {
return year;
}
public void setYear(int year) {
this.year = year;
}
public double getRating() {
return rating;
}
public void setRating(double rating) {
this.rating = rating;
}
}
适配器列表:
public class AdapterDataList extends BaseAdapter {
private Activity activity;
private LayoutInflater inflater;
private List<DataModelList> movieItems;
ImageLoader imageLoader = ControllerSingleton.getInstance().getImageLoader();
public AdapterDataList(Activity activity, List<DataModelList> movieItems) {
this.activity = activity;
this.movieItems = movieItems;
}
@Override
public int getCount() {
return movieItems.size();
}
@Override
public Object getItem(int location) {
return movieItems.get(location);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.list_row, null);
if (imageLoader == null)
imageLoader = ControllerSingleton.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail);
TextView title = (TextView) convertView.findViewById(R.id.title);
TextView rating = (TextView) convertView.findViewById(R.id.rating);
TextView genre = (TextView) convertView.findViewById(R.id.genre);
TextView year = (TextView) convertView.findViewById(R.id.releaseYear);
// getting movie data for the row
DataModelList m = movieItems.get(position);
// thumbnail image
thumbNail.setImageUrl(m.getThumbnailUrl(), imageLoader);
// title
title.setText(m.getTitle());
// rating
rating.setText("Rating: " + String.valueOf(m.getRating()));
// genre
// release year
year.setText(String.valueOf(m.getYear()));
return convertView;
}
}
整体片段我要解析这个JSON:
public class FirstFragment extends Fragment{
private static final String TAG = "log";
private static final String url = "https://api.jamendo.com/v3.0/tracks/?client_id=70c68f47&format=jsonpretty&limit=200&search=disco";
private ProgressDialog pDialog;
private List<DataModelList> movieList = new ArrayList<DataModelList>();
private ListView listView;
private AdapterDataList adapter;
public FirstFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.first_fragment, container, false);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = (ListView) getView().findViewById(R.id.list);
adapter = new AdapterDataList(getActivity(),movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(getActivity());
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
JsonArrayRequest movieReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
for (int i = 0; i < response.length(); i++) {
try {
JSONObject obj = response.getJSONObject(i);
JSONArray jsonArray = obj.getJSONArray("results");
for (int i2 = 0; i2 < jsonArray.length(); i2++) {
JSONObject jsonObject3 = jsonArray.getJSONObject(i2);
DataModelList movie = new DataModelList();
movie.setTitle(obj.getString("name"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("id"))
.doubleValue());
movie.setYear(obj.getInt("releasedate"));
movieList.add(movie);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
// notifying list adapter about data changes
// so that it renders the list view with updated data
adapter.notifyDataSetChanged();
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
ControllerSingleton.getInstance().addToRequestQueue(movieReq);
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
答案 0 :(得分:0)
使用Retrofit和Universal Image Loader解决。