I'm getting information using Gson in a PHP file. When this information is received, words containing accents are not encoded.
I already read: Android JSON CharSet UTF-8 problems
Converting String to Android JSONObject loses utf -8
An Error in Converting android JSON to UTF-8
But none of these worked for me, as it does not follow the example of my app.
musicas.php
[{"nome_titulo":"Música 1","nome_subtitulo":"Título","indicador_tempo":"2:00"},{"nome_titulo":"Música 2","nome_subtitulo":"Título","indicador_tempo":"4:10"},{"nome_titulo":"Trémbo","nome_subtitulo":"Caractere Inválido™","indicador_tempo":"4:10"}]
ActivityPrivate.java
//Se alguém souber também consertar o AsyncTask irei ficar grato, pois ele não está carregando o ProgressDialog até a lista ser exibida, mas isso é de menos, estou me importando mais com os caracteres
new Musics().execute();
private class Musics extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
// Showing progress dialog
pDialog = new ProgressDialog(Menu.this);
pDialog.setMessage("Please wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected Void doInBackground(Void... arg0) {
final RequestQueue queue = Volley.newRequestQueue(Menu.this);
final String url = "http://urlsiteaqui.com.br/musicas.php";
final StringRequest stringRequest = new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
Log.d(TAG, "Response " + response);
GsonBuilder builder = new GsonBuilder();
Gson mGson = builder.create();
List<ItemObject> posts = new ArrayList<ItemObject>();
posts = Arrays.asList(mGson.fromJson(response, ItemObject[].class));
adapter = new RecyclerAdapter(Menu.this, posts);
recyclerView.setAdapter(adapter);
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(Menu.this, "Ops, error", Toast.LENGTH_SHORT).show();
Log.d(TAG, "Error " + error.getMessage());
}
});
queue.add(stringRequest);
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
// Dismiss the progress dialog
if (pDialog.isShowing())
pDialog.dismiss();
}
}
}
RecyclerAdapter.java
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
/**
* Created by Drama on 05/08/2016.
*/
public class RecyclerAdapter extends RecyclerView.Adapter<RecyclerHolders> {
private List<ItemObject> itemList;
private Context context;
public RecyclerAdapter(Context context, List<ItemObject> itemList) {
this.itemList = itemList;
this.context = context;
}
@Override
public RecyclerHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.listar_item, null);
RecyclerHolders rcv = new RecyclerHolders(layoutView);
return rcv;
}
@Override
public void onBindViewHolder(RecyclerHolders holders, int position) {
holders.title.setText(itemList.get(position).getTitle());
holders.subtitle.setText(itemList.get(position).getSubtitle());
}
@Override
public int getItemCount() {
return this.itemList.size();
}
}
RecyclerHolders.java
import android.content.Intent;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import java.util.List;
/**
* Created by Drama on 05/08/2016.
*/
public class RecyclerHolders extends RecyclerView.ViewHolder implements View.OnClickListener {
public TextView title;
public TextView subtitle;
public RecyclerHolders(View itemView) {
super(itemView);
itemView.setOnClickListener(this);
title = (TextView)itemView.findViewById(R.id.titulo);
subtitle = (TextView)itemView.findViewById(R.id.subtitulo);
}
@Override
public void onClick(View view) {
//passando dados recebidos para uma nova atividade
String value = title.getText().toString();
String valueSub = subtitle.getText().toString();
Intent intent = new Intent(view.getContext(), Player.class);
intent.putExtra("title_music", value);
intent.putExtra("subtitle_music", valueSub);
view.getContext().startActivity(intent);
}
}
ItemObject.java
import com.google.gson.annotations.SerializedName;
/**
* Created by Drama on 06/08/2016.
*/
public class ItemObject {
@SerializedName("nome_titulo")
private String title;
@SerializedName("nome_subtitulo")
private String subtitulo;
public ItemObject(String title, String subtitulo, String tempo) {
this.title = title;
this.subtitulo = subtitulo;
}
public String getTitle() {
return title;
}
public String getSubtitle() {
return subtitulo;
}
}
Thank you very much, thank you in advance!