简体   繁体   English

当我的android应用程序在android设备/模拟器上运行时Jsoup无法正常工作

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

I am trying to use jsoup in my android project. 我正在尝试在我的Android项目中使用jsoup。 I added my jsoup to my project this way: 我通过这种方式将jsoup添加到项目中:

Created a folder named libs in the root of my project and put my jar files there. 在我的项目的根目录中创建了一个名为libs的文件夹,并将我的jar文件放在那里。 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. 然后,我去Java Build Path> Libraries下的Project Properties,然后单击libs文件夹下的Add JARs ...和选定的jsoup jar文件。

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 一切都很好,但是当我尝试在模拟器上运行它时,应用程序显示[不幸的是,Weather BD已停止],它向我显示以下错误:LogCat

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

My code for the Jsoup (I have made a inner class AsyncTaskParser under a class which extends Fragment). 我的Jsoup代码(我在扩展Fragment的类下创建了一个内部类AsyncTaskParser)。

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(); LogCat指示在方法jsupDataRecevier()此行doc = Jsoup.connect(html).get(); has problem. 有问题。 I don't understand what wrong I did. 我不明白我做错了什么。

you can not run network request in UI thread. 您无法在UI线程中运行网络请求。 you are invoking jsupDataRecevier(); 您正在调用jsupDataRecevier(); from onProgressUpdate() which runs in UI thread and it is violation the above mentioned policy. 来自在UI线程中运行的onProgressUpdate() ,它违反了上述策略。 you should use jsoup in your doInBackground() 您应该在doInBackground()使用jsoup

What I understood from your AsyncTask code, you didn't understand AsyncTask life cycle & how to use properly. 从您的AsyncTask代码中我了解到的是,您不了解AsyncTask的生命周期以及如何正确使用。 For a quick look you can check out this AsyncTask Tutorial 快速浏览,您可以查看此AsyncTask教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Android套接字应用程序在模拟器上运行但不在设备上运行 - Android socket application running on emulator but not working on device 我的Android应用程序在模拟器上运行,但无法在我的Android设备上运行 - My Android application runs on emulator but not working on my android device Android - Jsoup 连接在模拟器上成功但在设备上失败 - Android - Jsoup connect succeeds on emulator but fails on device 在Android模拟器上运行应用程序 - running application on android emulator 在仿真器上工作但不在真正的Android设备上 - Working on Emulator but not on the real Android device Android:开发的应用程序在模拟器中工作但未安装在我的设备中 - Android: developed application works in emulator but not getting installed in my device 在Android模拟器上运行Facebook应用程序 - running facebook application on android emulator 限制在Android移动模拟器上运行的Android应用程序 - Restricting running Android application on Android mobile emulator 后退按钮在模拟器上运行,但在Android设备中不运行 - back button is working on emulator but it is not working in device in android Android应用程序可在模拟器中运行,但无法在设备上运行 - Android application works in emulator but failed in device
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM