F
F
fanhypermax2018-11-11 00:57:09
Java
fanhypermax, 2018-11-11 00:57:09

What is the error "error: unreported exception ExecutionException;"...?

Main

public class MainActivity extends AppCompatActivity {

    TextView TextView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView = (TextView) findViewById(R.id.result);

        Get();

    }

    public void Get() {

        Sender handler = new Sender();

        String result = "";

        try {

            result = handler.execute("http://localhost/get").get();

        } catch (InterruptedException e) {
            //...
        }

        TextView.setText(result);
    }
}

Sender
public class Sender extends AsyncTask<String, Void, String> {

   private OkHttpClient client = new OkHttpClient();

    @Override
    protected String doInBackground(String... params)  {

        Request.Builder builder = new Request.Builder();
        builder.url(params[0]);

        Request request = builder.build();

        try {

            Response response = client.newCall(request).execute();
            if (!response.isSuccessful())
                throw new IOException("Unexpected code " + response.toString());
            return response.body().string();

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

        return null;
    }
    
}

"error: unreported exception ExecutionException; must be caught or declared to be thrown"
LNlL5O6kRYaGXNafBAMGNA.png

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Anatoly Zharov, 2018-11-11
@SeaBreeze876

handler.execute().get() can potentially throw an ExecutionException, and you don't catch it in the catch block

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question