title: Android- org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
body: <p>I am trying to send a HTTP post request to a REST service through my android app and the client runs as an async task. Here is the client: </p>
  
  <pre><code>@Override
  protected Void doInBackground(Void... params) {
  
      String address = "http://xxx.xx.x.xxx:8080/rest/manageUser/create";
      StringBuilder stringBuilder = null;
      ArrayList&lt;NameValuePair&gt; postParameters;
      try {
          HttpPost httpPost = new HttpPost(address);
  
  
          postParameters = new ArrayList&lt;NameValuePair&gt;();
          postParameters.add(new BasicNameValuePair("userId", userId));
          postParameters.add(new BasicNameValuePair("firstName", firstName));
          postParameters.add(new BasicNameValuePair("lastName", lastName));
          postParameters.add(new BasicNameValuePair("mobileNumber",
                  mobileNumber));
          postParameters.add(new BasicNameValuePair("loginStatus",
                  loginStatus));
  
          httpPost.setEntity(new UrlEncodedFormEntity(postParameters));
  
          HttpClient client = new DefaultHttpClient();
          HttpResponse response;
          stringBuilder = new StringBuilder();
  
          response = client.execute(httpPost);
  
          HttpEntity entity = response.getEntity();
          InputStream stream = entity.getContent();
  
          int b;
          while ((b = stream.read()) != -1) {
              stringBuilder.append((char) b);
          }
  
          // System.out.println(stringBuilder);
      } catch (Exception e) {
          e.printStackTrace();
      }
  
      JSONObject jobj = null;
  
      try {
          jobj = new JSONObject(stringBuilder.toString());
          System.out.println(jobj.toString());
      } catch (Exception ex) {
  
          ex.printStackTrace();
      }
      return null;
  }
  </code></pre>
  
  <p>Also when I create the client as an standalone java class it works fine. But when I use it from my Android app as an async task as above, I get the following exception:</p>
  
  <pre><code>at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
  at com.example.hello.service.client.CreateUser.doInBackground(CreateUser.java:64)
  at com.example.hello.service.client.CreateUser.doInBackground(CreateUser.java:1)
  at android.os.AsyncTask$2.call(AsyncTask.java:288)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:841)
   Caused by: org.apache.http.ProtocolException: The server failed to respond with a valid HTTP response
  at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:93)
  at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
  at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
  at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
  at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
  at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
  at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:428)
  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
  ... 10 more
   org.json.JSONException: End of input at character 0 of 
  at org.json.JSONTokener.syntaxError(JSONTokener.java:450)
  at org.json.JSONTokener.nextValue(JSONTokener.java:97)
  at org.json.JSONObject.&lt;init&gt;(JSONObject.java:155)
  at org.json.JSONObject.&lt;init&gt;(JSONObject.java:172)
  at com.example.hello.service.client.CreateUser.doInBackground(CreateUser.java:82)
  at com.example.hello.service.client.CreateUser.doInBackground(CreateUser.java:1)
  at android.os.AsyncTask$2.call(AsyncTask.java:288)
  at java.util.concurrent.FutureTask.run(FutureTask.java:237)
  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
  at java.lang.Thread.run(Thread.java:841)
  </code></pre>
  
  <p>Can anyone suggest what could be the problem. Also in my rest service, I am recieving the data with the @FormParam . Any help would be appreciated.</p>