title: How to put parameters into httpPost? Entity keeps giving me null for params
body: <p>I'm trying to send parameters to php to mysql via the POST method. It seems that my code doesn't load the parameters into httpPost. When debugging I see that params are null under httpPost variable and it seems that the entity isn't being picked up. Please any help would be greatly appreciated. I'm using android studio.</p>
  
  <p>Main Activity</p>
  
  <pre><code>public class MainActivity extends ActionBarActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  }
  
  
  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
      // Inflate the menu; this adds items to the action bar if it is present.
      getMenuInflater().inflate(R.menu.menu_main, menu);
      return true;
  }
  
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
      // Handle action bar item clicks here. The action bar will
      // automatically handle clicks on the Home/Up button, so long
      // as you specify a parent activity in AndroidManifest.xml.
      int id = item.getItemId();
  
      //noinspection SimplifiableIfStatement
      if (id == R.id.action_settings) {
          return true;
      }
  
      return super.onOptionsItemSelected(item);
  }
  
  public void setSave(View view) {
      new TestSQL().execute();
  
  }
  
  private class TestSQL extends AsyncTask&lt;String, String, JSONObject&gt; {
  
      @Override
      protected JSONObject doInBackground(String... params) {
          WriteToDatabase write = new WriteToDatabase();
          String a = "1";
          String b = "2";
          String c = "3";
          String d = "4";
          String e = "5";
          String f = "6";
          String g = "7";
          JSONObject json1 = write.writeDB(a, b,
                  c, d, e, f, g);
          return json1;
      }
  }
  </code></pre>
  
  <p>WriteToDatabase.java  to build List Namevaluepairs</p>
  
  <pre><code>public class WriteToDatabase {
  
  private JSONParser test;
  
  // URL of the PHP API
  private static String link = "http://192.168.1.102/test/test2.php";
  
  public JSONObject writeDB(String a,String b,
                            String  c, String d,String e,String f,String g) {
      // Building Parameters
      List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
      params.add(new BasicNameValuePair("name", b));
      params.add(new BasicNameValuePair("description",
              c));
      params.add(new BasicNameValuePair("add", d));
      params.add(new BasicNameValuePair("city", e));
      params.add(new BasicNameValuePair("cat", a));
      params.add(new BasicNameValuePair("uid", f));
      params.add(new BasicNameValuePair("day", g));
      JSONParser test = new JSONParser();
      JSONObject json = test.getJSONFromUrl(link, params);
  
      return json;
  
  }
  </code></pre>
  
  <p>}</p>
  
  <p>my JSONParser.</p>
  
  <pre><code>public class JSONParser {
  
  static InputStream is = null;
  static JSONObject jObj = null;
  static String json = "";
  
  // constructor
  public JSONParser() {
  
  }
  
  public JSONObject getJSONFromUrl(String url, List&lt;NameValuePair&gt; params) {
  
      // Making HTTP request
      try {
  
          HttpPost httpPost = new HttpPost(url);
          httpPost.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
       // With or without HTTP.UTF_8 won't work
          DefaultHttpClient httpClient = new DefaultHttpClient();
          UrlEncodedFormEntity ent = new UrlEncodedFormEntity(params, HTTP.UTF_8);
          httpPost.setEntity(ent);
          HttpResponse httpResponse = httpClient.execute(httpPost);
          HttpEntity httpEntity = httpResponse.getEntity();
          if (httpEntity != null) {
              Log.i("RESPONSE", EntityUtils.toString(httpEntity));
          }
          is = httpEntity.getContent();
  
      } catch (UnsupportedEncodingException e) {
          e.printStackTrace();
      } catch (ClientProtocolException e) {
          e.printStackTrace();
      } catch (IOException e) {
          e.printStackTrace();
  
      }
  
      try {
          BufferedReader reader = new BufferedReader(new InputStreamReader(
                  is, "iso-8859-1"), 8);
          StringBuilder sb = new StringBuilder();
          String line = null;
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
          is.close();
          json = sb.toString();
          Log.e("JSON", json);
      } catch (Exception e) {
          Log.e("Buffer Error", "Error converting result " + e.toString());
      }
  
      // try parse the string to a JSON object
      try {
          jObj = new JSONObject(json);
      } catch (JSONException e) {
          Log.e("JSON Parser", "Error parsing data " + e.toString());
      }
  
      // return JSON String
      return jObj;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>and my Logcat</p>
  
  <pre><code>11-15 21:30:04.273      818-836/com.example.julian.testforsql E/AndroidRuntime﹕ FATAL EXCEPTION: AsyncTask #1
  java.lang.RuntimeException: An error occured while executing doInBackground()
          at android.os.AsyncTask$3.done(AsyncTask.java:299)
          at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
          at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
          at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
          at java.util.concurrent.FutureTask.run(FutureTask.java:137)
          at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
          at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
          at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
          at java.lang.Thread.run(Thread.java:856)
   Caused by: java.lang.IllegalStateException: Content has been consumed
          at org.apache.http.entity.BasicHttpEntity.getContent(BasicHttpEntity.java:84)
          at org.apache.http.conn.BasicManagedEntity.getContent(BasicManagedEntity.java:100)
          at com.example.julian.testforsql.JSONParser.getJSONFromUrl(JSONParser.java:57)
          at com.example.julian.testforsql.WriteToDatabase.writeDB(WriteToDatabase.java:30)
          at com.example.julian.testforsql.MainActivity$TestSQL.doInBackground(MainActivity.java:62)
          at com.example.julian.testforsql.MainActivity$TestSQL.doInBackground(MainActivity.java:50)
          at android.os.AsyncTask$2.call(AsyncTask.java:287)
          at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
  </code></pre>
  
  <p>            at java.util.concurrent.FutureTask.run(FutureTask.java:137)
              at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
              at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
              at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
              at java.lang.Thread.run(Thread.java:856)</p>