简体   繁体   中英

Jsoup is not working when my android application is running on android device/emulator

I am trying to use jsoup in my android project. I added my jsoup to my project this way:

Created a folder named libs in the root of my project and put my jar files there. Then I go to project Properties, under Java Build Path > Libraries and clicked on Add JARs... and selected jsoup jar files, under the libs folder.

Everything is fine but when I am trying to run it on emulator the application says [Unfortunately, Weather BD has stoped] it shows me below error: LogCat

在此处输入图片说明在此处输入图片说明

My code for the Jsoup (I have made a inner class AsyncTaskParser under a class which extends Fragment).

private class AsyncTaskParser extends AsyncTask<Void, Void, Boolean> {

    CustomProgressBar progress;

    @Override
    protected void onPreExecute() {
        super.onPreExecute();

        progress = new CustomProgressBar(getActivity());
        progress.setCancelable(true);
        progress.setIndeterminate(true);
        progress.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
        progress.show();

    }

    @Override
    protected Boolean doInBackground(Void... arg0) {

        publishProgress();

        return true;
    }

    @Override
    protected void onProgressUpdate(Void... value) {
        jsupDataRecevier();
        bmdAdapter = new BMDListAdapter(getActivity(), city_name, minTemp,
                maxTemp, sunSet, sunRise);

        list.setAdapter(bmdAdapter);

        progress.dismiss();

    }

    @Override
    protected void onPostExecute(Boolean result) {
        super.onPostExecute(result);

        if (result == true) {
            // Log.e(getClass().getName(), "Json Parsing Succesfull");
        } else {
            Toast.makeText(getActivity(),
                    "Sorry unable to get data, Try again later",
                    Toast.LENGTH_LONG).show();
        }
    }


    public void jsupDataRecevier() {
        String html = "http://www.bmd.gov.bd/?/home/";
        Document doc;
        try {
            doc = Jsoup.connect(html).get();
            Elements link = doc.select("div").attr("class", "forecastbox ");
            String linkText = link.text();

            linkText = linkText.substring(0, linkText.indexOf("| Bangladesh"));

            linkText = linkText.replace("| ", "--");
            String modifiedText[] = linkText.split("--");

            int j;
            boolean flag = true; // will determine when the sort is finished
            String temp;

            while (flag) {
                flag = false;
                for (j = 0; j < modifiedText.length - 1; j++) {
                    if (modifiedText[j]
                            .compareToIgnoreCase(modifiedText[j + 1]) > 0) { // ascending
                                                                                // sort
                        temp = modifiedText[j];
                        modifiedText[j] = modifiedText[j + 1]; // swapping
                        modifiedText[j + 1] = temp;
                        flag = true;
                    }
                }
            }

            Scanner inp;
            for (int i = 0; i < modifiedText.length; i++) {

                inp = new Scanner(modifiedText[i].trim());
                String distric = modifiedText[i].substring(0,
                        modifiedText[i].indexOf(':'));
                city_name.add(distric);

                inp = new Scanner(modifiedText[i].substring(
                        modifiedText[i].indexOf(":"), modifiedText[i].length()));
                inp.next();
                String maxTemperature = inp.next();
                maxTemperature = inp.next().substring(0, 4);
                maxTemp.add(maxTemperature);

                String minTemperature = inp.next() + inp.next();
                minTemperature = inp.next().substring(0, 4);
                minTemp.add(minTemperature);

                inp.next();
                inp.next();
                inp.next();

                String sunrise = inp.next().replace('-', ':') + " "
                        + inp.next();
                sunRise.add(sunrise);

                inp.next();
                inp.next();
                inp.next();
                String sunset = inp.next().replace('-', ':') + " " + inp.next();
                sunSet.add(sunset);
            }

        } catch (IOException ex) {
            Toast.makeText(getActivity(), "Sorry try again later",
                    Toast.LENGTH_LONG).show();
            Intent homeIntent = new Intent(getActivity(), MainActivity.class);
            startActivity(homeIntent);
        }
    }


}

The LogCat is indicating that in method jsupDataRecevier() this line doc = Jsoup.connect(html).get(); has problem. I don't understand what wrong I did.

you can not run network request in UI thread. you are invoking jsupDataRecevier(); from onProgressUpdate() which runs in UI thread and it is violation the above mentioned policy. you should use jsoup in your doInBackground()

What I understood from your AsyncTask code, you didn't understand AsyncTask life cycle & how to use properly. For a quick look you can check out this AsyncTask Tutorial

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM