Sean
Sean

Reputation: 573

Weird NetworkOnMainThread exception

I'm new to android, please halp.

This is a really simple scenario, there's an actionbar, and when a tab is selected, I need to send an http request to get an article list, when the response arrives, update a listview and here's where I got this exception.

I'm aware that I should isolate network operations from UI thread, so I implement the network functions in an AsyncTask class.

The most weird part is, before I try to get the article list, I have actually called a network function once to log in, and that just works fine, no exceptions! And guess what, if I replace the get article list call with log in call, it works fine, too. All the code are just the same, except the arguments differ.

Here's how I do it, since too many modules will use the network module, I use a static wrapper:

UI -> Static Factory -> New AsyncTask -> Response arrives -> Call Static callback function in Static Factory -> Call callback function in UI

AsyncTask doInBackground

@Override
protected HttpResponse doInBackground(String... params) {
    // TODO Auto-generated method stub
    String uri = params[0];
    HttpGet get = new HttpGet(uri);
    try {
        return _client.execute(get);
    }
    catch(Exception e) {
        e.printStackTrace();
        return null;
    }   
}

AsyncTask post execute

    @Override
    protected void onPostExecute(HttpResponse result) {
        //Do something with result
        if (result != null) {
            try {
                JSONObject json = FormJsonFromResponse();
                Command.OnTaskComplete(json);
            } catch (Exception e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }

    }

static wrapper code:

@Override
    public static void OnTaskComplete(JSONObject json) {
        // TODO Auto-generated method stub
        if(_callback != null) {
            _callback.OnCommandComplete(json);
        }
    }

Here's how I call asynctask in static wrapper:

public static void LogIn(String user, String pass) {
    new NetworkTask().execute(_uriPrefix + login, _user, _pass);
}

Upvotes: 1

Views: 118

Answers (1)

323go
323go

Reputation: 14274

Revise your AsyncTask following this pattern:

@Override
protected JSONObject doInBackground(String... params) {
    // TODO Auto-generated method stub
    String uri = params[0];
    HttpGet get = new HttpGet(uri);
    try {
        HttpResponse response = _client.execute(get);
        return FormJsonFromResponse();
    }
    catch(Exception e) {
        e.printStackTrace();
        return null;
    }   
}

AsyncTask post execute

@Override
protected void onPostExecute(JSONObject result) {
    //Do something with result
    if (result != null) {
        try {
            Command.OnTaskComplete( result );
        } catch (Exception e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

}

Upvotes: 1

Related Questions