Asynctask无法在真实设备上运行

时间:2017-05-11 10:01:15

标签: android mysql android-asynctask androidhttpclient

在这篇文章发表之前,我还没有发现我的问题的帖子对我有帮助。 首先,我构建了一个非常简单的Android应用程序,应该连接到数据库(MySQL),将食物数据(食物名称)加载到ListView中,列出食物数量。

DB:db_food_resto

表:

#id_row  : int autoincrement primary key.

food_name : varchar 255 not null

food_country : varchar 255 not null //till now i don't need this column maybe in the future this way i added this.

我使用eclipse和Genymontion模拟器所有的东西都很棒没有错误编译器没有异常抛出并且在我在手机上测试时可以在模拟器上正常运行未加载的食物列表但是它在平板电脑上加载!

我使用asynctask获取数据我不知道为什么它不能在手机上工作并在平板电脑上工作。

我已经在这个问题上打了好几天。

这是我的代码,经过一些修改,让它更短一些。

公共类HistoryActivity扩展了Activity     {         static ArrayList arr = new ArrayList<>(); // Info是一个包含食物名称属性的类和一个包含一个参数foodname

的构造函数
    ListView historylistfood;
    LayoutInflater mInflater;
    TextView foodcount;
    @TargetApi(Build.VERSION_CODES.HONEYCOMB)
    @SuppressLint("NewApi")
    @Override
    protected void onCreate(Bundle savedInstanceState) {

                             ...
                             ...
            if (Build.VERSION.SDK_INT>=Build.VERSION_CODES.HONEYCOMB) {
            new Tasklist().executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,"load_all");

            }
            else {
                new Tasklist().execute("load_all");
            }
     }

    class Tasklist extends AsyncTask<String, String, String>
    {
        @Override
        protected String doInBackground(String... params) 
        {
            StringBuffer sb = null;
            try
            {
                String link="mydomain.com/file.php?type="+params[0];
                HttpClient httpclient = new DefaultHttpClient();

                HttpPost httppost = new HttpPost(link); 
                HttpResponse response = httpclient.execute(httppost);

                BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent())); 
                sb = new StringBuffer("");
                String line="";

                while ((line = in.readLine()) != null) 
                {
                    sb.append(line);        
                    //sb is a list of foods gotten from PHP file seperated with |
                    //something like that: food name 1|food name 2|food name 3|ABC|
                    //then i  break the loop
                     break;
                 } 
                 in.close(); 

                getlistfood(sb.toString());

            } catch(Exception e){
                e.printStackTrace() ;
            }
            return null;
        }
        @Override
        protected void onPostExecute(String result) 
        {
            // TODO Auto-generated method stub
            super.onPostExecute(result);
            if(arr.size()>0)
            {
                ArrayAdapter<Info> adapter = new HArrayAdapter(); 
                historylistfood.setAdapter(adapter);
                Toast.makeText(getApplicationContext(), "On post exectue", Toast.LENGTH_LONG).show();
                foodcount.setVisibility(View.VISIBLE);
                foodcount.setText(""+arr.size());
                foodcount.startAnimation(AnimationUtils.loadAnimation(getApplicationContext(), R.anim.movecount));
            }
            else
            {
                Toast.makeText(getApplicationContext(), "Intenet issue.", Toast.LENGTH_LONG).show(); // this is what cell phones showed
            }
        }
    }


    public void getlistfood(String st) {

        arr = new ArrayList<>();
        String[] spl =  st.toString().split("|");
        for(int i=0;i<spl.length;i++)
        {   Info i = new Info(spl[i]);
            arr.add(i);

        }
    }

    public class HArrayAdapter extends ArrayAdapter<Info> 
    {
        public  HArrayAdapter() {
             super(HistoryActivity.this,R.layout.activity_item,arr);
        }
        public View getView(int position, View convertView, ViewGroup parent) 
        {
            View itemv = convertView;
            Info poo = arr.get(position);
            if(itemv== null)
            { 
                itemv = mInflater.inflate(R.layout.activity_item, parent,false);
            }

            TextView foodname = (TextView)itemv.findViewById(R.id.food);
            foodname.setText(poo.getFoodname()); 
            return itemv; 
        }
    }
}

0 个答案:

没有答案