For sending requests to the server used httpurlconnection . I sent a post to a certain address like this: act=GETCOMMENTS&connectid=... etc. And everything worked perfectly, the answer came from the server and I parsil it. But the problem arose when I had to send some text through this request. Namely, sending a comment, where the comment itself is written in one of the request keys. &commenttext=test%20test2 this: &commenttext=test%20test2 . It had to come to the server in this form, and the server itself replaces %20 with a space. But it is test test2 that comes to the server with a real space, not %20 , and there the real spaces are cut off.
Here is the code for the method that performs all the requests:

 private String serverRequester2 (String reqParam){ try { URL url = new URL("http:/...определённыйСайт.../test.php"); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("USER-AGENT", "Mozilla/5.0"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestProperty("Accept-Charset", "UTF-8"); connection.setDoOutput(true); DataOutputStream dStream = new DataOutputStream(connection.getOutputStream()); dStream.writeBytes(reqParam); Log.d("REQ_PARAM",reqParam); dStream.flush(); dStream.close(); BufferedReader br = new BufferedReader(new InputStreamReader(connection.getInputStream())); String line = ""; StringBuilder responseOutput = new StringBuilder(); while ((line=br.readLine())!=null){ Log.d("LINE",line); responseOutput.append(line); } br.close(); return responseOutput.toString(); } catch (MalformedURLException e) { e.printStackTrace(); return null; } catch (IOException e) { e.printStackTrace(); return null; } } 

And this is how I call it and pass it the parameters that need to be sent to the server:

 return serverRequester2("act=CREATECOMMENT&connectid="+connectid+"&token="+token+"&clientcode=123123123&postid="+postID+"&commenttext=test%20test2"); 

What am I doing wrong? Why does the real space go instead of %20 ? By the way, the + sign also replaces the space. And I need the server to come exactly test%20test2 or test+test2 , and not test test2 .

PS At first there was a thought that the whole thing was in the server, but no, from another application written by another person, the request comes to Phonegap from the URL encoding, and not from the real space.

    2 answers 2

    To send the text test%20test2 you need to pass test%2520test2 , and for test+test2 you need test%2btest2 .

    More details:

    • Thanks, it worked! - cheerful_weasel

    Everything should work this way. %20 is the space encoded for transmission in the request parameter.
    If you want the server to get %20 and not a space, transfer %2520 , i.e. test%2520test2 in your example.

    ADDON
    Perhaps if you remove it from the header

     connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); 

    the server will stop decoding the parameters. Because it is he who decodes %20 into a space, since You clearly tell him that you are sending encoded data.

    • Thanks, it worked! - cheerful_weasel