首先,让我解释一下,我很清楚这个例外意味着什么,如果你要回答,请仔细阅读这个问题。
我知道当您尝试在网络线程上进行 UI 工作时会出现异常。我正在使用 AsyncTask,但我正在执行任何 UI 操作。
这是抛出错误的 AsyncTask 代码的片段注意 aps 和 adm 是提前声明的,它在 Extracter 初始化中抛出了异常:
do{
try{
HttpPost request = new HttpPost(params[0]);
request.setHeader("Accept", "application/json");
request.setHeader("Content-type", "application/json");
aps.setLastSent(lastSent);
StringEntity filterEntity = new StringEntity(aps.TempParameter()); //Remember this TempParameter is temporary function
request.setEntity(filterEntity);
response = httpClient.execute(request);
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
Extracter extracter = new Extracter(response);
lastSent = extracter.extractLastSent();
adm.addDictionary(extracter.extractDictionary());
}
} catch (ClientProtocolException e) {
//TODO Handle problems..
builder.append("Client protocol exception");
} catch (IOException e) {
//TODO Handle problems..
builder.append(e.toString());
}
}while(lastSent != "done");
return adm.getAlarmObjectList();
线
Extracter extracter = new Extracter(response);
是抛出异常的地方。下面是 Extracter 类的代码:
public class Extracter extends AlarmLoopTestActivity {
private JSONObject jo;
private JSONArray ja;
public Extracter(HttpResponse response){
StringBuilder builder = new StringBuilder();
HttpEntity entity = response.getEntity();
try {
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(content));
String line;
while((line = reader.readLine())!= null){
builder.append(line);
}
} catch (IllegalStateException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String json = builder.toString();
try {
jo = (JSONObject) new JSONTokener(json).nextValue();
ja = jo.getJSONArray("effectNamesDict");
} catch (JSONException e) {
jo = null;
e.printStackTrace();
}
}
我假设 printStackTrace 不被视为 UI 事件。除此之外,我很茫然。我需要对此有新的认识。如果我需要进一步详细说明,请告诉我。