I’ve added a return statement to my AsncTask and yet I still get an error telling me to add one. The only snippet of code that stops this Syntax error is adding a return statement after the catch statement, but that’s counter productive and doesn’t address the needs of the program and I can’t access the Strings I need to ( I need to check if the return OuputStream is equal to true.
Code:
@Override
protected Boolean doInBackground(String... userandpass) { //I still get an error telling me to add a return statement
// TODO Auto-generated method stub
URL url;
try {
url = new URL("http://127.0.0.1:1337");
HttpURLConnection URLconnection = (HttpURLConnection) url.openConnection();
URLconnection.setDoOutput(true);
URLconnection.setChunkedStreamingMode(0);
//output stream
OutputStream out = new BufferedOutputStream(URLconnection.getOutputStream());
writestream(out, userandpass);
//buffered server response
InputStream in = new BufferedInputStream(URLconnection.getInputStream());
String result = readstream(in);
Log.e(result, result);
// check we haven't been redirected (Hotel Wifi, for example).
checkrediect(URLconnection, url);
Boolean result_true = checkresult(result);
if(result_true) {
return true;
} else {
return false;
}
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
Well what are the “needs of the program”? What do you want the result to be if an
IOExceptionis thrown? It must be true, false, or an exception – at the moment, it’s none of those.I’d recommend that most of the time, you just let exceptions bubble up… can you actually proceed as if nothing had gone wrong in the case of an
IOException?As a side-note, this is ugly:
Just use:
(And ideally rename various methods and variables to follow Java naming conventions.)
I’d also suggest changing it to return
booleanrather thanBooelean.