如何使用Gson从服务器加载数据时检查超时

时间:2016-02-24 04:34:59

标签: android



new JSONData().execute(LinkData.HOME,LinkData.HOT,LinkData.NEW,LinkData.LIST_CATEGORY);






private class JSONData extends AsyncTask<String, Void, JSONArrayData> {

        @Override
        protected void onPreExecute() {
            loading = true;
            super.onPreExecute();
        }

        @Override
        protected JSONArrayData doInBackground(String... params) {
            CanBackPress = false;
            URL url;
            try {
                for (int i = 0; i < params.length; i++) {
                    if (checkInternet.checkMobileInternetConn()) {
                        url = new URL(params[i]);
                        InputStreamReader reader = new InputStreamReader(url.openStream(), "UTF-8");

                        if (i == 0) {
                            Post[] post;
                            post = new Gson().fromJson(reader, Post[].class);
                            for (Post p : post) {
                                data.getData().get(0).add(p);
                                // Log.d("post", "" + post.length);
                                Methods.savePostData(getBaseContext(), arrName[0], p);
                            }
                        }

                        if (i == 1) {
                            Post[] post;
                            post = new Gson().fromJson(reader, Post[].class);
                            for (Post p : post) {
                                data.getData().get(1).add(p);
                                Methods.savePostData(getBaseContext(), arrName[3], p);
                                Methods.savePostData(getBaseContext(), arrName[1], p);

                            }
                        }

                        if (i == 2) {
                            Post[] post;
                            post = new Gson().fromJson(reader, Post[].class);
                            for (Post p : post) {
                                data.getData().get(2).add(p);
                                Methods.savePostData(getBaseContext(), arrName[2], p);

                            }
                        }

                        if (i == 3) {
                            Zone[] zone;
                            zone = new Gson().fromJson(reader, Zone[].class);

                            for (Zone z : zone) {
                                data.getZoneList().get(0).add(z);
                                listCategory.add(z);
                            }

                        }
                    } else {

                        break;
                    }
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return data;
        }

        @Override
        protected void onPostExecute(JSONArrayData d) {
            super.onPostExecute(d);
            CanBackPress = true;
            stopThread = true;

            intent.putExtra("MyData", d);

//            if (d.getData().get(0).size() == 0 || d.getData().get(1).size() == 0 || d.getZoneList().get(0).size() == 0) {
//                Log.d("LOI","LOI");
//                showDialog();
//            } else {
            // neu tu push
            if (toNotify) {
                Log.d("eu tu push", "eu tu push");
                startActivity(intent);
                finish();
            } else {
                loading = false;
                loadfn = true;
                if (clickFalse == 0 && clickTrue == 0) {
                    Log.d("boqua","boqua");
                    //checkbox2();
                    stopThread = true;
                    startActivity(intent);
                    finish();
                } else if (login) {
                    Log.d("facebook","facebook");
                    stopThread = true;
                    startActivity(intent);
                    finish();

                }
            }
        }
    }
&#13;
&#13;
&#13;

我用Gson从服务器加载数据Json。现在我想检查以下情况的错误: - 超时(最重要) - 如果Json为null或非标准 我该怎么办? 我的代码加载数据 我称之为:

1 个答案:

答案 0 :(得分:0)

this回答中可以看出,超时引发的异常为java.net.SocketTimeoutException