我正在寻找解决方案,但我无法解决。
那就是我的JSON
{"错误":""" Filme":[{ "标题":"人猿星球的黎明", "图像":" http://api.androidhive.info/json/movies/1.jpg", "评级":8.3, " releaseYear":2014, "流派":["行动","戏剧","科幻"] }, { " title":" District 9", "图像":" http://api.androidhive.info/json/movies/2.jpg", "评分":8, " releaseYear":2009, "流派":["行动","科幻","惊悚"] }, ... ... }]}
那是我的代码
// Movies json url
private static final String url = "https://d....url....";
private ProgressDialog pDialog;
private List<Movie> movieList = new ArrayList<Movie>();
private HorizontalListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (HorizontalListView) 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();
// changing action bar color
getActionBar().setBackgroundDrawable(
new ColorDrawable(Color.parseColor("#1b1b1b")));
// Creating volley request obj
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).getJSONObject(TAG_MOVIE);
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
//Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// 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) {
VolleyLog.d(TAG, "Error: " + error.getMessage());
hidePDialog();
}
});
// Adding request to request queue
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;
}
}
但结果只是白色布局。我认为这是因为我的代码没有找到&#34; Filme&#34; JSON中的变量,但我不知道如何解决它。
任何想法?
谢谢!
答案 0 :(得分:0)
&#34; Filme&#34;它不是JSONObject,它实际上是一个JSONArray。只需将其解析为JSONArray: - )
编辑:
首先,使用StringRequest而不是JSONArray,因为你收到的东西不是JSONArray。
然后,一旦你有一个简单的字符串作为结果,做这样的事情:
JSONObject root = new JSONObject(result);
JSONArray films = root.getJSONArray("Filme");
JSONObject obj;
for (int i = 0; i < films.length(); i++) {
obj = films.getJSONObject(i);
//Put all the parsing for a single Movie JSONObject here
}
答案 1 :(得分:0)
这是我用“Kelevandos”帮助解决的问题。 也许它可以帮助别人。
// Request a string response from the provided URL.
StringRequest movieReq = new StringRequest(Request.Method.GET, url,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
//Log.d(TAG, response.toString());
hidePDialog();
JSONObject root;
try {
root = new JSONObject(response);
JSONArray films = root.getJSONArray("Filme");
JSONObject obj;
for (int i = 0; i < films.length(); i++) {
obj = films.getJSONObject(i);
//Put all the parsing for a single Movie JSONObject here
Movie movie = new Movie();
movie.setTitle(obj.getString("title"));
movie.setThumbnailUrl(obj.getString("image"));
movie.setRating(((Number) obj.get("rating"))
.doubleValue());
movie.setYear(obj.getInt("releaseYear"));
// Genre is json array
JSONArray genreArry = obj.getJSONArray("genre");
ArrayList<String> genre = new ArrayList<String>();
for (int j = 0; j < genreArry.length(); j++) {
genre.add((String) genreArry.get(j));
}
movie.setGenre(genre);
// adding movie to movies array
movieList.add(movie);