我已经向AsncTask添加了一个返回语句,但仍然收到一个错误,要求我添加一个返回语句。停止这个语法错误的唯一代码片段是在catch语句之后添加一个return语句,但这是适得其反的,并且不能满足程序的需要,并且我无法访问我需要的字符串(我需要检查return OuputStream是否等于true。
代码:
@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();
}
}发布于 2012-08-03 20:39:30
,但这会适得其反,并且不能满足程序的需求
那么,什么是“程序的需求”呢?如果抛出IOException,您希望得到什么结果?它必须是真的、假的或异常的--目前,它们都不是。
我建议在大多数情况下,只要让异常冒出来就行了。在IOException的情况下,您是否真的可以像没有任何错误一样继续进行
作为附注,这是丑陋的:
if(result_true) {
return true;
} else {
return false;
}只需使用:
return checkresult(result);(理想情况下,重命名各种方法和变量以遵循Java命名约定。)
我还建议将其更改为返回boolean而不是Booelean。
发布于 2012-08-03 20:40:07
catch语句的一个分支既不返回值也不抛出异常。
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}该块需要某种形式的代码来返回某些内容,否则该方法将无法正常运行。
发布于 2012-08-03 20:40:06
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return false;
}https://stackoverflow.com/questions/11795886
复制相似问题