2017-04-27 107 views
1

我試着檢索JSONArray數據,但是代碼只解析的JSONObject,我無法弄清楚如何做到這一點JSONArray解析問題

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

的JSON解析您是JSON對象不是一個JSON Array.make JSON對象請求

+0

那是什麼IM不確定該怎麼做,你對此有何幫助? – user3080457