title: Android POST request with JSON
body: <p>After researches i still cant send a JSON POST request to a server.</p>
  
  <p>I already tried some older answers:</p>
  
  <p><a href="https://stackoverflow.com/questions/4205980/java-sending-http-parameters-via-post-method-easily">Java - sending HTTP parameters via POST method easily</a></p>
  
  <p><a href="https://stackoverflow.com/questions/40238360/android-post-json-with-httpurlconnection">[Android]-POST Json with HttpUrlConnection</a></p>
  
  <p><a href="https://stackoverflow.com/questions/38013669/post-request-for-registering-user-data-on-server-by-httpurlconnection">Post request for registering user data on server by HttpUrlConnection</a></p>
  
  <p><a href="https://stackoverflow.com/questions/36833798/sending-json-object-via-http-post-method-in-android">Sending json object via http post method in android</a></p>
  
  <p><a href="https://stackoverflow.com/questions/3027066/how-to-send-a-json-object-over-request-with-android?rq=1">How to send a JSON object over Request with Android?</a></p>
  
  <p>My current code is:</p>
  
  <pre><code>FloatingActionButton btn_sendMsg = (FloatingActionButton) findViewById(R.id.btn_sendMsg);
      btn_sendMsg.setOnClickListener(new View.OnClickListener() {
          @Override
          public void onClick(View view) {
              /*Snackbar.make(view, "Sendevorgang...", Snackbar.LENGTH_LONG)
                      .setAction("Action", null).show();*/
  
              createMsg();
          }
      });
  
  private void createMsg() {
      Message message = new Message(txtbox_msg.getText().toString(), "testUser");
  
          AsyncT asyncT = new AsyncT();
          asyncT.execute(message);
  
  }
  </code></pre>
  
  <p>AsyncT.java :</p>
  
  <pre><code>@Override
  protected Message doInBackground(Message... params) {
  
      try {
          URL url = new URL("http://[ip]:[port]"); //in the real code, there is an ip and a port
          HttpURLConnection conn = (HttpURLConnection) url.openConnection();
          conn.setRequestMethod("POST");
          conn.setRequestProperty("Content-Type", "application/json");
          conn.setRequestProperty("Accept","application/json");
          conn.setDoOutput(true);
          conn.setDoInput(true);
          conn.connect();
  
          JSONObject jsonParam = new JSONObject();
          jsonParam.put("uname", params[0].getUser());
          jsonParam.put("message", params[0].getMessage());
          jsonParam.put("latitude", "0");
          jsonParam.put("longitude", "0");
          jsonParam.put("id", "1");
  
  
          DataOutputStream os = new DataOutputStream(conn.getOutputStream());
          os.writeBytes(URLEncoder.encode(jsonParam.toString(), "UTF-8"));
  
          os.flush();
          os.close();
  
          Log.i("STATUS", String.valueOf(conn.getResponseCode()));
          Log.i("MSG" , conn.getResponseMessage());
  
          conn.disconnect();
      } catch (Exception e) {
  
      }
  
  
      return null;
  }
  </code></pre>
  
  <p>I'm getting the error code networkonmainthreadexception 500</p>
  
  <p>How can i solve this?</p>