When my users click the login button I want to hit my webservice. I have the following code to do so.
public void onClick(final View view) {
String orgKey = inputCompany.getText().toString();
new getAppInfo().execute("http://example.webservice.com");
Here is my getAppInfo
private class getAppInfo extends AsyncTask<String, Void, String> {
/** The system calls this to perform work in a worker thread and
* delivers it the parameters given to AsyncTask.execute() */
@Override
protected String doInBackground(String... urls) {
String xml = null;
try {
// defaultHttpClient
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(urls[0]);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
xml = EntityUtils.toString(httpEntity);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
// return XML
return xml;
}
/** The system calls this to perform work in the UI thread and delivers
* the result from doInBackground() */
@Override
protected void onPostExecute(String result) {
Document doc = GetDomElement(result); // getting DOM element
NodeList nl = doc.getElementsByTagName("details");
getChildElements(nl);
}
The doBackground is running but the onPostExecute is not. I have moved it out of the on click and it has worked but I need it inside the onClick.
How can I get it to run the onPostExecute inside my onClick?
The syntax is correct you might be getting an exception in the background process, stopping the background thread. put a log statement at the end of your doInBackground or add a catch (Throwable t) to your try.
Have faith – it WILL be called as long as doInBackground completes successfully.
As an aside, you should do your DOM parse in the background as well – at the moment you are doing it in the UI thread which may cause ANR popups.