The*_*Cat 0 java android return
我已经向我的AsncTask添加了一个return语句,但我仍然收到一个错误,告诉我要添加一个.停止此语法错误的唯一代码片段是在catch语句之后添加一个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();
}
}
Run Code Online (Sandbox Code Playgroud)
Jon*_*eet 11
但这反效果并不能满足计划的需要
那么"该计划的需求"是什么?如果被抛出,你想要的结果IOException是什么?它必须是真实的,错误的或异常的 - 此刻,它不是那些.
我建议大多数时候,你只是让异常冒出来......你真的可以继续进行,好像没有出现任何问题IOException吗?
作为旁注,这很难看:
if(result_true) {
return true;
} else {
return false;
}
Run Code Online (Sandbox Code Playgroud)
只需使用:
return checkresult(result);
Run Code Online (Sandbox Code Playgroud)
(理想情况下,重命名各种方法和变量以遵循Java命名约定.)
我也建议改变它boolean而不是Booelean.
你有一个catch语句的分支既不返回值也不抛出异常.
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Run Code Online (Sandbox Code Playgroud)
那个块需要有某种形式的代码才能返回一些东西,否则该方法将无法正常运行.
| 归档时间: |
|
| 查看次数: |
507 次 |
| 最近记录: |