0

下面是我的代码,如果我删除此类工作正常并打印 Toast 消息 Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); 但是我的getDataTask解析下面的json文件问题有什么问题是doinbackground方法中的一些地方请帮助我

 {"status":0,"message":"No such school found"}



           public class thirdstep extends Activity {


ListView listCategory;

String status;
String message;
String MenuSelect;
ProgressBar prgLoading;
long Cat_ID;
String Cat_name;

String CategoryAPI;
int IOConnect = 0;
TextView txtAlert;
thirdstepAdapter cla;
static ArrayList<String> Category_ID = new ArrayList<String>();
static ArrayList<String> Category_name = new ArrayList<String>();
static ArrayList<String> Category_image = new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.category_list2);
    ImageButton btnback = (ImageButton) findViewById(R.id.btnback);
    listCategory = (ListView) findViewById(R.id.listCategory2);
       prgLoading = (ProgressBar) findViewById(R.id.prgLoading);
       txtAlert = (TextView) findViewById(R.id.txtAlert);
    cla = new thirdstepAdapter(thirdstep.this);

    new getDataTask().execute();

    listCategory.setAdapter(cla);

    btnback.setOnClickListener(new OnClickListener()

    {

        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            finish();
        }
    });

    Intent iGet = getIntent();
    Cat_ID = iGet.getLongExtra("category_id", 0);
    Cat_name = iGet.getStringExtra("category_name");

    Toast.makeText(this, Cat_ID + Cat_name, Toast.LENGTH_SHORT).show();

    MenuSelect = Utils.MenuSelect;

    listCategory.setOnItemClickListener(new OnItemClickListener() {

        public void onItemClick(AdapterView<?> arg0, View arg1,
                int position, long arg3) {
            // TODO Auto-generated method stub
            Intent iMenuList = new Intent(thirdstep.this,   fourthscreen.class);


            iMenuList.putExtra("Cat_ID",Cat_ID);
            iMenuList.putExtra("Menuitem", Category_ID.get(position));


            startActivity(iMenuList);

        }
    });



}

void clearData() {
    Category_ID.clear();
    Category_name.clear();
    Category_image.clear();
}





 public class getDataTask extends AsyncTask<Void, Void, Void>{

        getDataTask(){
            if(!prgLoading.isShown()){
                prgLoading.setVisibility(0);
                txtAlert.setVisibility(8);
            }
        }

        @Override
         protected void onPreExecute() {
          // TODO Auto-generated method stub

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            // TODO Auto-generated method stub
            parseJSONData();
            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub
            prgLoading.setVisibility(8);
            if((Category_ID.size() > 0) || IOConnect == 0){
                listCategory.setVisibility(0);
                listCategory.setAdapter(cla);
            }else{
                txtAlert.setVisibility(0);
            }
        }
    }

public void parseJSONData() {


    CategoryAPI = Utils.MenuList + Cat_ID;

    clearData();
    try {

        HttpClient client = new DefaultHttpClient();
        HttpConnectionParams
                .setConnectionTimeout(client.getParams(), 15000);
        HttpConnectionParams.setSoTimeout(client.getParams(), 15000);
        HttpUriRequest request = new HttpGet(CategoryAPI);
        HttpResponse response = client.execute(request);
        InputStream atomInputStream = response.getEntity().getContent();
        BufferedReader in = new BufferedReader(new InputStreamReader(
                atomInputStream));

        String line;
        String str = "";
        while ((line = in.readLine()) != null) {
            str += line;
        }

        JSONObject json = new JSONObject(str);

        JSONObject json2 = new JSONObject(str);


        status = json2.getString("status");
        message = json2.getString("message");

        if (status.equals("1")) {

        JSONObject data = json.getJSONObject("data");

        JSONArray school = data.getJSONArray("menu_groups");




        for (int i = 0; i < school.length(); i++) {
            JSONObject object = school.getJSONObject(i);

            Category_ID.add(object.getString("id"));
            Category_name.add(object.getString("title"));
            Category_image.add(object.getString("image"));

        }

        }
        else    
        {

            Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
        }

    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        IOConnect = 1;
        e.printStackTrace();
    } catch (JSONException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

}

4

1 回答 1

1

你正在调用parseJSONData()doInbackground 并且你有这个

  Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); // in parseJSONData()

您无法从doInbackground. 您需要在 ui 线程上更新 ui。中返回结果doInbackground。在onPostExecute更新 ui 中。

于 2013-08-13T14:03:58.263 回答