title: POST request from android to Nodejs
body: <p>im working on this program ,which i have an Android app that uses Nodejs server.the host is -<a href="https://rocky-thicket-82184.herokuapp.com" rel="nofollow noreferrer">https://rocky-thicket-82184.herokuapp.com</a>.
  now, im trying to just send and recieve a  post request . but it seems to not
   work.
  in the server side i get an error like : cannot GET/.
  and in the client side i get :error exeption <a href="https://rocky-thicket-82184.herokuapp.com" rel="nofollow noreferrer">https://rocky-thicket-82184.herokuapp.com</a>.</p>
  
  <p>I cannot find the problem ,i feel like i'm missing something ...</p>
  
  <p>here is my Android mainActivity.java:</p>
  
  <pre><code>package com.example.shanijoffe.hungry_monkey;
  
  import android.support.v7.app.AppCompatActivity;
  import android.os.Bundle;
  import android.util.JsonReader;
  import android.util.Log;
  import android.view.View;
  import android.widget.Button;
  import android.widget.EditText;
  import android.os.AsyncTask;
  import android.widget.Toast;
  
  import com.loopj.android.http.AsyncHttpClient;
  import com.loopj.android.http.AsyncHttpResponseHandler;
  import com.stormpath.sdk.Stormpath;
  import com.stormpath.sdk.StormpathConfiguration;
  
  import org.json.JSONArray;
  import org.json.JSONException;
  import org.json.JSONObject;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.io.UnsupportedEncodingException;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLEncoder;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.List;
  
  import javax.net.ssl.HttpsURLConnection;
  
  import cz.msebera.android.httpclient.Header;
  import cz.msebera.android.httpclient.HttpResponse;
  import cz.msebera.android.httpclient.NameValuePair;
  import cz.msebera.android.httpclient.client.ClientProtocolException;
  import cz.msebera.android.httpclient.client.HttpClient;
  import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
  import cz.msebera.android.httpclient.client.methods.HttpPost;
  import cz.msebera.android.httpclient.impl.client.DefaultHttpClient;
  import cz.msebera.android.httpclient.message.BasicNameValuePair;
  
  import static com.loopj.android.http.AsyncHttpClient.log;
  
  public class MainActivity extends AppCompatActivity
  {
  
      EditText user_name_edit;
      EditText password_edit;
      Button loginButton;
      String user;
      String user_pass;
      private static final String TAG = "MyActivity";
  
      @Override
      protected void onCreate(Bundle savedInstanceState)
      {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          user_name_edit=(EditText)findViewById(R.id.txt_usrname);
          password_edit=(EditText)findViewById(R.id.txt_password);
          loginButton=(Button)findViewById(R.id.btn_login);
  
          Log.i(TAG,"before connection");
  
      }
  
  
  
  
      public void OnClick(final View view) {
          view.setEnabled(false);
          user=user_name_edit.getText().toString();
          user_pass=password_edit.getText().toString();
          new SendPostRequest().execute();
  //
  //
  
  
      }
  
  
  
  
      public class SendPostRequest extends AsyncTask&lt;String, Void, String&gt;
      {
  
          String user_name=user_name_edit.getText().toString();
          String user_pass=password_edit.getText().toString();
          protected void onPreExecute(){}
  
          protected String doInBackground(String... arg0) {
  
              try {
                  Log.e("MainActivity", "in SendPostRequest  ");
                  URL url = new URL("https://rocky-thicket-82184.herokuapp.com");
  
                  InputStream inp = (InputStream) url.getContent();
                  Log.e("stream",inp.toString());
                  JSONObject postDataParams = new JSONObject();
                  postDataParams.put("username", user);
                  postDataParams.put("password", user_pass);
                  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();
                  int responseCode;
                  try (BufferedWriter writer = new BufferedWriter(
                          new OutputStreamWriter(os, "UTF-8"))) {
                      String encoded_string = getPostDataString(postDataParams);
                      Log.e("encoded", encoded_string);
                      //writer.write(getPostDataString(postDataParams));
                      writer.write(postDataParams.toString());
                      writer.flush();
                      writer.close();
                  }
                  os.close();
  
                  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();
                      Log.i("MainActivty",sb.toString());
                      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>and here is the nodejs code:</p>
  
  <pre><code>var express=require('express');
  var bodyParser=require('body-parser');
  var app=express();
  var urlencodedParser = bodyParser.urlencoded({ extended: false });
  app.get('/',function(req,res){
      res.sendFile(__dirname + '/index.html');
  });
  app.post('/',function(req,res){
      console.log(req.body);
      if (!req.body) 
          return res.sendStatus(400)
      res.send('welcome, ' + req.body.user)
  });
  
  app.listen(process.env.PORT || 3000)
  </code></pre>
  
  <p>can anyone take a look and see if they can help ?
  Thanks alot :)</p>