开发者

Android Thread loop to check httppost

I have a problem on refresh the listview using thread. How to use it ?

But the current problem i been using this method

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);
        //  httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
           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);



            array_list[i] =  " Table "+json_data.getString("table_id")+" : "+json_data.getString("order_foodname")+" x "+json_data.getString("order_quantity");
            table_list[i]=json_data.getString("table_id");


            listOfPhonebook.add(new Order("Table :"+json_data.getString("table_id"),json_data.getString("order_foodname"),json_data.getString("order_quantity")));
           // Toast.makeText(Chef.this, table_list[i] , Toast.LENGTH_LONG).show();
             }

        adapter = new OrderAdapter(this, listOfPhonebook);


            adapters = new ArrayAdapter<Object>(this,
                     R.layout.row, R.id.label, array_list);


      //       LayoutAnimationController controller 
      //       = AnimationUtils.loadLayoutAnimation(
       //        this, R.layout.list_layout_controller);
       //     lv1.setLayoutAnimation(controller);


    }catch(JSONException e){
            Log.e("log_tag", "Error parsing data "+e.toString());
    }


    return returnString; 
}

i use json to get the file i added the string into adapter


This is bordering on mind reading, but it sounds like you're trying to refresh the ListView outside of the UI-Thread. You cannot do that. Wrap the notifyDataSetChanged-Call inside a Runnable and post it to the UI-Thread, using a Handler:

private final Handler mHandler = new Handler(Looper.getMainLooper());

...

mHandler.post(new Runnable() {
   public void run() {
      mAdapter.notifyDataSetChanged();
   }
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