Java 为什么在使用BufferedReader打开URL流时捕获UnknownHostException会使我的应用程序崩溃?
我正在编写一个Android应用程序,它连接到一个网站并以JSON的形式检索搜索结果。此函数发生在AsyncTask中,AsyncTask被设置为与UI分离的流。我需要处理连接中断/不存在/太隐蔽的情况。我需要处理这种情况,以便向用户显示AlertDialog,让他们知道连接不好。我看到一些帖子建议为URLConnection设置超时参数,但我现在不使用URLConnection 现在,当我有一个数据连接时,这个函数可以完美地执行,但当没有连接时就不是这样了。当我运行emulator并禁用PC的internet连接时,运行该函数会显示一条“强制关闭”消息,并产生一个UnknownHostException。我正在捕获此异常,但我的应用程序仍然崩溃 我还需要处理一个无法找到缩略图的情况,这会产生FileNotFoundException 请告诉我该怎么做。谢谢Java 为什么在使用BufferedReader打开URL流时捕获UnknownHostException会使我的应用程序崩溃?,java,android,exception-handling,try-catch,bufferedreader,Java,Android,Exception Handling,Try Catch,Bufferedreader,我正在编写一个Android应用程序,它连接到一个网站并以JSON的形式检索搜索结果。此函数发生在AsyncTask中,AsyncTask被设置为与UI分离的流。我需要处理连接中断/不存在/太隐蔽的情况。我需要处理这种情况,以便向用户显示AlertDialog,让他们知道连接不好。我看到一些帖子建议为URLConnection设置超时参数,但我现在不使用URLConnection 现在,当我有一个数据连接时,这个函数可以完美地执行,但当没有连接时就不是这样了。当我运行emulator并禁用PC的
@Override
protected HashMap<String, String> doInBackground(Object... params) {
InputStream imageInput = null;
FileOutputStream imageOutput = null;
try {
URL url = new URL("http://www.samplewebsite.com/" + mProductID);
BufferedReader reader =
new BufferedReader(new InputStreamReader(url.openStream()));
String jsonOutput = "";
String temp = "";
while ((temp = reader.readLine()) != null) {
jsonOutput += temp;
}
JSONObject json = new JSONObject(jsonOutput);
// ... Do some JSON parsing and save values to HashMap
String filename = mProductID + "-thumbnail.jpg";
URL thumbnailURL = new URL("http://www.samplewebsite.com/img/" + mProductID + ".jpg");
imageInput = thumbnailURL.openConnection().getInputStream();
imageOutput = mContext.openFileOutput(outputName, Context.MODE_PRIVATE);
int read;
byte[] data = new byte[1024];
while ((read = imageInput.read(data)) != -1) {
imageOutput.write(data, 0, read);
}
reader.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
finally {
try {
imageOutput.close();
imageInput.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return mProductInfoHashMap;
}
@覆盖
受保护的HashMap doInBackground(对象…参数){
InputStream imageInput=null;
FileOutputStream imageOutput=null;
试试{
URL=新URL(“http://www.samplewebsite.com/“+mProductID);
BufferedReader读取器=
新的BufferedReader(新的InputStreamReader(url.openStream());
字符串jsonOutput=“”;
字符串temp=“”;
而((temp=reader.readLine())!=null){
jsonOutput+=温度;
}
JSONObject json=新的JSONObject(jsonOutput);
//…执行一些JSON解析并将值保存到HashMap
字符串filename=mProductID+“-thumbnail.jpg”;
URL thumbnailURL=新URL(“http://www.samplewebsite.com/img/“+mProductID+”.jpg”);
imageInput=thumbnailURL.openConnection().getInputStream();
imageOutput=mContext.openFileOutput(outputName,Context.MODE\u PRIVATE);
int-read;
字节[]数据=新字节[1024];
而((读取=图像输入。读取(数据))!=-1){
imageOutput.write(数据,0,读取);
}
reader.close();
}捕获(格式错误){
e、 printStackTrace();
}catch(filenotfounde异常){
e、 printStackTrace();
}捕获(未知后异常e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}捕获(JSONException e){
e、 printStackTrace();
}
最后{
试一试{
imageOutput.close();
imageInput.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回MPProductInfoHashMap;
}
您的问题不是未知成本,而是在finally块中,您没有正确关闭打开的资源(不是导致问题的原因,但实际上是您做错了),也没有捕获所有可能的异常(这就是为什么您的代码没有按预期工作)。对于要关闭的每个资源,最好使用try{{uu.close();}catch({uu异常e){…}
。这样,如果您的close()
调用中有一个异常,其他资源仍然会关闭,否则您只是让它们保持打开状态,然后直接跳入finally
中的catch
块
然而,问题的真正原因是,在获得初始异常然后进入finally
块之前,您的资源没有得到实例化。所以,它们仍然是空的。您应该捕获的异常以及IOException
是NullPointerException
希望这对您有所帮助。好的,我最初遇到的问题是,在添加FileNotFoundException和UnknownHostException时,它们是无法访问的,因为我已经有了IOException(它们都是从IOException继承的)。我现在已经将这两个异常都向上移动了,所以首先声明它们。然而,我仍然有问题。我的stacktrace显示一个未知的后异常,即使我捕捉到了它,我的应用程序也显示“强制关闭”。