First off, let me explain that I am well aware of what this exception means, please read the question thoroughly if you’re going to answer.
I know that the exception arises when you try to do UI work on a network thread. I am using AsyncTask but I’m doing any UI operations.
Here is the snippet from AsyncTask code where the error is being thrown
NOTE aps and adm are declared ahead of time
Its in the Extracter initialization that the exception is being thrown:
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();
The line
Extracter extracter = new Extracter(response);
is where the exception is thrown.
Here is the code for the Extracter class:
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();
}
}
I’m assuming printingStackTrace is not considered a UI event. Other then that, I’m at a loss. I need to get new eyes on this. Let me know if I need to elaborate anything further.
Your
Extracteris extending a classAlarmLoopTestActivitygoing from the name this is an android Activity and so it probably has some UI interaction methods in there!