I’m in trouble, I want to encode the new code for utf-8 and I’m not getting it

Asked

Viewed 109 times

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

    And what is the question? PS: What are these pública, vazio, se and nova resposta?

  • My question is: this first code is where utf-8 is working but without updating my .json. The second code already updates, plus the phrases of my file. json get confused without utf-8. How do I get it to appear in the second code updating and appearing the accent phrases?

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);
  • If possible explain what this code does, and where the problem was in another code.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.