Answer the question
In order to leave comments, you need to log in
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);
}
}
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;
}
}
Answer the question
In order to leave comments, you need to log in
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 questionAsk a Question
731 491 924 answers to any question