我有一个PHP
Web 服务并正在查询它以填充用户界面。我有一个问题JSONObject json = jParser.makeHttpRequest(url, "GET",params);
是忽略传递给它的 URL,而是针对最后使用的 PHP URL 运行查询。
要复制这一点,请在 editext1 中搜索“EF”并按下按钮。然后,从edittext1 的末尾删除'F'。LayoutSpinner 将填充 101 个 'Civic' 'Hatch' 'Civic' 'Hatch' 条目,因为它查询的是 test.php Web 服务而不是 menuitems.php。
setOnItemSelectedListener
每当任何微调控件发生更改时,都会触发URL 字符串。然后它会触发后台任务 LoadAllProducts 来查询 Web 服务。jParser.makeHttpRequest(url, "GET",params)
我已经通过使用断点和观察变量确认当行运行时 URL 字符串指向 menuitems.php 。以前有没有其他人遇到过这个问题,我应该走什么路线来解决它?
任何帮助将不胜感激。
这是我的代码:
// view products click event
btnViewProducts.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view)
// Execute LoadAllProducts
url = "http://www.jankuyado.com/Components/test.php";
new LoadAllProducts().execute();
}
// listener for EditText1 changes.
@Override
public void afterTextChanged(Editable s) {
chassiscode = v.getText().toString(); //put the chassiscode into a global variable.
url = "http://www.jankuyado.com/Components/menuitems.php";
new LoadAllProducts().execute();
}
class LoadAllProducts extends AsyncTask<String, String, String> {
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("chassiscode",chassiscode));
params.add(new BasicNameValuePair("Civic", model));
params.add(new BasicNameValuePair("layout", layout));
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url, "GET",params);
products = json.getJSONArray("components");
productslist.clear(); //clear the productslist and start from scratch.
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
if(url == "http://www.jankuyado.com/Components/test.php") {
//if components are being retrieved
// adding HashList to ArrayList
productslist.add(c.getString("ComponentName"));
} else if (url == "http://www.jankuyado.com/Components/menuitems.php") {
//else if menu items are being retrieved.
// Storing each json item in the productslist
if (c.has("model")) {
MenuReturnType = "model"; //flag global variable MenuReturnType as returning car Models (used later when parsing the Productlist).
productslist.add(c.getString("model"));
}
if (c.has("layout")) {
MenuReturnType = "layout"; //flag global variable MenuReturnType as returning car Layouts (used later when parsing the Productlist).
productslist.add(c.getString("layout"));
}
}
}
}
return null;
}
protected void onPostExecute(String file_url) {
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
//stuff that updates ui here