Problem with VOLLEY accents

1
    private class DescargarNoticias extends AsyncTask<Void, Void, Void> {
        public void onPreExecute() {

        }
        public void onPostExecute(Void unused) {
        }

        @Override
        protected Void doInBackground(Void... params) {
            StringRequest request = new StringRequest(Request.Method.POST, getString(R.string.municipio_noticias), new Response.Listener<String>() {
                @Override
                public void onResponse(String response) {
                    if (!response.equals("null")){
                        try {
                            basededatos.EliminarNoticias();
                            JSONArray jsonArray = new JSONArray(response);
                            for (int i = 0; i < jsonArray.length(); i++) {
                                basededatos.RegistrarNoticias(
                                        jsonArray.getJSONObject(i).getInt("folio"),
                                        jsonArray.getJSONObject(i).getString("encabezado"),
                                        jsonArray.getJSONObject(i).getString("fecha"),
                                        jsonArray.getJSONObject(i).getString("texto"),
                                        jsonArray.getJSONObject(i).getString("autor"));

                                Log.d("ENCABEZADO",jsonArray.getJSONObject(i).getString("encabezado"));
                               //D/ENCABEZADO: Entrega de un apoyo económi... <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
                            }
                            ConsultarNoticias();
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, new Response.ErrorListener() {
                @Override
                public void onErrorResponse(VolleyError error) {

                }
            }) {
                @Override
                protected Map<String, String> getParams() throws AuthFailureError {
                    Map<String, String> parameters = new HashMap<String, String>();
                    return parameters;
                }
            };
            requestQueue.add(request);
            return null;
        }
    }

Data that throws me:

//D/ENCABEZADO: Entrega de un apoyo económi...
  

PHP FILE

    <?php
if($_SERVER["REQUEST_METHOD"]=="POST"){
    include('conexion.php');
    $result  = mysqli_query($con,"SELECT * FROM noticias") or mysqli_error($con);
    while ($row = $result->fetch_assoc()) {
         $arr[] = $row;
    }
    $json = json_encode($arr,JSON_UNESCAPED_UNICODE);
    echo $json;
}
?>

How can I make him accept the accents?

    
asked by DoubleM 11.01.2018 в 08:44
source

1 answer

1

Since in comments you say that the data is fine in the database, I would verify three things:

  • That when creating the connection I set the charset to utf-8;

  • Before printing the JSON, put the header:

    header('Content-Type: application/json; charset=utf8');
    
  • Create and Print the JSON without further ado, without any other parameters in json_encode :

    $json = json_encode($arr); 
    echo $json; 
    
  • So I use it in an Android App and it works perfectly for me.

        
    answered by 13.01.2018 / 20:21
    source