使用volley读取JSON UTF-8

时间:2015-05-20 00:27:27

标签: android android-volley android-json

如何使用Volley库阅读Json utf-8?我在androidhive上尝试这个http://www.androidhive.info/2014/07/android-custom-listview-with-image-and-text-using-volley/课,我正在将json feed转换为UTF-8并将文件上传到我的服务器,但是我的android应用程序没有向我显示数据。

方法中的代码

   private void getDataFromUrl() {

    RequestQueue requestQueue = Volley.newRequestQueue(getActivity());


    // 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);
                            StoriesItem storiesItem = new StoriesItem();
                            storiesItem.setTitle(obj.getString("title"));
                            storiesItem.setThumbnailUrl(obj.getString("image"));
                            storiesItem.setRating(((Number) obj.get("rating"))
                                    .doubleValue());
                            storiesItem.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));
                            }
                            storiesItem.setGenre(genre);

                            // adding movie to movies array
                            storiesListItem.add(storiesItem);

                        } 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();


        }



    }) {

        /**
         * Passing some request headers
         * */
        @Override
        public Map<String, String> getHeaders() throws AuthFailureError {
            HashMap<String, String> headers = new HashMap<String, String>();
            headers.put("Content-Type", "application/json");
            headers.put("charset", "utf-8");
            return headers;
        }

    };



    requestQueue.add(movieReq);
    // Adding request to request queue
    //AppController.getInstance().addToRequestQueue(movieReq);


}

0 个答案:

没有答案