Mainpublic 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);
}
}
Senderpublic 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"