Android将参数传递给php-mysql JSON-Listview

Android将参数传递给php-mysql JSON-Listview,php,android,mysql,json,listview,Php,Android,Mysql,Json,Listview,我现在正在运行一个PHP脚本,它从我的数据库返回数据 下面是我的php脚本,其中有些信息被注释掉了。如您所见,我正在将dietid硬编码为=1。我想在脚本运行时将活动中的dietid传递到脚本中。我所有的代码都正常工作,数据显示在我的listview中,我如何传递dietid 提前谢谢 从数据库中获取数据的脚本已更新为dietId 截取的主要活动已更新为新的URL private void initView(){ //显示进度对话框 dialog=ProgressDialog.show(这个,“

我现在正在运行一个PHP脚本,它从我的数据库返回数据

下面是我的php脚本,其中有些信息被注释掉了。如您所见,我正在将dietid硬编码为=1。我想在脚本运行时将活动中的dietid传递到脚本中。我所有的代码都正常工作,数据显示在我的listview中,我如何传递dietid

提前谢谢

从数据库中获取数据的脚本已更新为dietId 截取的主要活动已更新为新的URL
private void initView(){
//显示进度对话框
dialog=ProgressDialog.show(这个,“,”加载…);
int-dietid=1;
字符串url=”http://xxxxxxxxxxx/diet_info.php?dietid=“+迪特里德;
FetchDietInformationTask任务=新建FetchDietInformationTask(此任务);
//List params=new ArrayList();
//参数添加(新的BasicNameValuePair(“dietid”、“1”);
task.execute(url);
}
公共void onFetchComplete(列表数据){
//关闭进度对话框
这个数据=数据;
System.out.println(“数据为”+数据);
如果(dialog!=null)dialog.disclose();
//创建新适配器
adapter=新的DietAdapterNew(此,数据);
//将适配器设置为list
setAdapter(适配器);
adapter.notifyDataSetChanged();
//setOnItemClickListener(myOnItemClickListener);
}
public void onFetchFailure(字符串msg){
//关闭进度对话框
如果(dialog!=null)dialog.disclose();
Toast.makeText(this,msg,Toast.LENGTH_LONG).show();
}
费特信息任务
公共类FetchDietInformationTask扩展了AsyncTask{
私有最终获取信息侦听器;
私有字符串msg;
公共FetchDietInformationTask(FetchDietInformationListener侦听器){
this.listener=listener;
}
@凌驾
受保护的字符串doInBackground(字符串…参数){
if(params==null)返回null;
//从参数获取url
字符串url=params[0];
试一试{
//创建http连接
HttpClient=new DefaultHttpClient();
HttpGet HttpGet=新的HttpGet(url);
//连接
HttpResponse response=client.execute(httpget);
//得到回应
HttpEntity=response.getEntity();
if(实体==null){
msg=“服务器无响应”;
返回null;
}
//获取响应内容并将其转换为json字符串
InputStream=entity.getContent();
返回streamToString(is);
}
捕获(IOE异常){
msg=“无网络连接”;
}
返回null;
}
@凌驾
受保护的void onPostExecute(字符串sJson){
if(sJson==null){
if(listener!=null)listener.onFetchFailure(msg);
回来
}        
试一试{
//将json字符串转换为json数组
JSONArray aJson=新JSONArray(sJson);
//创建列表
List foodmodelist=new ArrayList();

对于(int i=0;iIm从json捕获中得到一个无效的响应消息,即使我将URL设置为“它不会将数据加载到listview中,但如果我只是硬编码Selects中的2,则会这样做似乎当我在地址栏中加载该URL时,它会给出以下结果::mysqli_fetch_array()期望参数1为mysqli_result,布尔值为:while($row=mysqli_fetch_数组($result,mysqli_ASSOC)){$rows[]=$row;您是否在php中添加了以上两行script@Sunit不应该
$dietid=array($\u GET['dietid']);
应该
$dietid=$\u GET['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);
  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;