Android无限循环使用httppost检查传入数据
目前,当我尝试通过 HttpPost 刷新或更新列表视图适配器时遇到问题。
我想创建一个无尽的线程来调用 HttpPost 连接,以便获取数据并将其存储在适配器内。最后我想将适配器重新设置为列表视图。我可以让它在按钮上工作。但我想使用保持循环 mysql 中数据的线程。
请帮我。我找不到任何解决方案。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
lv2=(ListView)findViewById(R.id.ListView02);
tvalert=(TextView)findViewById(R.id.textviewalert);
getServerDatas("http://172.50.2.66/ChefGetOrdered.php");
tv = new TextView(this);
tv.setText("Incoming Order");
tv1 = new TextView(this);
tv1.setText(" Waiting list");
lv1.addHeaderView(tv);
lv2.addHeaderView(tv1);
lv2.setAdapter(adapter);
lv2.setClickable(true);
lv2.setTextFilterEnabled(true);
lv1.setAdapter(adapter);
lv1.setClickable(true);
lv1.setDivider(null);
lv1.setDividerHeight(0);
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lv1.getItemAtPosition(position);
int a = (int) lv1.getItemIdAtPosition(position);
int ab = a+1;
Toast.makeText(Chef.this, o.toString() , Toast.LENGTH_LONG).show();
Toast.makeText(Chef.this, ab+"", Toast.LENGTH_LONG).show();
}
});
Button button2 = (Button) findViewById(R.id.ButtonCheck);
button2.setOnClickListener(this);
}
private String getServerDatas(String returnString) {
InputStream is = null;
String result = "";
List<Order> listOfPhonebook = new ArrayList<Order>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(returnString);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Toast.makeText(Chef.this, e.toString()+"Number 1 " , Toast.LENGTH_LONG).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
totalorder = jArray.length();
array_list=new String[totalorder];
table_list= new String[totalorder];
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
table_list[i]=json_data.getString("table_id");
listOfPhonebook.add(new Order("Table :"+json_data.getString("table_id")+"\n Time:",json_data.getString("order_foodname"),json_data.getString("order_quantity")));
}
adapter = new OrderAdapter(this, listOfPhonebook);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
Currently I'm having problem when I try to refresh or update the listview adapter via HttpPost.
I want to make an endless thread calling the HttpPost connection in order to fetch the data and store inside an adapter. At the end I want to re-set the adapter to the listview. I could make it work on button. But I want work with the thread that keep loop to the data in mysql.
Please help me. I cant find any solution.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv1=(ListView)findViewById(R.id.ListView01);
lv2=(ListView)findViewById(R.id.ListView02);
tvalert=(TextView)findViewById(R.id.textviewalert);
getServerDatas("http://172.50.2.66/ChefGetOrdered.php");
tv = new TextView(this);
tv.setText("Incoming Order");
tv1 = new TextView(this);
tv1.setText(" Waiting list");
lv1.addHeaderView(tv);
lv2.addHeaderView(tv1);
lv2.setAdapter(adapter);
lv2.setClickable(true);
lv2.setTextFilterEnabled(true);
lv1.setAdapter(adapter);
lv1.setClickable(true);
lv1.setDivider(null);
lv1.setDividerHeight(0);
lv1.setTextFilterEnabled(true);
lv1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Object o = lv1.getItemAtPosition(position);
int a = (int) lv1.getItemIdAtPosition(position);
int ab = a+1;
Toast.makeText(Chef.this, o.toString() , Toast.LENGTH_LONG).show();
Toast.makeText(Chef.this, ab+"", Toast.LENGTH_LONG).show();
}
});
Button button2 = (Button) findViewById(R.id.ButtonCheck);
button2.setOnClickListener(this);
}
private String getServerDatas(String returnString) {
InputStream is = null;
String result = "";
List<Order> listOfPhonebook = new ArrayList<Order>();
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(returnString);
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
is = entity.getContent();
}catch(Exception e){
Toast.makeText(Chef.this, e.toString()+"Number 1 " , Toast.LENGTH_LONG).show();
}
//convert response to string
try{
BufferedReader reader = new BufferedReader(new InputStreamReader(is,"iso-8859-1"),8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
result=sb.toString();
}catch(Exception e){
Log.e("log_tag", "Error converting result "+e.toString());
}
try{
JSONArray jArray = new JSONArray(result);
totalorder = jArray.length();
array_list=new String[totalorder];
table_list= new String[totalorder];
for(int i=0;i<jArray.length();i++){
JSONObject json_data = jArray.getJSONObject(i);
table_list[i]=json_data.getString("table_id");
listOfPhonebook.add(new Order("Table :"+json_data.getString("table_id")+"\n Time:",json_data.getString("order_foodname"),json_data.getString("order_quantity")));
}
adapter = new OrderAdapter(this, listOfPhonebook);
}catch(JSONException e){
Log.e("log_tag", "Error parsing data "+e.toString());
}
return returnString;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
抽象循环并获取数据,从其他线程更新 UI 的必要条件是 Handler。请在此处了解更多信息。
Abstracting of loop and getting the data, the necessary thing you should have to update UI from other thread is a Handler. Read more here.