@fanhypermax

В чём ошибка «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
  • Вопрос задан
  • 516 просмотров
Пригласить эксперта
Ответы на вопрос 1
@SeaBreeze876
Front-end разработчик
handler.execute().get() потенциально может бросить ExecutionException, а вы в catch блоке ее не ловите
Ответ написан
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы