0

I have one activity with a listview with some towns of Málaga (Spain). I fill the list with a MySQL query using PHP and JSON. The towns have special character (like Málaga). When I click on one option of the list I call other activity which do a query to a database. I pass in the url the value of the selected item in the list for filter the query. The problem is when I execute the query. If I select one town without special characters all it's right but if I select one with special character I have the error java.lang.IllegalArgumentException: Illegal character in query. I try to pass the variable in my web browser and it works. Why have I the error?

This is the code of the second activity

public class Partidos2 extends Activity {
private String result=null;
private InputStream is=null;
private StringBuilder sb=null; 
private ProgressDialog pDialog;
Bundle bundle;
String localidad;
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_partidos2);
    bundle=this.getIntent().getExtras();
    new async().execute();

}
class async extends AsyncTask<String, String, String>{
    protected void onPreExecute(){
        pDialog=new ProgressDialog(Partidos2.this);
        pDialog.setMessage("Cargando...");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
    }
    protected String doInBackground(String...params){
        try{
            HttpClient httpclient=new DefaultHttpClient();
            HttpPost httppost=new HttpPost("http://proyectoabel.tk/android/partidos.php?localidad="+bundle.getString("localidad"));
            HttpResponse response=httpclient.execute(httppost);
            HttpEntity entity=response.getEntity();
            is=entity.getContent();
        } catch(Exception e){
            Log.e("Conexion: ",e.toString());
            Toast.makeText(getApplicationContext(), "Conexión fallida", Toast.LENGTH_SHORT).show();
        }
        try{
            BufferedReader reader=new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
            sb=new StringBuilder();
            sb.append(reader.readLine()+"\n");
            String line="0";
            while((line=reader.readLine())!=null){
                sb.append(line+"\n");
            }
            is.close();
            result=sb.toString();
            return result;
        } catch(Exception e){
            Log.e("Error: ", e.toString());
            return result;
        }
    }
    protected void onPostExecute(String result){
        try{
            JSONArray jArray=new JSONArray(result);
            TableLayout tv=(TableLayout)findViewById(R.id.maintable);
            tv.removeAllViewsInLayout();
            int contador=1;
            for(int i=-1;i<jArray.length()-1;i++){
                TableRow tr=new TableRow(Partidos2.this);
                tr.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT));
                if(contador==1){
                    TextView ea=new TextView(Partidos2.this);
                    ea.setText("Local");
                    ea.setGravity(Gravity.CENTER);
                    ea.setTextColor(Color.WHITE);
                    ea.setTypeface(null, Typeface.BOLD);
                    tr.addView(ea);
                    TextView eb=new TextView(Partidos2.this);
                    eb.setText("Visitante");
                    eb.setGravity(Gravity.CENTER);
                    eb.setTextColor(Color.WHITE);
                    eb.setTypeface(null, Typeface.BOLD);
                    tr.addView(eb);
                    TextView fe=new TextView(Partidos2.this);
                    fe.setText("Fecha");
                    fe.setGravity(Gravity.CENTER);
                    fe.setTextColor(Color.WHITE);
                    fe.setTypeface(null, Typeface.BOLD);
                    tr.addView(fe);
                    TextView ho=new TextView(Partidos2.this);
                    ho.setText("Hora");
                    ho.setGravity(Gravity.CENTER);
                    ho.setTextColor(Color.WHITE);
                    ho.setTypeface(null, Typeface.BOLD);
                    tr.addView(ho);
                    TextView ca=new TextView(Partidos2.this);
                    ca.setText("Cat.");
                    ca.setGravity(Gravity.CENTER);
                    ca.setTextColor(Color.WHITE);
                    ca.setTypeface(null, Typeface.BOLD);
                    tr.addView(ca);
                    TextView pi=new TextView(Partidos2.this);
                    pi.setText("Pista");
                    pi.setGravity(Gravity.CENTER);
                    pi.setTextColor(Color.WHITE);
                    pi.setTypeface(null, Typeface.BOLD);
                    tr.addView(pi);
                    tv.addView(tr);
                    final View vline=new View(Partidos2.this);
                    vline.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 2));
                    vline.setBackgroundColor(Color.WHITE);
                    tv.addView(vline);
                    contador=0;
                }
                else{
                    JSONObject json_data=jArray.getJSONObject(i);
                    TextView b=new TextView(Partidos2.this);
                    String stime=String.valueOf(json_data.getString("EquipoA"));
                    b.setText(stime);
                    b.setGravity(Gravity.CENTER);
                    b.setTextColor(Color.WHITE);
                    b.setPadding(0, 0, 10, 0);
                    tr.addView(b);
                    TextView b1=new TextView(Partidos2.this);
                    String stime1=String.valueOf(json_data.getString("EquipoB"));
                    b1.setText(stime1);
                    b1.setGravity(Gravity.CENTER);
                    b1.setTextColor(Color.WHITE);
                    b1.setPadding(0, 0, 10, 0);
                    tr.addView(b1);
                    TextView b2=new TextView(Partidos2.this);
                    String stime2=String.valueOf(json_data.getString("Fecha"));
                    b2.setText(stime2);
                    b2.setGravity(Gravity.CENTER);
                    b2.setTextColor(Color.WHITE);
                    b2.setPadding(0, 0, 10, 0);
                    tr.addView(b2);
                    TextView b3=new TextView(Partidos2.this);
                    String stime3=String.valueOf(json_data.getString("Hora"));
                    b3.setText(stime3);
                    b3.setGravity(Gravity.CENTER);
                    b3.setTextColor(Color.WHITE);
                    b3.setPadding(0, 0, 10, 0);
                    tr.addView(b3);
                    TextView b4=new TextView(Partidos2.this);
                    String stime4=String.valueOf(json_data.getString("Abreviatura"));
                    b4.setText(stime4);
                    b4.setGravity(Gravity.CENTER);
                    b4.setTextColor(Color.WHITE);
                    b4.setPadding(0, 0, 10, 0);
                    tr.addView(b4);
                    TextView b5=new TextView(Partidos2.this);
                    String stime5=String.valueOf(json_data.getString("Pista"));
                    b5.setText(stime5);
                    b5.setGravity(Gravity.CENTER);
                    b5.setTextColor(Color.WHITE);
                    tr.addView(b5);
                    tv.addView(tr);
                    final View vline1=new View(Partidos2.this);
                    vline1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, 1));
                    vline1.setBackgroundColor(Color.WHITE);
                    tv.addView(vline1);
                    pDialog.dismiss();
                }
            }
        } catch(JSONException e){
            Log.e("Error: ", e.toString());
        }
    }
}
}

