0

我在我的应用程序中使用 Async Task 来使用 restful web 服务从 web 服务中获取响应。我的代码

    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_json_page);
    _mContext = this;

    new JSONParserTask().execute();

}

异步任务类

    private class JSONParserTask extends AsyncTask<Void, Void, ListAdapter >{


    ProgressDialog dialog;


    @Override
    protected void onPreExecute() {

    //  dialog = new ProgressDialog(_mContext); 
    //  dialog.setMessage("Loading...");
    //  dialog.show();
        super.onPreExecute();

    }


    @Override
    protected ListAdapter doInBackground(Void... arg0) {
        ListAdapter adapter = null;
        itemsList = new ArrayList<HashMap<String, String>>();

            jParser = new JSONParser();
        JSONObject json = jParser.getJSONFromUrl(Constants.JsonURL);

          if(json == null){

              Log.v(TAG, "----------- null");
              return null;
          }


            try {

                // Getting Array of Items
                items = json.getJSONArray(TAG_ITEMS);       
                 // looping through All items
                for(int i = 0; i < items.length(); i++) {
                    JSONObject itemsObj = items.getJSONObject(i);
                    JSONObject products = null;
                    products = itemsObj.getJSONObject(TAG_PRODUCT);
                    Log.d(TAG,"product array "+products.toString());

                    JSONArray images = products.getJSONArray(TAG_IMAGES);
                    JSONObject imagesObj = images.getJSONObject(0);
                    Log.d(TAG, "......."+ imagesObj.getString(TAG_LINK));

                    String imageUrl = imagesObj.getString(TAG_LINK);


                    // Storing each json item in variable
                    String kind = itemsObj.getString(TAG_KIND);
                    String id = itemsObj.getString(TAG_KID);
                            String selfLink = itemsObj.getString(TAG_SELFLINK);

                            HashMap<String, String> map = new HashMap<String, String>();

                         // adding each child node to HashMap key => value
                        map.put(TAG_KIND, kind);
                        map.put(TAG_KID, id);
                        map.put(TAG_SELFLINK, selfLink);

                            // adding HashList to ArrayList
                                itemsList.add(map);

                }
              /**
                 * Updating parsed JSON data into ListView
                 * */
                 adapter = new SimpleAdapter(_mContext, itemsList,
                        R.layout.list_item_row,
                        new String[] { TAG_KIND,  TAG_SELFLINK }, new int[] {
                                R.id.name, R.id.mobile });



            } catch(JSONException e){
                e.printStackTrace();
            }       
            return adapter;
    }

    @Override
    protected void onPostExecute(ListAdapter adapter) {


            lv.setAdapter(adapter);    

            // Launching new screen on Selecting Single ListItem
            lv.setOnItemClickListener(new OnItemClickListener() {

               public void onItemClick(AdapterView<?> parent, View view,
                        int position, long id) {
                    // some action
                }
            });

        //dialog.dismiss();
    }      
   }

使用此代码,无需使用进度对话框,一切都可以正常工作。如果你找到了,与进度对话框相关的代码会在上面的类中注释掉。如果我取消注释进度对话框代码,则不会从服务器收到任何响应。我也尝试过调试,但从来没有想过要消除这个错误。有人可以告诉我在这里做错了什么。

4

4 回答 4

1

好的,原因是您正在更新您的doInBackground()方法中的适配器

        adapter = new SimpleAdapter(_mContext, itemsList,
                    R.layout.list_item_row,
                    new String[] { TAG_KIND,  TAG_SELFLINK }, new int[] {
                            R.id.name, R.id.mobile });

此代码与 MAIN THREAD 相关,不应在后台线程中调用,将其从此处删除,并将其添加到onPostExecute(),只需从后台线程传递一个数组列表并在onPostExecute()

于 2012-11-01T12:27:07.383 回答
0

试试这个,而不是

    ProgressDialog mProgressDialog;

    @Override
    protected void onPreExecute() {
        mProgressDialog= ProgressDialog.show(getApplicationContext(),"", getString(R.string.dialog_wait_message));
        super.onPreExecute();
    }

    @Override
    protected ListAdapter doInBackground(Void... arg0){
      //do your stuff here
    }

    @Override
    protected void onPostExecute(Void result) {
            if(mProgressDialog!=null && mProgressDialog.isShowing()){
                mProgressDialog.dismiss();
       }
}
于 2012-11-01T12:15:16.233 回答
0

尝试这个

          @Override
    protected void onPreExecute() {
        pd=new ProgressDialog(m_context);
        pd.setTitle("Authenticating");
        pd.show();

    }
  @Override
    protected Void doInBackground(Void... args) {
            //your stuff
           }
        @Override
    protected void onPostExecute(Void result) {

        pd.dismiss();
           }
于 2012-11-01T12:15:51.693 回答
0

尝试这个

ProgressDialog mProgressDialog;

@Override
        protected void onPreExecute() {
            mProgressDialog= ProgressDialog.show(getApplicationContext(),"", getString(R.string.dialog_wait_message));
            super.onPreExecute();
        }




   protected void onPostExecute(Void result) {
                if(mProgressDialog!=null){
                    mProgressDialog.dismiss();
           }
}
于 2012-11-01T12:11:11.270 回答