title: Send POST data in Android to PHP
body: <p>I'm looking for a way to send POST data to a PHP script and display the result.I want to send POST data of $username to php.
  This is my php file:</p>
  
  <pre><code>&lt;?php
  
      ...// connect
  
              $username = $_POST['txtUsername'];// this data
  
      $mysqli-&gt;query("SET NAMES 'utf8'");
      $sql="SELECT * FROM $username";
      $result=$mysqli-&gt;query($sql);
      while($e=mysqli_fetch_assoc($result)){
      $output[]=$e; 
      }   
  
      print(json_encode($output)); 
      $mysqli-&gt;close();   
  
      ?&gt;
  </code></pre>
  
  <p>My activities:
  <br>DataParser.class</p>
  
  <pre><code>public class DataParser extends AsyncTask&lt;Void,Void,Integer&gt;{
  
  Context c;
  ListView lv;
  String jsonData;
  
  ProgressDialog pd;
  ArrayList&lt;Spacecraft&gt; spacecrafts = new ArrayList&lt;&gt;();
  
  public DataParser(Context c, ListView lv, String jsonData) {
      this.c = c;
      this.lv = lv;
      this.jsonData = jsonData;
  }
  
  @Override
  protected void onPreExecute() {
      super.onPreExecute();
  
      pd= new ProgressDialog(c);
      pd.setTitle("Parse");
      pd.setMessage("Parsing..Please wait");
      pd.show();
  }
  
  @Override
  protected Integer doInBackground(Void... voids) {
      return this.parseData();
  }
  
  @Override
  protected void onPostExecute(Integer result) {
      super.onPostExecute(result);
  
  
      pd.dismiss();
      if (result==0){
          Toast.makeText(c,"Unable to parse",Toast.LENGTH_SHORT).show();
      }
      else {
          CustomAdapter adapter = new CustomAdapter(c,spacecrafts);
          lv.setAdapter(adapter);
      }
  }
  private  int parseData(){
      try {
          JSONArray ja = new JSONArray(jsonData);
          JSONObject jo = null;
  
      spacecrafts.clear();
          Spacecraft s=null;
  
  
          for (int i=0;i&lt;ja.length();i++){
              jo=ja.getJSONObject(i);
              int id = jo.getInt("id");
              String name =jo.getString("name");
              String send =jo.getString("send");
              String comment =jo.getString("comment");
              String date =jo.getString("date");
  
              s=new Spacecraft();
              s.setId(id);
              s.setName(name);
              s.setSend(send);
              s.setComment(comment);
              s.setDate(date);
  
              spacecrafts.add(s);
          }
  
  
          return 1;
      } catch (JSONException e) {
          e.printStackTrace();
      }
  
      return 0;
  }}
  </code></pre>
  
  <p>MainActivity: </p>
  
  <pre><code>public class MainActivity extends AppCompatActivity implements AsyncResponse {
  
  String urlAddress="http://mysite/myphp.php";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
      final ListView lv = (ListView) findViewById(R.id.lv) ;
  
      Intent intent = getIntent();
      final String username = intent.getStringExtra("location");
  
      Downloader d = new Downloader(MainActivity.this,urlAddress,lv);
      d.execute();
  </code></pre>
  
  <p>And Downloader.class:</p>
  
  <pre><code>public class Downloader extends AsyncTask&lt;Void,Void,String&gt; {
  
  Context c;
  String urlAddress;
  ListView lv;
  
  ProgressDialog pd;
  public Downloader(Context c, String urlAddress, ListView lv) {
      this.c = c;
      this.urlAddress = urlAddress;
      this.lv = lv;
  }
  
  @Override
  protected void onPreExecute() {
      super.onPreExecute();
  
      pd = new ProgressDialog(c);
      pd.setTitle("Fetch");
      pd.setMessage("Fetching...Please wait");
      pd.show();
  }
  
  @Override
  protected String doInBackground(Void... voids) {
      return this.downloadData();
  }
  
  @Override
  protected void onPostExecute(String s) {
      super.onPostExecute(s);
  
      pd.dismiss();
  
      if(s==null){
          Toast.makeText(c,"Unsuccessful,Null,returned",Toast.LENGTH_SHORT).show();
      }
      else {
      DataParser parser = new DataParser(c,lv,s);
          parser.execute();
  
      }
  
  }
  
  
  private String downloadData(){
      HttpURLConnection con = Connector.connect(urlAddress);
      if (con==null){
          return null;
      }
      InputStream is=null;
      try {
  
          is=new BufferedInputStream(con.getInputStream());
          BufferedReader br = new BufferedReader(new InputStreamReader(is));
  
      String line = null;
          StringBuffer response = new StringBuffer();
  
          if (br!=null){
              while ((line=br.readLine())!=null){
                  response.append(line+"\n");
              }
  
              br.close();
          }
          else {return null;}
          return response.toString();
  
      } catch (IOException e) {
          e.printStackTrace();
      }
      finally {
          if(is != null){
              try {
                  is.close();
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
      return null;
  }}
  </code></pre>
  
  <p>Please help me, Thank you!</p>