Send phrase to web service using JSON

2

I have a small problem when sending a phrase using JSON to a web service, if I send a string without spaces like String name="Jose" ;, there is no problem, but when trying to send a whole sentence, the consultation is not done. What would be the way to send a phrase with spaces like String phrase="The house of the mountain";

String frase="La casa de martin es roja";

String url="http://sapito.org/Archivos/File/registrys2.php?frase="+frase.trim();


jsonRequest=new JsonObjectRequest(Request.Method.GET,url,null,this,this);
requestQueue.add(jsonRequest);

I receive the data in a PHP file.

Thanks in advance and regards.

    
asked by Fulano 09.12.2018 в 22:48
source

2 answers

2

Before sending the phrase, you must replace the blank spaces with %20

String frase="La casa de martin es roja";
String frase2 = frase.replace(" ","%20");
String url="http://sapito.org/Archivos/File/registrys2.php?frase="+frase2;
    
answered by 10.12.2018 / 00:33
source
2

I answered some time ago a case similar to yours, raised in this question: Problem receiving data on Android with JSON, PHP and MySQL

Since there the OP seems to have abandoned the question, I almost put that answer here, because it explains the source of the problem by citing the source, then giving some solutions.

The origin of the problem

In the case you raise, the URLs are being encoded according to the RFC 3986 , which explains in section 2.1 that URLs containing non-allowed characters will be replaced by the% sign % followed by its binary octet . The section puts just as an example the blank, whose binary octet would be 20 , so a URL containing blank spaces, these spaces are replaced by %20 .

Therefore, in requests whose URL is created like this in your application:

http://sapito.org/Archivos/File/registrys2.php?frase=La casa de martin es roja

If RFC 3986 is applied, the server will receive the URL like this:

http://sapito.org/Archivos/File/registrys2.php?frase=La%20casa%20de%20martin%20es%20roja

Solutions

Several solutions are possible, either on the application side or on the server side.

A. On the application side

You can replace blank spaces by the + sign, doing something like this:

String frase="La casa de martin es roja";
frase = frase.replaceAll(" ", "+");

This will produce a URL like that, which is totally valid for the case at hand:

http://sapito.org/Archivos/File/registrys2.php?frase=La+casa+de+martin+es+roja

B. On the server side

You can let the application send the encoded URL according to the RFC 3986 rule and work it on the server.

$urlPost=( empty($_GET["frase"]) ) ? NULL : urldecode($_GET["frase"]);

if ($urlPost){
    //Trabajar con la URL
}else{
    //Indicar el error en forma de array
}
    //Emitir la respuesta 

You can also make a replacement instead of using urldecode . But, it is not worth giving up a function that exists precisely for that. Note that on the application side it would not make any sense to decode the URL, because it is encoded precisely when it is sent to the server.

    
answered by 10.12.2018 в 02:12