title: Java how to send special character is post
body: <p>Im trying to send a post to a backend API from android and this is what I have but some special characters relating to application/x-www-form-urlencoded content type wont be sent or cause problems. Im wondering if there is a way to send the special characters in this format? (+,&amp;) I'm attempting to send the message &quot;This is a message + a test&quot; but my database is getting &quot;This is a message  a test&quot;</p>
  <p>I've tried passing JSON but the backend that my co-students wrote doesn't accept JSON and returns a 401 not authorized error.</p>
  <pre><code>public void sendMessage() {
          if(!mEdit.getText().toString().trim().matches(&quot;&quot;)) {                                                  //Cant send empty strings
              Thread thread = new Thread(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          URL url = new URL(Sendurl);                                                                 //Create URL
                          HttpURLConnection conn = (HttpURLConnection) url.openConnection();                          //Create a Connector
                          conn.setRequestMethod(&quot;POST&quot;);                                                              //Dictate the Method
                          conn.setRequestProperty(&quot;Content-Type&quot;, &quot;application/json; utf-8&quot;);                  //Some other properties
                          conn.setRequestProperty(&quot;Accept&quot;, &quot;application/json&quot;);
                          conn.setDoOutput(true);
                          conn.setDoInput(true);
  
                          //This part causes a 401 error because it is the equivalent of passing parameters but our backend uses body formatting
                          JSONObject jsonParam = new JSONObject();
                          Date date = new Date();
                          RequestParams params = new RequestParams();
  
  
                          params.put(&quot;username&quot;, &quot;admin&quot;);
                          params.put(&quot;sessionID&quot;, 1);
                          params.put(&quot;cardNum&quot;, 111111111);
                          params.put(&quot;sentByStaff&quot;, &quot;false&quot;);
                          params.put(&quot;message&quot;, &quot;This is the message + a test&quot;);
                          params.put(&quot;time&quot;, date.getTime()/1000);
  
                          Log.i(&quot;JSON&quot;,  params.toString());
                         // OutputStreamWriter os = new OutputStreamWriter(conn.getOutputStream());                         //Prepare the connection for output
  
                          POST_PARAMS =  params.toString();     //Converts post parameters to body type
  
                          System.out.println(POST_PARAMS);
                          try(OutputStream os = conn.getOutputStream()) {
                              byte[] input = POST_PARAMS.getBytes(&quot;utf-8&quot;);
                              os.write(input, 0, input.length);
                              os.flush();
                              os.close();
                          }
                          //os.write(POST_PARAMS);                                                           //Write our parameters to the post
                          //os.flush();                                                                                 //Do the thing
                          //os.close();
  
                          Log.i(&quot;STATUS&quot;, String.valueOf(conn.getResponseCode()));                               //Debug
                          Log.i(&quot;MSG&quot;, conn.getResponseMessage());                                               //Debug
  
                          int responseCode = 0;                                                                       //get response code
                          responseCode = conn.getResponseCode();
  
                          if (responseCode == HttpURLConnection.HTTP_OK) {                                            //On success
                                  mEdit.setText(&quot;&quot;);
                          }
                          conn.disconnect();                                                                          //disconnect after attempting post
                          getMessage();                                                                               //update our messages
                      } catch (Exception e) {                                                                         //dirty catch all
                          e.printStackTrace();
                      }
                  }
              });
              thread.start();
          }
      }
  </code></pre>