And this is the code of the PHP script.

$localidad=$_GET["localidad"];
include"../conexion.php";
$mysqli=new mysqli($servidor, $usuario, $clave, $basedatos);    
$mysqli->set_charset("latin1");
if($mysqli->connect_error){
    echo "Error nยบ ".$conexion->connect_errno.": ".$conexion->connect_error;
}
if($result=$mysqli->query("SELECT E1.Nombre EquipoA, E2.Nombre EquipoB, DATE_FORMAT(Fecha, '%d/%m/%Y') Fecha, TIME_FORMAT(Hora, '%H:%i') Hora, Abreviatura, PA.Nombre Pista
FROM PARTIDO P INNER JOIN EQUIPO E1 ON P.EquipoA=E1.Codigo_equipo 
INNER JOIN EQUIPO E2 ON P.EquipoB=E2.Codigo_equipo 
INNER JOIN PABELLON PA ON P.Codigo_pabellon=PA.Codigo_pabellon 
INNER JOIN CATEGORIA ON P.Codigo_categoria=CATEGORIA.Codigo_categoria
INNER JOIN LOCALIDAD ON PA.Codigo_localidad=LOCALIDAD.Codigo_localidad
WHERE Jornada=(SELECT valor FROM CONFIGURACION WHERE descripcion='jornada') AND LOCALIDAD.Nombre='$localidad' ORDER BY Fecha, Hora")){
    while($row=$result->fetch_array()){
        $resultado[]=$row;
    }
        print(json_encode($resultado));
}
else{
    print(json_encode($mysqli->error));
}
mysqli_close($mysqli);  
7
  • 1
    Try this String localidad= bundle.getString("localidad"); URLEncoder.encode(localidad, "UTF-8") Hope it helps. Commented May 13, 2014 at 7:26
  • stackoverflow.com/questions/18910187/… Commented May 13, 2014 at 7:27
  • I use it but I have the same error Commented May 13, 2014 at 7:42
  • I follow the link you send me and now the error is other org.JSON.JSONException: Value AlhaurÃ-n of type java.lang.String cannot be converted to JSONArray (The value I pass is Alhaurín) Commented May 13, 2014 at 7:50
  • 1
    Change the below code to php script $localidad=urldecode($_GET["localidad"]); Commented May 13, 2014 at 8:02

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.