我试图检索JSONArray数据,但是代码只是解析JSONObject,我无法弄清楚如何做到这一点
package com.example.darrenwilliamson.st3;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.widget.ListView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.NetworkError;
import com.android.volley.NoConnectionError;
import com.android.volley.ParseError;
import com.android.volley.Response;
import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
import java.util.List;
public class ActivityNews extends Activity {
// Log tag
private static final String TAG = ActivityNews.class.getSimpleName();
// News json url
String url = "https://newsapi.org/v1/articles?source=bbc-sport&sortBy=top&apiKey=45570115d8af4e89880161af1c5fa087";
private ProgressDialog pDialog;
private List<DataSet> dataList = new ArrayList<DataSet>();
private ListView listView;
private CustomListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_news);
listView = (ListView) findViewById(R.id.list);
adapter = new CustomListAdapter(this, dataList);
listView.setAdapter(adapter);
pDialog = new ProgressDialog(this);
// Showing progress dialog before making http request
pDialog.setMessage("Loading...");
pDialog.show();
// Creating volley request obj
JsonArrayRequest newsReq = new JsonArrayRequest(url,
new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
hidePDialog();
// Parsing json
try {
JSONArray jr = new JSONArray(url);
JSONObject jb = (JSONObject)jr.getJSONObject(0);
JSONArray st = jb.getJSONArray("articles");
for(int i=0;i<st.length();i++) {
JSONObject obj = response.getJSONObject(i);
DataSet news = new DataSet();
news.setAuthor_(obj.getString("author"));
news.setTitle_(obj.getString("title"));
news.setDescription_(obj.getString("description"));
news.setImage_(obj.getString("url"));
news.setUrlToImage_(obj.getString("urlToImage"));
news.setPublished_(obj.getString("published"));
// adding news to news array
dataList.add(news);
}
} catch (final JSONException e) {
Log.e(TAG, "Json parsing error: " + e.getMessage());
runOnUiThread(new Runnable() {
@Override
public void run() {
Toast.makeText(getApplicationContext(),
"Json parsing error: " + e.getMessage(),
Toast.LENGTH_LONG).show();
}
});
}
// 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();
if (error instanceof TimeoutError) {
Log.e(TAG, "TimeoutError");
} else if (error instanceof NoConnectionError) {
Log.e(TAG,"NoConnectionError");
} else if (error instanceof AuthFailureError) {
Log.e(TAG,"AuthFailureError");
} else if (error instanceof ServerError) {
Log.e(TAG,"ServerError");
} else if (error instanceof NetworkError) {
Log.e(TAG,"NetworkError");
} else if (error instanceof ParseError) {
Log.e(TAG,"ParseError" + error.getMessage());
}
}
});
// Adding request to request queue
AppController.getInstance().addToRequestQueue(newsReq);
}
@Override
public void onDestroy() {
super.onDestroy();
hidePDialog();
}
private void hidePDialog() {
if (pDialog != null) {
pDialog.dismiss();
pDialog = null;
}
}
}
基本上我要求的只是数据作者,标题,描述,URL等。 请帮忙
错误:
ParseErrororg.json.JSONException:
答案 0 :(得分:0)
您的解析的JSON是json对象而不是JSON Array.make Json对象请求