本文介绍了如何解决getDataTask方法错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面是我的code有一个问题在getDataTask一些地方,如果我删除这个类是做工精细和打印吐司消息Toast.makeText(此,信息,Toast.LENGTH_SHORT).show();但什么是在下面的JSON文件的问题我getDataTask即时解析问题在doinbackground方法,一些地方帮我请

below is my code there is a problem some where in getDataTask if i remove this class is work fine and print Toast message Toast.makeText(this, message, Toast.LENGTH_SHORT).show(); but what is problem in my getDataTask im parsing below json file problem is some where in doinbackground method help me please

 {"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();
    }
}

}

推荐答案

正在调用 parseJSONData()在doInbackground和你有这样的

You are calling parseJSONData() in doInbackground and you have this

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

您无法更新从 doInbackground UI。你需要在UI线程上更新用户界面。返回结果 doInbackground 。在 onPostExecute 更新用户界面。

you cannot update ui from doInbackground. You need to update ui on the ui thread. Return result in doInbackground. In onPostExecute update ui.

这篇关于如何解决getDataTask方法错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-05 09:11