title: Getting POST request from android app by Java servlet
body: <p>I am working on a project to communicate an Android application with a Java servlet. In this android application, the string entered in editText view should be sent to Java servlet as a request and Java servlet should replace this string with another string as a response.</p>
  
  <p>Here is my code for Android app:</p>
  
  <pre><code>public class DoubleMeActivity extends Activity implements OnClickListener {
  
  static EditText inputValue=null;
  Integer doubledValue =0;
  Button doubleMe;
  TextView tv;
  
  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
  
      setContentView(R.layout.calculate);
  
      inputValue = (EditText) findViewById(R.id.inputNum);
      doubleMe = (Button) findViewById(R.id.doubleme);
      tv = (TextView) findViewById(R.id.textView2);
      doubleMe.setOnClickListener(this);
  
   // check if you are connected or not
      if(isConnected()){
          tv.setBackgroundColor(0xFF00CC00);
          tv.setText("You are conncted");
      }
      else{
          tv.setText("You are NOT conncted");
      }
  
  }
  
  
  
  @Override
  public void onClick(final View v) {
  
      switch (v.getId()){
      case R.id.doubleme:
          if(!validate())
              Toast.makeText(getBaseContext(), "Enter some data!", Toast.LENGTH_LONG).show();
          else
  
          new HttpAsyncTask().execute("http://139.179.92.123:8080/SmartStoreServlet/FemtoStore");
  
  
  
  
  
  
          break;
          }
  }
  
  
  
  private class HttpAsyncTask extends AsyncTask&lt;String, Void, String&gt; {
      @Override
      protected String doInBackground(String... urls) {
  
  
  
          return POST(urls[0]);
      }
      // onPostExecute displays the results of the AsyncTask.
      @Override
      protected void onPostExecute(String result) {
          Toast.makeText(getBaseContext(), "Data Sent!", Toast.LENGTH_LONG).show();
     }
  }
  
  public static String POST(String url){
      InputStream inputStream = null;
      String result = "";
      try {
  
          // 1. create HttpClient
          HttpClient httpclient = new DefaultHttpClient();
  
          // 2. make POST request to the given URL
          HttpPost httpPost = new HttpPost(url);
  
          JSONObject jsonObject = new JSONObject();
          jsonObject.accumulate("string" , inputValue.getText().toString());
  
          String json = jsonObject.toString();
  
          System.out.println(json);
          StringEntity se = new StringEntity(json);
  
          httpPost.setEntity(se);
  
          // 7. Set some headers to inform server about the type of the content   
          httpPost.setHeader("Accept", "application/json");
          httpPost.setHeader("Content-type", "application/json");
  
          // 8. Execute POST request to the given URL
          HttpResponse httpResponse = httpclient.execute(httpPost);
          System.out.println("ajsajsd" + inputStream); --&gt; is not executed
          // 9. receive response as inputStream
          inputStream = httpResponse.getEntity().getContent();
  
          // 10. convert inputstream to string
  
          if(inputStream != null)
          {
              result = convertInputStreamToString(inputStream);
              System.out.println("jndkjsn" + result);
          }
          else
              result = "Did not work!";
  
      } catch (Exception e) {
          Log.d("InputStream", e.getLocalizedMessage());
      }
  
      // 11. return result
      return result;
  }
  
  
  
  private boolean validate(){
      if(inputValue.getText().toString().trim().equals("")) //DO CHECK
          return false;
      else
          return true;    
  }
  
  public boolean isConnected(){
      ConnectivityManager connMgr = (ConnectivityManager) getSystemService(Activity.CONNECTIVITY_SERVICE);
          NetworkInfo networkInfo = connMgr.getActiveNetworkInfo();
          if (networkInfo != null &amp;&amp; networkInfo.isConnected()) 
              return true;
          else
              return false;    
  }
  
  private static String convertInputStreamToString(InputStream inputStream) throws IOException{
      BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
      String line = "";
      String result = "";
      while((line = bufferedReader.readLine()) != null)
          result += line;
  
      inputStream.close();
      return result;
  
  }   
  
  }
  </code></pre>
  
  <p>and my code for java servlet:</p>
  
  <pre><code>public class FemtoStore extends HttpServlet {
  private static final long serialVersionUID = 1L;
  
  /**
   * @see HttpServlet#HttpServlet()
   */
  public FemtoStore() {
      super();
      // TODO Auto-generated constructor stub
  }
  
  /**
   * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      doPost(request, response);
  }
  
  /**
   * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
   */
  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
      // TODO Auto-generated method stub
      System.out.println(request.getRemoteAddr());
  
      BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
      String json = "";
      if(br != null){
          json = br.readLine();
      }
  
      System.out.println(json);
  }
  
  }
  </code></pre>
  
  <p>The problem is I am getting connection refused error on my android app side and I am working with a real device. If you help me I will be very thankful.</p>