今天下午我使用 Google 的 Gson 解析 Json 获得了很多乐趣,但我在这里写了这段代码:
public class Fetcher extends AsyncTask<String, Void, JsonReader> {
@Override
protected JsonReader doInBackground(String... urlString) {
try {
URL url = new URL(urlString[0]);
BufferedReader reader = new BufferedReader(new InputStreamReader(url.openStream()));
JsonReader jReader = new JsonReader(reader);
return jReader;
} catch(MalformedURLException malformedEx) {
Log.e("malformed url: ", "here are the details: ", malformedEx);
} catch(IOException ioEx) {
Log.e("IO problem: ", "here are the details: ", ioEx);
} catch(Exception generalEx) {
Log.e("an exception we did not expect: ", "here are the details: ", generalEx);
}
//return statement here
}
}
显然会提示我缺少 return 语句,但我不确定要返回什么,因为我需要将 JsonReader 放在 try block 中,我不能只创建一个空的,因为没有它的构造函数。我不认为我必须要求这个。请出主意。
最佳答案
如果您想捕获异常(确定要吗?),那么 return null
可能是正确的选择。
关于java try catch block 返回什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24598098/