我正在尝试获取JSON格式的数据。但是在获取时创建的列表仅针对每个列表项具有最后的对象详细信息。请指导。
以下是获取数据的Mainactivity代码。
package com.example.ankurdell.customlistview;
import java.util.ArrayList;
import java.util.List;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.provider.Settings;
import android.util.Log;
import android.view.Menu;
import android.widget.ListView;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
public class MainActivity extends Activity {
// Log tag
private static final String TAG = MainActivity.class.getSimpleName();
// Movies json url
private static final String url = "https://api.dailymotion.com/videos";
private ProgressDialog pDialog;
private ArrayList<DailyMovie> movieList = new ArrayList<>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, movieList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
JsonObjectRequest movieReq = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG, response.toString());
hidePDialog();
// Parsing json
try {
JSONArray jsonArray = response.getJSONArray("list");
DailyMovie movie = new DailyMovie();
for (int a = 0; a < jsonArray.length(); a++) {
JSONObject obj = jsonArray.getJSONObject(a);
System.out.println("Object Value " + a + " " + obj.toString());
movie.setId(obj.getString("id"));
movie.setTitle(obj.getString("title"));
movie.setOwner(obj.getString("owner"));
movie.setChannel(obj.getString("channel"));
// adding movie to movies array
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) {
System.out.println("Error");
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
app.AppController.getInstance().addToRequestQueue(movieReq);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
这里是DailyMovie类
package com.example.ankurdell.customlistview;
/**
* Created by Ankur Dell on 8/20/2017.
*/
public class DailyMovie {
private String id;
private String title;
private String channel;
private String owner;
public DailyMovie() {
}
public DailyMovie(String moviename, String movieid, String theme, String movieowner) {
this.id = movieid;
this.title = moviename;
this.channel = theme;
this.owner = movieowner;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getChannel() {
return channel;
}
public void setChannel(String channel) {
this.channel = channel;
}
public String getOwner() {
return owner;
}
public void setOwner(String owner) {
this.owner = owner;
}
}
可以提出一些我做错的建议。我提到https://www.androidhive.info/2014/09/android-json-parsing-using-volley/进行编码。
答案 0 :(得分:1)
DailyMovie只是一个元素。正确?
您必须在for语句
中声明DailyMovie变量for (int a = 0; a < jsonArray.length(); a++) {
JSONObject obj = jsonArray.getJSONObject(a);
System.out.println("Object Value " + a + " " + obj.toString());
DailyMovie movie = new DailyMovie();
movie.setId(obj.getString("id"));
movie.setTitle(obj.getString("title"));
movie.setOwner(obj.getString("owner"));
movie.setChannel(obj.getString("channel"));
// adding movie to movies array
movieList.add(movie);
}