Android无限循环使用httppost检查传入数据

发布于 2024-10-27 20:49:34 字数 3434 浏览 1 评论 0原文

目前,当我尝试通过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2024-11-03 20:49:34

抽象循环并获取数据,从其他线程更新 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文