Php AsyncTask如何实现,在此代码中刷新?
下面的代码提取了ANDROID MYSQL从PHP读取JSON数据,我该如何做才能每10秒更新一次,ListView也会更新,应用程序基本上总是咨询不断运行PHP的数据库,以每10秒重新读取生成的JSON代码,提前感谢您的任何帮助或建议 代码如下:Php AsyncTask如何实现,在此代码中刷新?,php,android,mysql,json,android-asynctask,Php,Android,Mysql,Json,Android Asynctask,下面的代码提取了ANDROID MYSQL从PHP读取JSON数据,我该如何做才能每10秒更新一次,ListView也会更新,应用程序基本上总是咨询不断运行PHP的数据库,以每10秒重新读取生成的JSON代码,提前感谢您的任何帮助或建议 代码如下: public class VerA extends ActionBarActivity { String myJSON; private static final String TAG_RESULTS="result"; private sta
public class VerA extends ActionBarActivity {
String myJSON;
private static final String TAG_RESULTS="result";
private static final String TAG_ID = "id";
private static final String TAG_NAME = "nombre";
private static final String TAG_ADD ="telefono";
JSONArray peoples = null;
ArrayList<HashMap<String, String>> personList;
ListView list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
personList = new ArrayList<HashMap<String, String>>();
getData();
}
// METOD SHOW LIST
protected void showList(){
try {
JSONObject jsonObj = new JSONObject(myJSON);
peoples = jsonObj.getJSONArray(TAG_RESULTS);
for(int i=0;i<peoples.length();i++){
JSONObject c = peoples.getJSONObject(i);
String id = c.getString(TAG_ID);
String name = c.getString(TAG_NAME);
String address = c.getString(TAG_ADD);
HashMap<String,String> persons = new HashMap<String,String>();
persons.put(TAG_ID,id);
persons.put(TAG_NAME,name);
persons.put(TAG_ADD,address);
personList.add(persons);
}
ListAdapter adapter = new SimpleAdapter(
VerA.this, personList, R.layout.list_item,
new String[]{TAG_ID,TAG_NAME,TAG_ADD},
new int[]{R.id.id, R.id.name, R.id.address}
);
list.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
///METOD GET DATA JSON READ FROM PHP
public void getData(){
class GetDataJSON extends AsyncTask<String, Void, String>{
@Override
protected String doInBackground(String... params) {
DefaultHttpClient httpclient = new DefaultHttpClient(new BasicHttpParams());
HttpPost httppost = new HttpPost("http://tuescuelanoticias.x10.mx/selectAllJSON.php");
// Depends on your web service
httppost.setHeader("Content-type", "application/json");
InputStream inputStream = null;
String result = null;
try {
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
inputStream = entity.getContent();
// json is UTF-8 by default
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null)
{
sb.append(line + "\n");
}
result = sb.toString();
} catch (Exception e) {
// Oops
}
finally {
try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
}
return result;
}
@Override
protected void onPostExecute(String result){
myJSON=result;
showList();
}
}
GetDataJSON g = new GetDataJSON();
g.execute();
}
}
公共类VerA扩展了ActionBarActivity{
字符串myJSON;
私有静态最终字符串标记_RESULTS=“result”;
私有静态最终字符串标记\u ID=“ID”;
私有静态最终字符串标记_NAME=“nombre”;
专用静态最终字符串标记_ADD=“telefono”;
JSONArray-peoples=null;
ArrayList个人主义者;
列表视图列表;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.ListView);
personList=新的ArrayList();
getData();
}
//METOD显示列表
受保护的无效显示列表(){
试一试{
JSONObject jsonObj=新的JSONObject(myJSON);
peoples=jsonObj.getJSONArray(标记结果);
对于(int i=0;i您必须创建一个服务
来调用getData()
方法每10次minstankyou@helldawg13我这样做了:在onCreate中,添加下一个metod,并且正在工作,但是出现了一个新问题,每次调用metod时,滚动条、条拉起,如何解决这个问题。我想是的,每隔10秒运行一次metod getData,但只检查数据库中指示存在新数据的值,然后再次调用get data.it会在每次创建服务时重新创建listview您必须创建一个服务
来调用getData()
方法每10次minstankyou@helldawg13我这样做了:在onCreate中,添加下一个metod,并且正在工作,但是出现了一个新问题,每次调用metod时,滚动条、条拉起,如何解决这个问题。我想是的,每隔10秒运行一次metod getData,但只检查数据库中指示存在新数据的值,然后再次调用get data.it会在您每次访问服务时重新创建listview
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView);
myHandler.postDelayed(new Runnable() {
@Override
public void run() {
personList = new ArrayList<HashMap<String, String>>();
getData();
handler.postDelayed(this, 10000);
}
}, 0);
}