Problem with spaces in Android

1

I have a form in android which communicates with a WebService to fill a database of MySql , the problem is that when I run the app and write in the editText for example the name, if I write two words the data does not reach MySQL, but if I fill the editText with only one word without leaving spaces then if they reach MySql

This is my Java code:

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.Toast;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;

import org.json.JSONArray;
import org.json.JSONException;

public class FormNegocio extends AppCompatActivity {
    EditText nombren, telefonon, direccionnR, direccionnE, correon, observacionesn;
    RadioButton tespecial, carga, encomiendan;
    Button confirmarnform, regresarnform;
    ListView listaResultado;
    String tipon;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_form_negocio);

        nombren = (EditText)findViewById(R.id.nombren);
        telefonon = (EditText)findViewById(R.id.telefonon);
        direccionnR = (EditText)findViewById(R.id.direccionnR);
        direccionnE = (EditText)findViewById(R.id.direccionnE);
        correon = (EditText)findViewById(R.id.correon);
        observacionesn = (EditText)findViewById(R.id.observacionesn);

        tespecial = (RadioButton)findViewById(R.id.tespecial);
        carga = (RadioButton)findViewById(R.id.carga);
        encomiendan = (RadioButton)findViewById(R.id.encomiendan);

        confirmarnform = (Button)findViewById(R.id.confirmarNform);
        regresarnform = (Button)findViewById(R.id.regresarNform);

        confirmarnform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                if (tespecial.isChecked()){
                    tipon = "T. Especial";
                }else if (carga.isChecked()){
                    tipon = "carga";
                }else if (encomiendan.isChecked()){
                    tipon = "encomienda";
                }

               final String nombreform = nombren.getText().toString().trim();

                String registro = "http://miurl.com/micarpeta/registrarn.php?idusuarion=NULL&nombren="+nombreform+"&telefonon="+telefonon.getText()+"&direccionnR="+direccionnR.getText()+"&direccionnE="+direccionnE.getText()+"&correon="+correon.getText()+"&tipon="+tipon+"&observacionesn="+observacionesn.getText();

                EnviarRecibirDatos(registro);
                Intent confirmarnform = new Intent(FormNegocio.this, Fin.class);
                startActivity(confirmarnform);
            }
        });


        regresarnform.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent regresarnform = new Intent(FormNegocio.this, Inicio.class);
                startActivity(regresarnform);
            }
        });
    }

    public void EnviarRecibirDatos(String URL){

        Toast.makeText(getApplicationContext(), "Tenemos tu solicitud", Toast.LENGTH_SHORT).show();


        RequestQueue queue = Volley.newRequestQueue(this);
        StringRequest stringRequest = new StringRequest(Request.Method.GET, URL, new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {


                if (response.length()>0){
                    try {
                        JSONArray ja = new JSONArray(response);
                        Log.i("sizejson",""+ja.length());

                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                }

            }
        }, new Response.ErrorListener(){
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });

        queue.add(stringRequest);

    }



}
    
asked by Fredy Nestor Ruiz 24.08.2017 в 03:13
source

2 answers

1

That happened to me, I explain:

The problem comes when you pass the parameters by GET to PHP, if you go by example:

mipagina.com/script.php?nombre=pepe

I accept it, but ...

mipagina.com/script.php?nombre=pepe garcía

I do not accept it to have that space, the solution I gave was to format those spaces in android before encapsulating them to send them.

I replaced all the "", for "% 20", the% 20 replaces the spaces in a text string of a URL, from that moment the app started to save me the text strings with spaces in the mysql. Remaining the URL that you send like this:

mipagina.com/script.php?nombre=pepe%20garcía

Create a function to which you pass a string of text, and make a return with the spaces replaced.

    
answered by 24.08.2017 / 09:24
source
3

The problem you are having when forming the url with the following code:

String registro = "http://miurl.com/micarpeta/registrarn.php?idusuarion=NULL&nombren="+nombreform+"&telefonon="+telefonon.getText()+"&direccionnR="+direccionnR.getText()+"&direccionnE="+direccionnE.getText()+"&correon="+correon.getText()+"&tipon="+tipon+"&observacionesn="+observacionesn.getText();

since urls do not allow blank spaces, so you can take a look at the documentation of URLEncoder, leaving your code as follows:

String nombre = URLEncoder.encode(nombreform, "UTF-8");
String telefono = URLEncoder.encode(telefonon.getText(), "UTF-8");
String direccionR = URLEncoder.encode(direccionnR.getText(), "UTF-8");
String direccionE = URLEncoder.encode(direccionnE.getText(), "UTF-8");
String correo = URLEncoder.encode(correon.getText(), "UTF-8");
String tipo = URLEncoder.encode(tipon, "UTF-8");
String observaciones = URLEncoder.encode(observacionesn.getText(), "UTF-8");

String registro = "http://miurl.com/micarpeta/registrarn.php?idusuarion=NULL&nombren="+nombre+"&telefonon="+telefono+"&direccionnR="+direccionR+"&direccionnE="+direccionE+"&correon="+correo+"&tipon="+tipo+"&observacionesn="+observaciones;

Note : You only need to encode the value of each parameter, not the entire url.

Note 2 : Note that the spaces in the query parameters are represented by '+', not by '% 20'. The '% 20' is usually used to represent spaces in URIs (the part before the string separator character of the URI query '?'), Not in the query string (the part after the '?').

    
answered by 24.08.2017 в 09:19