I'm having problems, I want to code the new code for utf-8 and I'm not getting it

-3
// CODIGO ANTIGO QUE NAO ATUALIZA, MAS UTILIZA UTF-8.

    Cache cache = AppController.getInstance().getRequestQueue().getCache();
    if (cache.get(URL_FEED) != null) {
        // fetch the data from cache
        try {
            String data = new String(cache.get(URL_FEED).data, "utf-8");
            try {
                parseJsonFeed(new JSONObject(data));
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

    } else {
        // making fresh volley request and getting json
        JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
                URL_FEED, null, new Response.Listener<JSONObject>() {

                    @Override
                    public void onResponse(JSONObject response) {
                        VolleyLog.d(TAG, "Response: " + response.toString());
                        if (response != null) {
                            parseJsonFeed(response);
                        }
                    }
                }, new Response.ErrorListener() {

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

        // Adding request to volley request queue
        AppController.getInstance().addToRequestQueue(jsonReq);
    }
}


// CODIGO QUE ATUALIZA, MAS NAO UTILIZA UTF-8.
JsonObjectRequest jsonReq =  new  JsonObjectRequest ( Method . GET , 
         URL_FEED ,  null ,  new  Response . Listener < JSONObject >()  {
                    Override 
                    pública  vazio onResponse ( JSONObject resposta )  { 
                        VolleyLog . d ( TAG ,  "Resposta:"  + resposta . toString ()); 
                        se  ( resposta ! =  NULL )  { 
                            parseJsonFeed ( resposta ); 
                        } 
                    } 
                },  nova  resposta . ErrorListener ( )  {

                    Override 
                    pública  vazio onErrorResponse ( VolleyError erro )  { 
                        VolleyLog . d ( TAG ,  "Erro:"  + erro . getMessage ()); 
                    } 
                });
    
asked by anonymous 08.02.2015 / 03:13

1 answer

0

Try this:

JsonArrayRequest jsonReq = new JsonArrayRequest(URL_FEED, new Response.Listener<JSONArray>() {

                    @Override
                    public void onResponse(JSONArray response) {
                        VolleyLog.d(TAG, "Response: " + response.toString());
                        for (int i = 0; i < response.length(); i++) {

                    try {
                            JSONObject feedObj = (JSONObject) response.get(i);

                            FeedItem item = new FeedItem();
                            item.setId(feedObj.getInt("id"));
                            item.setName(feedObj.getString("name"));

                            // Image might be null sometimes
                            String image = feedObj.isNull("image") ? null : feedObj
                                    .getString("image");
                            item.setImge(image);
                            item.setStatus(feedObj.getString("status"));
                            item.setProfilePic(feedObj.getString("profilePic"));
                            item.setTimeStamp(feedObj.getString("timeStamp"));

                            // url might be null sometimes
                            String feedUrl = feedObj.isNull("url") ? null : feedObj
                                    .getString("url");
                            item.setUrl(feedUrl);

                            feedItems.add(item);

                        // notify data changes to list adapater
                        listAdapter.notifyDataSetChanged();
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }}}
                }, new Response.ErrorListener() {

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

        // Adding request to volley request queue
        AppController.getInstance().addToRequestQueue(jsonReq);

Instead:

Cache cache = AppController.getInstance().getRequestQueue().getCache();
if (cache.get(URL_FEED) != null) {
    // fetch the data from cache
    try {
        String data = new String(cache.get(URL_FEED).data, "utf-8");
        try {
            parseJsonFeed(new JSONObject(data));
        } catch (JSONException e) {
            e.printStackTrace();
        }
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

} else {
    // making fresh volley request and getting json
    JsonObjectRequest jsonReq = new JsonObjectRequest(Method.GET,
            URL_FEED, null, new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                    VolleyLog.d(TAG, "Response: " + response.toString());
                    if (response != null) {
                        parseJsonFeed(response);
                    }
                }
            }, new Response.ErrorListener() {

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

    // Adding request to volley request queue
    AppController.getInstance().addToRequestQueue(jsonReq);
    
23.02.2015 / 23:15