I am using Kevin Sawicki's library for HTTP requests in my Android application. The actual call to the library methods for making the request is made in a class file (it's not called from an activity) called TemplateHelper
. The method that calls the HttpRequest library in my TemplateHelper class looks like this
public static JSONObject GetTemplates() {
try {
return new JSONObject(HttpRequest.get("http://myapi.mycompany.com/templates").body());
} catch (HttpRequestException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}
From my activity i then call TemplateHelper.GetTemplates()
to get the data. However, when i do this Android throws the exception android.os.NetworkOnMainThreadException
.
A quick Google search shows me the code for running the HTTPRequest on a separate thread. But if i run the code in a separate thread, how can i return the result to the main thread?