Q:

Android endless loop to check for incoming data using httppost

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; 
}

A:

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.

