Home > Back-end >  So I am making an app that tells me if I should bring an umbrella today
So I am making an app that tells me if I should bring an umbrella today

Time:06-06

I am making an app in Android Studio and I get a Fatal Exception when I click on the button. The Idea of code is (using the jsoup liabrary) to look up the chance to rain today and if the percentage is >50 than it says yes and if it less than it says no.
here is the code

    enterpackage com.example.doineedanumbrellatoday;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import android.view.View;
import android.widget.*;
import java.io.IOException;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
TextView txtview;
Button button;

    public MainActivity() throws IOException {
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        txtview = (TextView)findViewById(R.id.text);
        button = (Button)findViewById(R.id.button);
        button.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
if(view.getId()==R.id.button){
    Document doc= null;
    try {
        doc = Jsoup.connect("https://dalivali.bg/?location=222").timeout(6000).get();

    } catch (IOException e) {
        e.printStackTrace();
    }
    org.jsoup.select.Elements rain = doc.select("span.rain-num");
    //System.out.println(rain);
    char[] chars2 = rain.toString().toCharArray();
    StringBuilder chance = new StringBuilder();

    for(char c : chars2){
        if(Character.isDigit(c)){
            chance.append(c);
        }
    }
    if(Integer.parseInt(chance.toString())>=50)
        txtview.setText("Yes");
    else
        txtview.setText("No");
}
    }



} 

Could someone tell me why I get an exception

CodePudding user response:

As Gabe Sechan suggested, you have to run the connection of JSoup code in a background thread so your UI doesn't freeze and then you have to start a UI thread inside a background thread because you can't access views like TextView inside a background thread. So, you can start a background thread for the connection and UI thread for setting of texts like this:

        Thread thread = new Thread() {
            @Override
            public void run() {

                Document document = null;

                try {
                    document = Jsoup.connect("https://dalivali.bg/?location=222").timeout(6000).get();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                assert document != null;
                org.jsoup.select.Elements rain = document.select("span.rain-num");

                char[] chars2 = rain.toString().toCharArray();
                StringBuilder chance = new StringBuilder();

                for (char c : chars2) {
                    if (Character.isDigit(c)) {
                        chance.append(c);
                    }
                }

                runOnUiThread(() -> {
                    if (Integer.parseInt(chance.toString()) >= 50)
                        txtview.setText("Yes");
                    else
                        txtview.setText("No");
                });
            }
        };

        thread.start();
  • Related