title: Android Passing parameters to php - mysql JSON - Listview
body: <p>I am currenrly running a PHP script which returns data from my database. </p>
  
  <p>Below is my php script with some information commented out. As you can see i am hardcoding the dietid to = 1. I would like to pass into the script the dietid from my activity when the script runs. All my code works OK and the data is displayed in my listview, how do i pass the dietid?</p>
  
  <p>Thanks in advance</p>
  
  <h1>Script to get data from db updated withdietid</h1>
  
  <pre><code>$con = mysqli_connect($host, $user, $pwd, $db);
  
  
   if(mysqli_connect_errno($con)) {
   die("Failed to connect to MySQL: " . mysqli_connect_error());
    } 
  
  
     $dietid = array( $_GET['dietid'] );
     $sql = "SELECT * FROM food WHERE dietid =".$dietid;
     $result = mysqli_query($con, $sql);
  
  
     $rows = array();
  
  
    while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
     $rows[] = $row; 
     }
  
  
     mysqli_close($con);
  
     echo json_encode($rows);
  </code></pre>
  
  <h1>Snipped Main Activity UPDATED WITH NEW URL</h1>
  
  <pre><code>  private void initView() {
      // show progress dialog
      dialog = ProgressDialog.show(this, "", "Loading...");
  
      int dietid = 1;
  
      String url = "http://xxxxxxxxxxx/diet_info.php?dietid="+dietid;
      FetchDietInformationTask task = new FetchDietInformationTask(this);
  //    List&lt;NameValuePair&gt; params = new ArrayList&lt;NameValuePair&gt;();
  
   //   params.add(new BasicNameValuePair("dietid","1"));
      task.execute(url);
  }
  
    public void onFetchComplete(List&lt;FoodInfoModel&gt; data) {
      // dismiss the progress dialog
      this.data = data;
      System.out.println("data is " + data);
  
      if(dialog != null)  dialog.dismiss();
  
      // create new adapter
       adapter = new DietAdapterNew(this, data);
      // set the adapter to list
      listview.setAdapter(adapter);
      adapter.notifyDataSetChanged();
  
     // listview.setOnItemClickListener(myOnItemClickListener);
      }
  
  
  public void onFetchFailure(String msg) {
      // dismiss the progress dialog
      if(dialog != null)  dialog.dismiss();
  
      Toast.makeText(this, msg, Toast.LENGTH_LONG).show();
  }
  </code></pre>
  
  <h1>FetDietInformationTask</h1>
  
  <pre><code>   public class FetchDietInformationTask extends AsyncTask&lt;String, Void,String&gt;{
  
          private final FetchDietInformationListener listener;
          private String msg;
  
  public FetchDietInformationTask(FetchDietInformationListener listener) {
      this.listener = listener;
  }
  
  @Override
  protected String doInBackground(String... params) {
      if(params == null) return null;
  
      // get url from params
      String url = params[0];
  
  
      try {
          // create http connection
          HttpClient client = new DefaultHttpClient();
          HttpGet httpget = new HttpGet(url);
  
          // connect
          HttpResponse response = client.execute(httpget);
  
          // get response
          HttpEntity entity = response.getEntity();
  
          if(entity == null) {
              msg = "No response from server";
              return null;        
          }
  
          // get response content and convert it to json string
          InputStream is = entity.getContent();
          return streamToString(is);
      }
      catch(IOException e){
          msg = "No Network Connection";
      }
  
      return null;
      }
  
      @Override
          protected void onPostExecute(String sJson) {
      if(sJson == null) {
          if(listener != null) listener.onFetchFailure(msg);
          return;
      }        
  
      try {
          // convert json string to json array
          JSONArray aJson = new JSONArray(sJson);
          // create list list
          List&lt;FoodInfoModel&gt; foodmodelist = new ArrayList&lt;FoodInfoModel&gt;();
  
          for(int i=0; i&lt;aJson.length(); i++) {
              JSONObject json = aJson.getJSONObject(i);
              FoodInfoModel foodmodel = new FoodInfoModel();
  
  
              foodmodel.setDietID(Integer.parseInt(json.getString("dietid")));
              foodmodel.setDay(Integer.parseInt(json.getString("day")));
              foodmodel.setTime(Integer.parseInt(json.getString("time")));
              foodmodel.setQty(Integer.parseInt(json.getString("qty")));
              foodmodel.setItem(json.getString("item"));
              foodmodel.setMeasure(json.getString("measure"));
  
  
  
  
              // add the name to list
              foodmodelist.add(foodmodel);
  
  
         for(int e=0; e&lt;foodmodel.getDay(); e++) {
  
             List&lt;DayFoodModel&gt; daylist = new ArrayList&lt;DayFoodModel&gt;();
  
                  DayFoodModel dayfoodmodel = new DayFoodModel();
                  dayfoodmodel.setDay(foodmodel.getDay());
                  dayfoodmodel.setTime(foodmodel.getTime());
                  dayfoodmodel.setItem(foodmodel.getItem());
                  dayfoodmodel.setQty(foodmodel.getQty());
                  dayfoodmodel.setFoodData(foodmodel.getItem() + foodmodel.getMeasure());
  
  
                  daylist.add(dayfoodmodel);
              }
          }
  
          //notify the activity that fetch data has been complete
          if(listener != null) listener.onFetchComplete(foodmodelist);
      } catch (JSONException e) {
          msg = "Invalid response";
          if(listener != null) listener.onFetchFailure(msg);
          return;
      }        
      }
  
  /**
   * This function will convert response stream into json string
   * @param is respons string
   * @return json string
   * @throws IOException
   */
      public String streamToString(final InputStream is) throws IOException{
      BufferedReader reader = new BufferedReader(new InputStreamReader(is));
      StringBuilder sb = new StringBuilder(); 
      String line = null;
  
      try {
          while ((line = reader.readLine()) != null) {
              sb.append(line + "\n");
          }
      } 
      catch (IOException e) {
          throw e;
      } 
      finally {           
          try {
              is.close();
          } 
          catch (IOException e) {
              throw e;
          }
      }
  
      return sb.toString();
      }
      }
  </code></pre>
  
  <h1>Listener</h1>
  
  <pre><code>public interface FetchDietInformationListener {
  public void onFetchComplete(List&lt;FoodInfoModel&gt; data);
  public void onFetchFailure(String msg);
  }
  </code></pre>