【发布时间】:2015-12-27 08:05:42
【问题描述】:
考虑到下面的代码,当我调用 foo1() 然后 foo2() 时,一切都很好。但是当我将它们一起调用时,我随机得到错误:
- 错误转换结果 java.io.IOException: 尝试读取已关闭的流。
错误有时与 foo1 有时与 foo2 有关。但是,在每个错误之后,相关方法会停止工作,但会在几秒钟后恢复。如何解决问题?
public class myActivity extends Activity {
//some code including onCreate method
private void foo1() {
new Thread(new Runnable() {
@Override
public void run() {
try{
while (true){
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = jsonParser.getJSONFromUrlByGet(url1);
myList.add(jsonObject);
url1 = getNewUrl(url1);
if(!jsonObject.getJSONObject("pagination").has("next_url"))
break;
} catch (Exception exception) {
exception.printStackTrace();
}
}
}).start();
}
private void foo2() {
new Thread(new Runnable() {
@Override
public void run() {
try{
while (true){
JSONParser jsonParser = new JSONParser();
JSONObject jsonObject = jsonParser.getJSONFromUrlByGet(url2);
myList.add(jsonObject);
url2 = getNewUrl(url2);
if(!jsonObject.getJSONObject("pagination").has("next_url"))
break;
} catch (Exception exception) {
exception.printStackTrace();
}
}
}).start();
}
}
public class JSONParser {
static InputStream is = null;
public JSONObject getJSONFromUrlByGet(String url) {
try {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
}
return jObj;
}
这是日志:
12-27 11:48:46.076: I/System.out(20738): got foo1 first pack
12-27 11:48:46.201: I/System.out(20738): got foo1 second pack
12-27 11:48:46.771: I/System.out(20738): got foo1 third pack
12-27 11:48:47.386: I/System.out(20738): got foo2 first pack
12-27 11:48:47.391: I/System.out(20738): E/Buffer Error(6888): Error converting result java.io.IOException: Attempted read on closed stream.
12-27 11:48:47.681: I/System.out(20738): got foo2 second pack
12-27 11:48:47.731: I/System.out(20738): got foo1 fourth pack
12-27 11:48:48.651: I/System.out(20738): got foo1 fifth pack
12-27 11:48:48.656: I/System.out(20738): E/Buffer Error(6888): Error converting result java.io.IOException: Attempted read on closed stream.
12-27 11:48:50.826: I/System.out(20738): got foo2 third pack
12-27 11:48:50.836: I/System.out(20738): got foo2 fourth pack
12-27 11:48:50.856: I/System.out(20738): got foo2 fifth pack
12-27 11:48:50.891: I/System.out(20738): got foo2 sixth pack
12-27 11:48:50.891: I/System.out(20738): got foo2 seventh pack
12-27 11:48:52.686: I/System.out(20738): E/Buffer Error(6888): Error converting result java.io.IOException: Attempted read on closed stream.
12-27 11:48:52.696: I/System.out(20738): got foo1 sixth pack
【问题讨论】:
-
任何时候遇到异常,发布堆栈跟踪