Android将参数传递到mysql JSON-Listview


Android Passing parameters to mysql JSON - Listview

我目前正在运行一个PHP脚本,该脚本从我的数据库中返回数据。

下面是我的php脚本,其中注释了一些信息。正如你所看到的,我正在将dietid硬编码为=1。当脚本运行时,我想将活动中的营养师传递到脚本中。我所有的代码都工作正常,数据显示在我的列表视图中,我如何传递dietid?

提前感谢

从使用dietid更新的数据库获取数据的脚本

$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);

用新URL更新的分段主活动

  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<NameValuePair> params = new ArrayList<NameValuePair>();
 //   params.add(new BasicNameValuePair("dietid","1"));
    task.execute(url);
}
  public void onFetchComplete(List<FoodInfoModel> 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();
}

胎儿饮食信息任务

   public class FetchDietInformationTask extends AsyncTask<String, Void,String>{
        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<FoodInfoModel> foodmodelist = new ArrayList<FoodInfoModel>();
        for(int i=0; i<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<foodmodel.getDay(); e++) {
           List<DayFoodModel> daylist = new ArrayList<DayFoodModel>();
                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();
    }
    }

侦听器

public interface FetchDietInformationListener {
public void onFetchComplete(List<FoodInfoModel> data);
public void onFetchFailure(String msg);
}
$dietid = $_GET['dietid'];
$sql = "SELECT * FROM food WHERE dietid =".$dietid;
String url = "http://xxxxxxxxxxx/diet_info.php?dietid="+dietid;