首页 > 解决方案 > 如何在 java 中使用 jsoup 的值?

问题描述

我正在尝试使用我在使用 jsoup 在 Android Studio 中对 yahoo Finance 进行网络抓取时获得的信息做出一个 if 语句。

我使用下面的代码在我的文本视图中获得了比特币的价值,但是我不可能在“if 语句”中使用比特币的价值。我尝试使用Integer.parseInt(quote.text())等等,但我的应用程序不会显示任何内容,即使“if”和“else”都显示了比特币的价值?我能够构建 APK,但是当我按下按钮时没有任何反应?

请注意,如果您同时删除了 if 语句, foo = Double.parseDouble(String.valueOf(quote));它将在 textview 上显示比特币的价值。

如果比特币的价值低于或高于 70000,我想显示一个文本。有人知道吗?

public class MainActivity extends AppCompatActivity {

    TextView texx;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        texx=(TextView)findViewById(R.id.textView);
        Button button=(Button) findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new doit().execute();
            }
        });
    }


    public class doit extends AsyncTask<Void, Void, Void>{

        String word;
        double a = 70000;
        double foo;


        @Override
        protected Void doInBackground(Void... voids) {

            String url="https://finance.yahoo.com/quote/BTC-USD?p=BTC-USD&.tsrc=fin-srch";

            try {

                Document doc = Jsoup.connect(url).get();
                Element quote = doc.select("span[data-reactid='32']").first();

                foo = Double.parseDouble(String.valueOf(quote));

                if(foo<a){
                    word = quote.text();
                }
                else{
                    word = quote.text();
                }






            }catch (Exception e){e.printStackTrace();}

            return null;
        }

        @Override
        protected void onPostExecute(Void aVoid) {
            super.onPostExecute(aVoid);

            texx.setText(word);
        }
    }

标签: androidandroid-studioif-statementweb-scrapingjsoup

解决方案


推荐阅读