I have an Android application that performs queries with a database hosted on a host with phpmyadmin, and when I try to insert text that contains accents or some special character does not insert it into the database. Both the app and the database I have with the utf8 and utf8_general_ci collation. I understand that you have to enter an instruction in the php file, but I have tried several things and none has worked. On the other hand, if I enter a word with an accent from the phpmyadmin console, it appears in the application.
<?php
$cod=$_REQUEST['cod'];
$tit=$_REQUEST['tit'];
$des=$_REQUEST['des'];
$cnx=new PDO("mysql:host=localhost;dbname;charset=utf8_general_ci","dbser","dbpass");
$res=$cnx->query("insert into noticias (id, titulo, descripcion) VALUES (0, '$tit', '$des')");
public class First_fragment extends Fragment {
ListView lstCursos;
String recuperado;
private ListView mylist;
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_tab, container, false);
//De este modo cambiamos el tema para cada uno de los layout
getActivity().setTheme(R.style.Barra);
Bundle recupera = getActivity().getIntent().getExtras ();
if (recupera != null) {
recuperado = recupera.getString ( "cod" );
}
Thread tr2 = new Thread () {
@Override
public void run() {
final String resultado = enviarGET ( recuperado );
getActivity().runOnUiThread ( new Runnable () {
@Override
public void run() {
////////////////////////////////////////
cargarListView ( ArregloLista ( resultado ) );
////////////////////////////////////////
// //////////////////////////////////////
}
} );
}
};
tr2.start ();
return v;
}
public String enviarGET(String id){
URL url = null;
String linea = "";
int respuesta = 0;
StringBuilder resul = null;
try {
//url = new URL ("http://192.168.1.41/WebService/llenarnoticias.php?id="+id);
url = new URL ("url");
//Ahora enviamos el dato
HttpURLConnection conection = (HttpURLConnection) url.openConnection ();
//Guardamos la respuesta en el entero, porque sera un uno o un cero
//Esto es lo que devuelve la BD una vez hacemos la consulta
respuesta = conection.getResponseCode ();
//Inicializamos resul
resul = new StringBuilder ();
if (respuesta == HttpURLConnection.HTTP_OK) {
InputStream in = new BufferedInputStream(conection.getInputStream());
BufferedReader reader = new BufferedReader (new InputStreamReader(in));
while ((linea = reader.readLine ()) != null){
resul.append (linea);
}
}
} catch (Exception e) {}
return resul.toString ();
}
//Metodo que permite crear un arraylista para llena el listview
public ArrayList<String> ArregloLista(String response){
ArrayList<String> listado = new ArrayList<String> ();
try {
JSONArray json = new JSONArray (response);
String texto = "";
String texto2 = "";
for(int i=0;i<json.length ();i++){
texto = json.getJSONObject(i).getString("titulo");
texto2 = json.getJSONObject(i).getString("descripcion");
listado.add("\n" + texto + "\n\n" +texto2 + "\n");
}
} catch (Exception e) {}
return listado;
}
//Aquí es donde va TODA LA CHICHA
public void cargarListView(ArrayList<String> datos){
ArrayAdapter<String> adaptador = new ArrayAdapter<String> (getActivity(), android.R.layout.simple_list_item_1, datos);
lstCursos = (ListView) getActivity().findViewById (R.id.listCursos);
lstCursos.setAdapter (adaptador);
}
}