考虑下面的代码,当我调用foo1()然后调用foo2()时,一切都很好。但是当我把它们召集在一起时,我随机得到错误:
错误有时与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
答案 0 :(得分:0)
所以问题是你正在使用线程,它会随机执行,因此有时你会在foo1中得到错误,有时会在foo2中得到错误。
在run()方法中,由于while(true){}
因此,假设您的foo1开始执行,它进入while循环,并调用getJSONFromUrlByGet(),后者将读取直到数据结束然后关闭流。见is.close()
但是由于静态实例,'is'在类的所有实例之间共享。因此,当再次调用getJSONFromUrlByGet()时,流已经关闭,因此异常。