WaitingInMainSignalCatcherLoop线程对信号3 ANR作出反应

时间:2016-10-19 13:10:33

标签: android android-fragments android-activity java-threads android-anr-dialog

当我尝试解析json输出并在由片段组成的活动中显示我的输出时,我得到的应用程序没有响应。 随着输出不断变化,我根据我从respnse获得的小部件类型动态添加我的片段。 片段可以是10-15。正如在stackoverflow的答案之一中所见,我保留了我在单独线程上解析的逻辑,然后我在ui线程上添加片段。

    @Override
    protected void onPostExecute(String s) {
        progressBar.setVisibility(View.GONE);
        if (!s.equalsIgnoreCase("") && !s.equalsIgnoreCase("null")) {
            fragment_container.setVisibility(View.VISIBLE);
            network_error.setVisibility(View.GONE);
            try {
                JSONObject jsonObject = new JSONObject(s);
                JSONArray widgetJsonArray = jsonObject.getJSONArray("widgets");
                for (int i = 0; i < widgetJsonArray.length(); i++) {
                    JSONObject widgetObject = widgetJsonArray.getJSONObject(i);
                    String widget_type = widgetObject.getString("widget_type");
                    if (widget_type.equalsIgnoreCase("auto_product_deals")) {
                        processAutoProductDeals(widgetObject);
                    } else if (widget_type.equalsIgnoreCase("manual_product")) {
                        processManualProduct(widgetObject);
                    } else if (widget_type.equalsIgnoreCase("manual_list")) {
                        processManualList(widgetObject);
                    }
                }
            } catch (JSONException e) {
                e.printStackTrace();
            }
        } else {
            network_error.setVisibility(View.VISIBLE);
        }
    }

    private void processManualList(final JSONObject widgetObject) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    ArrayList<ManualListPojo> listManual = new ArrayList<>();
                    JSONObject dataObject = widgetObject.getJSONObject("data");
                    String title = dataObject.getString("title");
                    if (!title.equalsIgnoreCase("shop by brand") && !title.equalsIgnoreCase("shop by discount")) {
                        JSONArray dealsArray = dataObject.getJSONArray("deals");
                        for (int i = 0; i < dealsArray.length(); i++) {
                            ManualListPojo manualDealPojo = new ManualListPojo();
                            JSONObject manualObject = dealsArray.getJSONObject(i);
                            manualDealPojo.setTitle(manualObject.getString("title"));
                            manualDealPojo.setImage(manualObject.getString("image"));
                            manualDealPojo.setPrice_text(manualObject.getString("price_text"));
                            manualDealPojo.setValid_till(manualObject.getString("valid_till"));
                            manualDealPojo.setStore(manualObject.optString("store"));
                            JSONObject linkObject = manualObject.getJSONObject("link");
                            if (linkObject.has("category"))
                                manualDealPojo.setCategory(linkObject.getString("category"));
                            if (linkObject.has("comparables"))
                                manualDealPojo.setComparables(linkObject.getString("comparables"));
                            if (linkObject.has("url"))
                                manualDealPojo.setUrl(linkObject.getString("url"));
                            if (linkObject.has("filters")) {
                                JSONObject filtersObject = linkObject.getJSONObject("filters");
                                if (filtersObject.has("properties")) {
                                    JSONArray properties = filtersObject.getJSONArray("properties");
                                    ArrayList<String> propertiesArray = new ArrayList<>();
                                    for (int j = 0; j < properties.length(); j++) {
                                        propertiesArray.add(properties.getString(j));
                                    }
                                    manualDealPojo.setProperty(propertiesArray);
                                }
                                if (filtersObject.has("price")) {
                                    JSONObject priceObject = filtersObject.optJSONObject("price");
                                    manualDealPojo.setMin(priceObject.optString("min"));
                                    manualDealPojo.setMax(priceObject.optString("max"));
                                }
                                manualDealPojo.setSort(filtersObject.optString("sort"));
                            }
                            listManual.add(manualDealPojo);
                        }
                    }
                    if (listManual.size() > 0) {
                        final Activity activity = (Activity) IndexActivity.this;
                        if (activity != null && !activity.isFinishing()) {
                            final FragmentManager fragmentManager = getFragmentManager();
                            final ManualListFragment hello = new ManualListFragment(listManual, title);
                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                                    fragmentTransaction.add(R.id.fragment_container, hello, "Manual_List");
                                    fragmentTransaction.commitAllowingStateLoss();
                                }
                            });

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

    }

    private void processManualProduct(final JSONObject widgetObject) {
        Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    ArrayList<ItemPojo> listItem = new ArrayList<>();
                    JSONObject dataObject = widgetObject.getJSONObject("data");
                    String title = dataObject.getString("title");
                    JSONArray jsonItemArray = dataObject.getJSONArray("items");
                    for (int i = 0; i < jsonItemArray.length(); i++) {
                        JSONObject itemObject = jsonItemArray.getJSONObject(i);
                        ItemPojo itemPojoObject = new ItemPojo();
                        itemPojoObject.setImage(itemObject.getString("image"));
                        itemPojoObject.setId(itemObject.getString("id"));
                        itemPojoObject.setFinal_price(itemObject.getString("final_price"));
                        if (itemObject.has("mrp")) {
                            itemPojoObject.setMrp(itemObject.getString("mrp"));
                        }
                        itemPojoObject.setPrice(itemObject.getString("price"));
                        itemPojoObject.setTitle(itemObject.getString("title"));
                        itemPojoObject.setRatings(itemObject.getString("ratings"));
                        itemPojoObject.setDiscount(itemObject.getString("discount"));
                        itemPojoObject.setAvg_rating(itemObject.getString("avg_rating"));
                        listItem.add(itemPojoObject);
                    }
                    final Activity activity = (Activity) IndexActivity.this;
                    if (activity != null && !activity.isFinishing()) {
                        final FragmentManager fragmentManager = getFragmentManager();
                        final ManualProductFragment hello = new ManualProductFragment(listItem, title);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                                fragmentTransaction.add(R.id.fragment_container, hello, "Manual_Product");
                                fragmentTransaction.commitAllowingStateLoss();
                            }
                        });
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };
        thread.start();

    }


    private void processAutoProductDeals(final JSONObject widgetObject) {
        final Thread thread = new Thread() {
            @Override
            public void run() {
                try {
                    ArrayList<DealPojo> listDeal = new ArrayList<>();
                    JSONObject dataObject = widgetObject.getJSONObject("data");
                    String title = dataObject.getString("title");
                    JSONArray dealsArray = dataObject.getJSONArray("deals");
                    for (int i = 0; i < dealsArray.length(); i++) {
                        JSONObject dealObject = dealsArray.getJSONObject(i);
                        DealPojo dealPojoObject = new DealPojo();
                        dealPojoObject.setStore(dealObject.getString("store"));
                        String url = dealObject.getString("url");
                        if (url.contains("sendtostore.php")) {
                            url.replace("sendtostore", "justswipe");
                        }
                        dealPojoObject.setUrl(url);
                        dealPojoObject.setId(dealObject.getString("id"));
                        dealPojoObject.setPrice(dealObject.getString("price"));
                        dealPojoObject.setMrp(dealObject.getString("mrp"));
                        dealPojoObject.setPrice_text(dealObject.getString("price_text"));
                        dealPojoObject.setTitle(dealObject.getString("title"));
                        dealPojoObject.setImage(dealObject.getString("image"));
                        listDeal.add(dealPojoObject);
                    }
                    final Activity activity = (Activity) IndexActivity.this;
                    if (activity != null && !activity.isFinishing()) {
                        final FragmentManager fragmentManager = getFragmentManager();
                        final AutoProductDeal hello = new AutoProductDeal(listDeal, title);
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                                fragmentTransaction.add(R.id.fragment_container, hello, "Auto_Product");
                                fragmentTransaction.commitAllowingStateLoss();
                            }
                        });

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

    }

0 个答案:

没有答案