Android Передача параметров в MySQL JSON — Listview

Сейчас я использую PHP-скрипт, который возвращает данные из моей базы данных.

Ниже мой php скрипт с закомментированной информацией. Как вы можете видеть, я жестко прописываю диету в = 1. Я хотел бы передать в сценарий диету из моей деятельности при запуске скрипта. Весь мой код работает нормально, и данные отображаются в моем списке просмотра, как я могу передать 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);
}

0

Решение

$dietid = $_GET['dietid'];
$sql = "SELECT * FROM food WHERE dietid =".$dietid;

String url = "http://xxxxxxxxxxx/diet_info.php?dietid="+dietid;
-1

Другие решения

Других решений пока нет …

По вопросам рекламы [email protected]