如何处理 AsyncTask 或 Fragment 中的 Null?

How to handle Null in AsyncTask or Fragment?

在我的 AsyncTask 中,我得到一个对象 Document 以便在 Fragment 中与他进一步合作。问题是有时我得到 document =NULL。我可以捕获这个异常,但不知道如何处理它。假设我有一个 Null。但接下来要做什么?如果我得到数据,我就形成了我的片段。但是如果我得到一个 Null 我不知道如何行动

我的异步任务:

public class MyAsincTask extends AsyncTask<String, Void, Document> {
    private Document document;
    private final ProgressDialog progressDialog;

    public MyAsincTask(Context context) {
        progressDialog = MyProgress.getProgressDialog(context);
    }

    @Override
    protected void onPreExecute() {
        progressDialog.show();
        super.onPreExecute();
    }

    @Override
    protected Document doInBackground(String... params) {
        try {
            document = Jsoup.connect(params[0]).timeout(0).get();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return document;
    }

    @Override
    protected void onPostExecute(Document document) {
        progressDialog.cancel();
        super.onPostExecute(document);
    }
}

如果数据接收成功,我在这里形成我的片段。但是如果你来了 Null 呢?:

public void setData() {
        String link = String.format("http://core.android?key=pe=xml", guestid, hotel);
       new MyAsincTask(getActivity()){
           @Override
           protected void onPreExecute() {
               super.onPreExecute();
           }

           @Override
           protected void onPostExecute(Document document) {
               super.onPostExecute(document);
               Elements elementsId = document.select("id");
               Elements elementsCheckin = document.select("checkin");
               Elements elementsCheckout = document.select("checkout");
               Elements elementsNights = document.select("nights");

               for (int i = 0; i < elementsId.size(); i++) {
                   HistoryBean historyBean = new HistoryBean();
                   historyBean.setId(elementsId.get(i).ownText());
                   historyBean.setCheckin(elementsCheckin.get(i).ownText());
                   historyBean.setCheckout(elementsCheckout.get(i).ownText());
                   historyBean.setNigth(elementsNights.get(i).ownText());
                   historyBeans.add(historyBean);
               }
               listView.setAdapter(historyAdapter);
           }
       }.execute(link);
    }

片段示例:

public class Folio extends Fragment {
    private ArrayList<FolioBean> folioBeans;
    private TableLayout tableFolio;
    private String guestId;
    private String hotel;
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.folio, null);
         guestId = getArguments().getString("guestid");
         hotel = getArguments().getString("hotel");

        folioBeans = new ArrayList<>();
        tableFolio = (TableLayout) v.findViewById(R.id.tableFolio);
        if (InternetResiver.isOnline(getActivity())) {
            setData();
        } else {
            AlertDialog alert = InternetResiver.getAlertDialog(getActivity());
            alert.show();
        }
        return v;
    }

    public static Folio newInstance(String guestid,String hotel,String room) {
        Folio f = new Folio();
        Bundle b = new Bundle();
        b.putString("guestid", guestid);
        b.putString("hotel", hotel);
        b.putString("room", room);
        f.setArguments(b);
        return f;
    }

    public void setData() {
        String link = String.format("htype=xml", guestId, hotel);
        new MyAsincTask(getActivity()){
            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(Document document) {
                super.onPostExecute(document);
                Elements elementsDate = document.select("date");
                Elements elementsName = document.select("name");
                Elements elementsSumm = document.select("summa");

                for (int i = 0; i < elementsName.size(); i++) {
                    FolioBean folioBean = new FolioBean();
                    folioBean.setDate(elementsDate.get(i).ownText());
                    folioBean.setName(elementsName.get(i).ownText());
                    folioBean.setSumm(elementsSumm.get(i).ownText());
                    folioBeans.add(folioBean);
                }
                TableRow.LayoutParams row_params = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 20f);
                TableRow.LayoutParams row_params2 = new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.WRAP_CONTENT, 30f);

                for (FolioBean folioBean : folioBeans) {
                    TableRow tableRow = new TableRow(App.get());
                    TextView data = new TextView(App.get());
                    TextView name = new TextView(App.get());
                    TextView summ = new TextView(App.get());
                    data.setTextSize(10);
                    name.setTextSize(10);
                    summ.setTextSize(10);
                    name.setTextColor(Color.BLACK);
                    summ.setTextColor(Color.BLACK);
                    data.setTextColor(Color.BLACK);
                    if (folioBean.getName().contains("Проживание")){
                        data.setTypeface(null, Typeface.BOLD);
                        name.setTypeface(null, Typeface.BOLD);
                        summ.setTypeface(null, Typeface.BOLD);
                    }
                    data.setText(" " + folioBean.getDate());
                    name.setText(" " + folioBean.getName() + " ");
                    summ.setText(folioBean.getSumm() + " ");
                    tableRow.addView(data, row_params2);
                    tableRow.addView(name, row_params2);
                    tableRow.addView(summ, row_params);
                    tableFolio.addView(tableRow);
                }
            }
        }.execute(link);
    }
}

在您的 onPost() 方法中对文档进行检查

if (document!=null){

      Elements elementsId = document.select("id");
           Elements elementsCheckin = document.select("checkin");
           Elements elementsCheckout = document.select("checkout");
           Elements elementsNights = document.select("nights");

           for (int i = 0; i < elementsId.size(); i++) {
               HistoryBean historyBean = new HistoryBean();
               historyBean.setId(elementsId.get(i).ownText());
               historyBean.setCheckin(elementsCheckin.get(i).ownText());
               historyBean.setCheckout(elementsCheckout.get(i).ownText());
               historyBean.setNigth(elementsNights.get(i).ownText());
               historyBeans.add(historyBean);
           }
           listView.setAdapter(historyAdapter);
}