title: response code 500 Android POST method
body: <p>I have read many posts with the same problem and tried to apply their solutions, but no one worked for me. I am pretty new to Java and Android. The url is "https", this is not an error. My java code is the following:</p>
  
  <pre><code>public class Main3Activity extends AppCompatActivity {
  
      Button b1;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main3);        
  
          b1 = (Button) findViewById(R.id.button_pago);
          b1.setOnClickListener(new View.OnClickListener()   {
              public void onClick(View v)  {
                  try {
                      new SendPostRequest().execute();
                  } catch (Exception e) {
                      // TODO Auto-generated catch block
                      e.printStackTrace();
                  }
              }
          });
  
      }
  
  
  
  
      public class SendPostRequest extends AsyncTask&lt;String, Void, String&gt; {
  
          protected void onPreExecute(){}
  
          protected String doInBackground(String... arg0) {
  
              try {
  
                  URL url = new URL("https://www.myweb.com/login_app.php"); 
  
                  JSONObject postDataParams = new JSONObject();
                  postDataParams.put("precio", "5000");
                  postDataParams.put("texto", "f...h");
                  Log.e("params",postDataParams.toString());
  
                  HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                  conn.setReadTimeout(15000 /* milliseconds */);
                  conn.setConnectTimeout(15000 /* milliseconds */);
                  conn.setRequestMethod("POST");
                  conn.setDoInput(true);
                  conn.setDoOutput(true);
  
                  OutputStream os = conn.getOutputStream();
                  BufferedWriter writer = new BufferedWriter(
                          new OutputStreamWriter(os, "UTF-8"));
                  writer.write(getPostDataString(postDataParams));
  
                  writer.flush();
                  writer.close();
                  os.close();
  
                  int responseCode=conn.getResponseCode();
  
                  if (responseCode == HttpsURLConnection.HTTP_OK) {
  
                      BufferedReader in=new BufferedReader(new
                              InputStreamReader(
                              conn.getInputStream()));
  
                      StringBuffer sb = new StringBuffer("");
                      String line="";
  
                      while((line = in.readLine()) != null) {
  
                          sb.append(line);
                          break;
                      }
  
                      in.close();
                      return sb.toString();
  
                  }
                  else {
                      return new String("false : "+responseCode);
                  }
              }
              catch(Exception e){
                  return new String("Exception: " + e.getMessage());
              }
  
          }
  
          @Override
          protected void onPostExecute(String result) {
              Toast.makeText(getApplicationContext(), result,
                      Toast.LENGTH_LONG).show();
          }
      }
  
      public String getPostDataString(JSONObject params) throws Exception {
  
          StringBuilder result = new StringBuilder();
          boolean first = true;
  
          Iterator&lt;String&gt; itr = params.keys();
  
          while(itr.hasNext()){
  
              String key= itr.next();
              Object value = params.get(key);
  
              if (first)
                  first = false;
              else
                  result.append("&amp;");
  
              result.append(URLEncoder.encode(key, "UTF-8"));
              result.append("=");
              result.append(URLEncoder.encode(value.toString(), "UTF-8"));
  
          }
          return result.toString();
      }
  }
  </code></pre>
  
  <p>Thank you very much for your time.</p>
  
  <p>Checking the log file, I found many messages like the following:</p>
  
  <pre><code>81.184.8.77 - - [23/Aug/2016:12:54:05 +0200] "POST /login_app.php HTTP/1.1" 500 379 "-" "Dalvik/1.6.0 (Linux; U; Android 4.4.2; Android SDK built for x86 Build/KK)" "-"
  81.184.8.77 - - [23/Aug/2016:12:59:37 +0200] "POST /login_app.php HTTP/1.1" 500 10793 "-" "Dalvik/2.1.0 (Linux; U; Android 5.1.1; ONE E1001 Build/LMY47V)" "-"
  81.184.8.77 - - [23/Aug/2016:13:01:05 +0200] "POST /login_app.php HTTP/1.1" 500 10807 "-" "Dalvik/1.6.0 (Linux; U; Android 4.4.2; Android SDK built for x86 Build/KK)" "-"
  81.184.8.77 - - [23/Aug/2016:13:01:07 +0200] "POST /login_app.php HTTP/1.1" 500 379 "-" "Dalvik/1.6.0 (Linux; U; Android 4.4.2; Android SDK built for x86 Build/KK)" "-"
  </code></pre>