Android JSON崩溃了NetworkOnMainThreadException

时间:2013-12-01 18:56:46

标签: android json

我有我的项目,一切都很好,但有一个问题。我把我的应用程序交给了朋友,进行测试。然后她说它崩溃了。我们发现应用程序崩溃了:

            isInternetPresent = cd
            isInternetPresent = cd.isConnectingToInternet();

            if (isInternetPresent) {

                JSONParser jParser = new JSONParser();

                JSONObject json = jParser
                        .getJSONFromUrl(CONTROL_URL);

                try {
                    control = json.getInt("value");

                } catch (JSONException e) {
                    // TODO Auto-generated catch
                    // block
                    e.printStackTrace();
                }

                if (control == 1) {
                        new AttemptLogin().execute();

                } else {
                    //I'm showing something when there is not allow to use it
                }
            } else {
                //I'm showing something when there is no Internet connection
            }

我的用于向数据库发送值的类:

class AttemptLogin extends AsyncTask<String, String, String> {

        @Override
        protected void onPostExecute(String file_url) {
            // TODO Auto-generated method stub
            super.onPostExecute(file_url);
            pDialog.dismiss();

        }

        @Override
        protected String doInBackground(String... args) {
            // TODO Auto-generated method stub
            position += 1;
            String pos = position.toString();

            int success;
            try {
                List<NameValuePair> params = new ArrayList<NameValuePair>();
                params.add(new BasicNameValuePair("pos", pos));

                JSONObject json = jsonParser.makeHttpRequest(VOTE_URL, "POST",
                        params);

                success = json.getInt(TAG_SUCCESS);

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

            return null;
        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            super.onPreExecute();
            pDialog = new ProgressDialog(Dalyvis.this);
            pDialog.setMessage("Siunčiame balsą...");
            pDialog.setIndeterminate(false);
            pDialog.setCancelable(true);
            pDialog.show();

        }

    }

我不知道为什么,但对于模拟器来说一切都很棒。我真的知道我的朋友有互联网连接。谁能帮助我?

修改

我猜异常是NetworkOnMainThreadException。因为稍后,我首先获得控制值,然后检查设备是否有互联网连接。我换了它。然后首先检查互联网连接,然后检查控制值。对于模拟器来说一切都还可以。检查我之前的问题和正确答案:JSON can't get Int value from php

事实上我的应用程序在这里崩溃了:

                isInternetPresent = cd.isConnectingToInternet();
                if (isInternetPresent) {
                    JSONParser jParser = new JSONParser();
                    JSONObject json = jParser.getJSONFromUrl(CONTROL_URL);

                    try {
                        control = json.getInt("value");

                    } catch (JSONException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

0 个答案:

没有答案