• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps KeyDispatchingTimeOut when Parsing XML

I make a code that parsing xml from server, and i do it with AsynkTask (doInBackground) in my activity,but it take too very long time to parsing and finally show ANR Exception and Reason "KeyDispatchingTimeOut"on my activity :
this is my activity :


Code:
public class ListProductLama extends Activity{
    ArrayList<listBarang> list=new ArrayList<listBarang>();
    ArrayList<listBarang> temp=new ArrayList<listBarang>();
    DrawableManager manager=new DrawableManager();
    ParsingXML parse;
    ListProductLama this_c=this;
    onScroll listener=new onScroll();
    ListAdapter adapter;
    int itemsPerPage = 4;
    int itemLoaded=0;
    boolean loadingMore = false;
    boolean full;
    GridView grid;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.adapter);
        StrictMode.ThreadPolicy policy = new StrictMode.
        ThreadPolicy.Builder().permitAll().build();
        StrictMode.setThreadPolicy(policy);
        new GetData().execute();

    }
    //Runnable untuk load the items 
    private Runnable loadMoreListItems = new Runnable() {           
        @Override
        public void run() {
            loadingMore = true;

            //Reset the array that holds the new items
            temp=new ArrayList<listBarang>();

            if(itemLoaded+itemsPerPage>=list.size()){
                itemsPerPage=list.size()-itemLoaded;
                full=true;
            }
                int index=itemLoaded;

                for (int i = 0; i < itemsPerPage; i++) {        
                    temp.add(list.get(index));
                    index++;                
                }
            runOnUiThread(returnRes);

        }
    };  

    private Runnable returnRes = new Runnable() {
        @Override
        public void run() {         
            //Loop through the new items and add them to the adapter
            if(temp != null && temp.size() > 0){
                for(int i=0;i<temp.size();i++)
                    adapter.add(temp.get(i));
            }
            adapter.notifyDataSetChanged();
            itemLoaded+=itemsPerPage;
            loadingMore = false;
        }
    };
    class onScroll implements OnScrollListener{

        @Override
        public void onScroll(AbsListView view, int firstVisibleItem,
                int visibleItemCount, int totalItemCount) {
            // TODO Auto-generated method stub
            if(itemLoaded<list.size()){
                //int lastInScreen = firstVisibleItem + visibleItemCount;

                //if((lastInScreen == totalItemCount) && !(loadingMore)){                   
                    Thread thread =  new Thread(null, loadMoreListItems);
                    thread.start();
                //}
            }           
        }

        @Override
        public void onScrollStateChanged(AbsListView view, int scrollState) {
            // TODO Auto-generated method stub

        }

    }

    public class GetData extends AsyncTask<Void, Void, Void>{
          ProgressDialog dialog;

            protected void onPreExecute() {
            dialog = ProgressDialog.show(this_c, "", "Loading....", true, true);
                dialog.setOnCancelListener(new OnCancelListener() {
                    @Override
                    public void onCancel(DialogInterface arg0) {
                        GetData.this.cancel(true);
                    }
                });
            }

            protected Void doInBackground(Void... params) {
                adapter=new ListAdapter(getApplicationContext(), R.layout.adapterlist,list,manager);
                ParsingXML parse=new ParsingXML(this_c, constant.GET_LIST_PRODUCT);
                parse.parse();
                full=false;
                ListProductHandler handler=(ListProductHandler)parse.getMyExampleHandler();
                ParsedListProductDataSet a=handler.getParsedData();
                list=a.getList();
                return null;
            }

            protected void onPostExecute(Void unused) {
                grid=(GridView)findViewById(R.id.gridView1);        
                grid.setAdapter(adapter);
                grid.setOnScrollListener(listener);
                dialog.dismiss();
            }


      }
}

and this is my ParsingXml class :

Code:
public class ParsingXML {
            //private Context ctx;
    private int status;
    private String raw_url;
    Context mctx;
    private Handler han;
    public DefaultHandler getMyExampleHandler() {
        return myExampleHandler;
    }
    DefaultHandler myExampleHandler=null;

    public ParsingXML(Context ctx , int status){
            //this.ctx=ctx;
            this.status=status;
            this.mctx=ctx;
            if(status==constant.GET_LIST_PRODUCT){
                raw_url=constant.URL+"listBarang.php";
            }
    }
    public ParsingXML(Context ctx,int status,String id){
        this.status=status;
        this.mctx=ctx;
        if(status==constant.GET_DETAIL_PRODUCT){
            raw_url=constant.URL+"detailBarang.php?id="+id;
        }
    }

    public void parse(){    
        try {
            StrictMode.ThreadPolicy policy = new StrictMode.
            ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            URL url = new URL(raw_url);

            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            if(status==constant.GET_LIST_PRODUCT){
                myExampleHandler = new ListProductHandler();
            }
            else if(status==constant.GET_DETAIL_PRODUCT){
                myExampleHandler = new DetailProductHandler();
            }
            xr.setContentHandler(myExampleHandler);

            xr.parse(new InputSource(url.openStream()));

        } catch (Exception e) {
            Toast.makeText(mctx, "Connection Error", Toast.LENGTH_LONG).show();
        }
    }

}
I very need some help for my last project on my college. any help very apreciated.. :)
 
It's hard for me to read the code as I'm on the phandroid app but my guess is that something in your asynctask is calling or blocking the main activity. Hth
 
Back
Top Bottom