Php 为什么AsyncTask总是返回相同的响应?
我有这样一段代码,它调用一个php脚本,用json进行回复。 json用于显示为listview。 问题是,下面的代码始终为我提供相同的json,即使这些数据不再存在于通过php脚本连接的数据库中 我遵循这个指南 但我无法计算运行时会发生什么。 我希望有人能帮助我Php 为什么AsyncTask总是返回相同的响应?,php,android,listview,android-asynctask,Php,Android,Listview,Android Asynctask,我有这样一段代码,它调用一个php脚本,用json进行回复。 json用于显示为listview。 问题是,下面的代码始终为我提供相同的json,即使这些数据不再存在于通过php脚本连接的数据库中 我遵循这个指南 但我无法计算运行时会发生什么。 我希望有人能帮助我 public class ShiftListFragment extends ListFragment { // Progress Dialog private static ProgressDialog pDialog; //
public class ShiftListFragment extends ListFragment {
// Progress Dialog
private static ProgressDialog pDialog;
// Creating JSON Parser object
JSONParser jParser = new JSONParser();
ArrayList<HashMap<String, String>> productsList;
// url to get all products list
private static String url_all_products = "http://www.changeshifts.netsons.org/android_connect/get_all_shifts.php";
// JSON Node names
private static final String TAG_SUCCESS = "success";
private static final String TAG_AVAILABLE_SHIFTS = "Available shifts";
private static final String TAG_IDSHIFT = "shift id";
private static final String TAG_DATE = "date";
private static final String TAG_SHIFT = "shift";
private static final String TAG_START_LOCATION = "start location";
private static final String TAG_BEGIN = "begin";
private static final String TAG_END = "end";
private static final String TAG_IDENTIFICATION_NUMBER = "identification number";
// products JSONArray
JSONArray products = null;
// Custom adapter to view list of shifts
private CustomAdapter adapter;
/**
* The serialization (saved instance state) Bundle key representing the
* activated item position. Only used on tablets.
*/
private static final String STATE_ACTIVATED_POSITION = "activated_position";
/**
* The fragment's current callback object, which is notified of list item
* clicks.
*/
private Callbacks mCallbacks = sDummyCallbacks;
/**
* The current activated item position. Only used on tablets.
*/
private int mActivatedPosition = ListView.INVALID_POSITION;
/**
* A callback interface that all activities containing this fragment must
* implement. This mechanism allows activities to be notified of item
* selections.
*/
public interface Callbacks {
/**
* Callback for when an item has been selected.
*/
public void onItemSelected(String id);
}
/**
* A dummy implementation of the {@link Callbacks} interface that does
* nothing. Used only when this fragment is not attached to an activity.
*/
private static Callbacks sDummyCallbacks = new Callbacks() {
@Override
public void onItemSelected(String id) {
}
};
/**
* Mandatory empty constructor for the fragment manager to instantiate the
* fragment (e.g. upon screen orientation changes).
*/
public ShiftListFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Loading products in Background Thread
new LoadAllProducts().execute();
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// Restore the previously serialized activated item position.
if (savedInstanceState != null
&& savedInstanceState.containsKey(STATE_ACTIVATED_POSITION)) {
setActivatedPosition(savedInstanceState
.getInt(STATE_ACTIVATED_POSITION));
}
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Activities containing this fragment must implement its callbacks.
if (!(activity instanceof Callbacks)) {
throw new IllegalStateException(
"Activity must implement fragment's callbacks.");
}
mCallbacks = (Callbacks) activity;
}
@Override
public void onDetach() {
super.onDetach();
// Reset the active callbacks interface to the dummy implementation.
mCallbacks = sDummyCallbacks;
}
@Override
public void onListItemClick(ListView listView, View view, int position,
long id) {
super.onListItemClick(listView, view, position, id);
// Notify the active callbacks interface (the activity, if the
// fragment is attached to one) that an item has been selected.
mCallbacks.onItemSelected(productsList.get(position).get(TAG_IDSHIFT));
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
if (mActivatedPosition != ListView.INVALID_POSITION) {
// Serialize and persist the activated item position.
outState.putInt(STATE_ACTIVATED_POSITION, mActivatedPosition);
}
}
/**
* Turns on activate-on-click mode. When this mode is on, list items will be
* given the 'activated' state when touched.
*/
public void setActivateOnItemClick(boolean activateOnItemClick) {
// When setting CHOICE_MODE_SINGLE, ListView will automatically
// give items the 'activated' state when touched.
getListView().setChoiceMode(
activateOnItemClick ? ListView.CHOICE_MODE_SINGLE
: ListView.CHOICE_MODE_NONE);
}
private void setActivatedPosition(int position) {
if (position == ListView.INVALID_POSITION) {
getListView().setItemChecked(mActivatedPosition, false);
} else {
getListView().setItemChecked(position, true);
}
mActivatedPosition = position;
}
/**
* Background Async Task to Load all product by making HTTP Request
* */
class LoadAllProducts extends AsyncTask<String, String, String> {
/**
* Before starting background thread Show Progress Dialog
* */
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(getActivity());
pDialog.setMessage("Loading Shifts. Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
/**
* getting All products from url
* */
protected String doInBackground(String... args) {
// Building Parameters
List<NameValuePair> params = new ArrayList<NameValuePair>();
// getting JSON string from URL
JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params);
// Check your log cat for JSON reponse
Log.d("All Products: ", json.toString());
try {
// Checking for SUCCESS TAG
int success = json.getInt(TAG_SUCCESS);
if (success == 1) {
// products found
// Getting Array of Products
products = json.getJSONArray(TAG_AVAILABLE_SHIFTS);
productsList = new ArrayList<HashMap<String,String>>();
// looping through All Products
for (int i = 0; i < products.length(); i++) {
JSONObject c = products.getJSONObject(i);
// Storing each json item in variable
// String idShift = c.getString(TAG_IDSHIFT);
String date = c.getString(TAG_DATE);
String shift = c.getString(TAG_SHIFT);
String location = c.getString(TAG_START_LOCATION);
String begin = c.getString(TAG_BEGIN);
String end = c.getString(TAG_END);
String sn = c.getString(TAG_IDENTIFICATION_NUMBER);
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
// adding each child node to HashMap key => value
// map.put(TAG_IDSHIFT, idShift);
map.put(TAG_DATE, date);
map.put(TAG_SHIFT, shift);
map.put(TAG_START_LOCATION, location);
map.put(TAG_BEGIN, begin);
map.put(TAG_END, end);
map.put(TAG_IDENTIFICATION_NUMBER, sn);
// adding HashList to ArrayList
productsList.add(map);
}
} else {
// no Shifts found
// Launch Add New product Activity
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
/**
* After completing background task Dismiss the progress dialog
* **/
protected void onPostExecute(String file_url) {
super.onPostExecute(file_url);
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
getActivity().runOnUiThread(new Runnable(){
public void run(){
/**
* Updating parsed JSON data into ListView
* */
adapter = new CustomAdapter(getActivity(), R.layout.list_item, productsList);
setListAdapter(adapter);
}
});
}
}
公共类ShiftListFragment扩展ListFragment{
//进度对话框
专用静态进程对话框;
//创建JSON解析器对象
JSONParser jParser=新的JSONParser();
ArrayList productsList;
//获取所有产品列表的url
私有静态字符串url\u所有产品=”http://www.changeshifts.netsons.org/android_connect/get_all_shifts.php";
//JSON节点名称
私有静态最终字符串标记_SUCCESS=“SUCCESS”;
专用静态最终字符串标记\u可用\u移位=“可用移位”;
私有静态最终字符串标记_IDSHIFT=“shift id”;
私有静态最终字符串标记_DATE=“DATE”;
私有静态最终字符串标记\u SHIFT=“SHIFT”;
私有静态最终字符串标记\u START\u LOCATION=“START LOCATION”;
私有静态最终字符串标记\u BEGIN=“BEGIN”;
私有静态最终字符串标记_END=“END”;
专用静态最终字符串标记\u标识\u编号=“标识编号”;
//产品JSONArray
JSONArray产品=null;
//用于查看班次列表的自定义适配器
专用自定义适配器;
/**
*表示
*激活项目位置。仅用于片剂。
*/
私有静态最终字符串状态\u ACTIVATED\u POSITION=“ACTIVATED\u POSITION”;
/**
*片段的当前回调对象,它被通知列表项
*点击。
*/
私有回调mCallbacks=sDummyCallbacks;
/**
*当前激活的项目位置。仅用于平板电脑。
*/
private int mActivatedPosition=ListView.INVALID_POSITION;
/**
*包含此片段的所有活动都必须使用的回调接口
*此机制允许向活动通知项目
*选择。
*/
公共接口回调{
/**
*选择项时的回调。
*/
已选择公共id(字符串id);
}
/**
*{@link Callbacks}接口的虚拟实现
*无。仅当此片段未附加到活动时使用。
*/
私有静态回调sDummyCallbacks=新回调(){
@凌驾
已选择公共id(字符串id){
}
};
/**
*片段管理器实例化
*碎片(如屏幕方向改变时)。
*/
公共移位列表片段(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//ListView的Hashmap
productsList=新的ArrayList();
//在后台线程中加载产品
新建LoadAllProducts().execute();
}
@凌驾
已创建视图上的公共void(视图,捆绑保存状态){
super.onViewCreated(视图,savedInstanceState);
//恢复以前序列化的激活项目位置。
如果(savedInstanceState!=null
&&savedInstanceState.containsKey(状态为激活状态){
设置激活位置(savedInstanceState
.getInt(状态激活位置);
}
}
@凌驾
公共事务主任(活动){
超级转速计(活动);
//包含此片段的活动必须实现其回调。
if(!(回调的活动实例)){
抛出新的非法状态异常(
“活动必须实现片段的回调。”);
}
mCallbacks=(回调)活动;
}
@凌驾
公共无效连接(){
super.onDetach();
//将活动回调接口重置为虚拟实现。
mCallbacks=sDummyCallbacks;
}
@凌驾
public void onListItemClick(ListView ListView、View视图、int位置、,
长id){
super.onListItemClick(列表视图、视图、位置、id);
//通知活动回调接口(活动,如果
//片段被附加到一个)上,表示已选择项目。
mCallbacks.onItemSelected(productsList.get(position.get(TAG_IDSHIFT));
}
@凌驾
SaveInstanceState上的公共无效(束超出状态){
super.onSaveInstanceState(超出状态);
if(mActivatedPosition!=列表视图。位置无效){
//序列化并保留激活的项目位置。
outState.putInt(状态激活位置,mActivatedPosition);
}
}
/**
*打开“单击激活”模式。当此模式打开时,列表项将被删除
*如果触摸时处于“激活”状态。
*/
public void setActivateOnItemClick(布尔activateOnItemClick){
//当设置CHOICE_MODE_SINGLE时,ListView将自动
//触摸物品时使其处于“激活”状态。
getListView().setChoiceMode(
activateOnItemClick?ListView.CHOICE\u MODE\u SINGLE
:ListView.CHOICE\u MODE\u NONE);
}
私有void setActivatedPosition(内部位置){
if(position==ListView.INVALID_position){
getListView().setItemChecked(mActivatedPosition,false);
}否则{
getListView().setItemChecked(位置,true);
}
mActivatedPosition=位置;
}
/**
*通过发出HTTP请求加载所有产品的后台异步任务
* */
类LoadAllProducts扩展了AsyncTask{
/**
*在启动后台线程显示进度对话框之前
* */
@凌驾
受保护的void onPreExecute(){
super.onPreExecute();
pDialog=newprogressdialog(getActivity());
pDialog.setMessage(“正在加载班次,请稍候…”);
pDialog.setUndeterminate(假);
pDialog.setCancelable(假);
pDialog.show();
}
/**
*从url获取所有产品
* */
受保护的字符串doInBackground(字符串…args){
//建筑参数
List params=new ArrayList();
//获取JSON字符串