Note that there are some explanatory texts on larger screens.

plurals
  1. POHow and where to call my Task when extending AsyncTask
    primarykey
    data
    text
    <p>I am having difficulties understanding AsyncTask, even after reading everything about it on Developer.Android. I am looking for some insight in how I should proceed. This is the situation :</p> <p>I have an Activity which, on an onClick event calls the LoginCheck() method of an underlying LoginController class. The LoginController class then proceeds to fetch whatever information is nescesarry from a UserInfo class or from the Activity(User and Password) and creates an instance of a RestClient which then makes the call to the web service and attempts to log in. RestClient has a private class CallServiceTask that extends AsyncTask. </p> <p>I have a few design problems here that I hope you can be of assistance with. </p> <ul> <li>Am I doing it right? Is this a proper way to make sure that any calls to the web service are being done asynchronously? </li> <li>How do use onProgressUpdate or whatever to notify the user that the application is in the process of logging in?</li> <li>How would I go about getting the data that is saved in DoinBackground() ?</li> </ul> <p>Below you'll find snippets of the project in question :</p> <p><em>RestClient</em></p> <pre><code>// From the constructor... rtnData = new Object[]{ new JSONObject() , Boolean.TRUE }; public void ExecuteCall(RequestMethod method) throws Exception { Object[] parameters = new Object[]{ new HttpGet() , new String("") }; switch(method) { case GET: { //add parameters String combinedParams = ""; if(!params.isEmpty()){ combinedParams += "?"; for(NameValuePair p : params) { String paramString = p.getName() + "=" + URLEncoder.encode(p.getValue()); if(combinedParams.length() &gt; 1) { combinedParams += "&amp;" + paramString; } else { combinedParams += paramString; } } } HttpGet request = new HttpGet(url + combinedParams); //add headers for(NameValuePair h : headers) { request.addHeader(h.getName(), h.getValue()); } parameters[0] = request; parameters[1] = url; new CallServiceTask().execute(request, url); jsonData = ((JSONObject) rtnData[0]).optJSONObject("data"); connError = (Boolean) rtnData[1]; break; } case POST: .... } } private Object[] executeRequest(HttpUriRequest request, String url) { HttpClient client = new DefaultHttpClient(); client = getNewHttpClient(); HttpResponse httpResponse; try { httpResponse = client.execute(request); HttpEntity entity = httpResponse.getEntity(); if (entity != null) { InputStream instream = entity.getContent(); String response = convertStreamToString(instream); try { rtnData[0] = new JSONObject(response); rtnData[1] = false; } catch (JSONException e1) { rtnData[1] = true; e1.printStackTrace(); } // Closing the input stream will trigger connection release instream.close(); } } catch (ClientProtocolException e) { client.getConnectionManager().shutdown(); e.printStackTrace(); } catch (IOException e) { client.getConnectionManager().shutdown(); e.printStackTrace(); } return rtnData; } </code></pre> <p><em>CallServiceTask</em></p> <pre><code> private class CallServiceTask extends AsyncTask&lt;Object, Void, Object[]&gt; { protected Object[] doInBackground(Object... params) { HttpUriRequest req = (HttpUriRequest) params[0]; String url = (String) params[1]; return executeRequest(req, url); } @Override protected void onPostExecute(Object[] result) { rtnData = result; } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload