使用凌空的API?

时间:2015-06-01 21:41:19

标签: android arrays json rest android-volley

我正在尝试使用图书馆凌空的API,但我不能,因为它在我运行应用程序时没有向我显示任何内容。

我认为问题在于URL API中包含了它(json):

{"books":[{"book":{PARAMETERS...}}]}

所以我无法访问我的参数。然后我的网址API在网址末尾没有.json,但我不认为这是问题所在。

public class MainActivity extends Activity {


private static final String TAG = MainActivity.class.getSimpleName();


private static final String url = "http://############";
private ProgressDialog pDialog;
private List<Books> booksList = new ArrayList<Books>();
private ListView listView;
private Adapter adapter;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    listView = (ListView) findViewById(R.id.list);
    adapter = new Adapter(this, booksList);
    listView.setAdapter(adapter);

    pDialog = new ProgressDialog(this);
    pDialog.setMessage("Loading...");
    pDialog.show();

    // changing action bar color
    //getActionBar().setBackgroundDrawable(
      //      new ColorDrawable(Color.parseColor("#1b1b1b")));

    JsonArrayRequest booksReq = new JsonArrayRequest(url,
            new Response.Listener<JSONArray>() {
                @Override
                public void onResponse(JSONArray response) {
                    Log.d(TAG, response.toString());
                    hidePDialog();

                    for (int i = 0; i < response.length(); i++) {
                        try {

                            JSONObject obj = response.getJSONObject(i);
                            Books books = new Books();
                            Books.setTitle(obj.getString("title"));
                            Books.setPhoto_url(obj.getString("image"));

                            booksList.add(books);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
            }, new Response.ErrorListener() {

                public void onErrorResponse(VolleyError error) {
                    VolleyLog.d(TAG, "Error: " + error.getMessage());
                    hidePDialog();

                }
            });
    Controller.getInstance().addToRequestQueue(booksReq);
}
@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;
}
}

我在URL API中的json:

{
    "books": [
        {
            "book": {
                "title": "namebook",
                "photo_url": {
                    "src": "http://######",
                    "alt": ""
                }
            }
        }
    ]
}

那么我该如何展示它们呢?

1 个答案:

答案 0 :(得分:0)

尝试使用 JsonObjectRequest(int method, String url, JSONObject jsonRequest, Listener<JSONObject> listener, ErrorListener errorListener)代替JsonArrayRequest,因为您的回复似乎是JSONObject

此外,您的回复

中没有此类关键 image
Books.setPhoto_url(obj.getString("image"));

查看您的JSON回复,如果您尝试使用以下代码,则应该有效:

JsonObjectRequest artistReq = new JsonObjectRequest(Method.GET, url, null, new Listener<JSONObject>() {
    @Override
    public void onResponse(JSONObject response) {
        Log.d(TAG, response.toString());
        hidePDialog();

        try {
            if (response.optJSONArray("books") != null) {
                JSONArray books = response.getJSONArray("books");

                for (int i = 0; i < books.length(); i++) {
                    JSONObject book = books.getJSONObject(i);

                    String title = book.getString("title");

                    // next K,V pair consist of JSONObject photo_url
                    JSONObject photoUrl = book.getJSONObject("photo_url");
                    String photoSrc = photoUrl.getString("src");
                    String photoAlt = photoUrl.getString("alt");
                    // use extracted values in your Book object
                }
            }
        } catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        // notifying list adapter about data changes
        // so that it renders the list view with updated data
        adapter.notifyDataSetChanged();
    }
}, new ErrorListener() {
    @Override
    public void onErrorResponse(VolleyError error) {
        VolleyLog.d(TAG, "Error: " + error.getMessage());
        hidePDialog();

    }
});

希望它会有所帮助!