Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>There are some correction that you need to make in the <code>doInBackground()</code>. But before that a small detailing about the <code>AsyncTask</code>. </p> <p><code>AsyncTask</code> is used when you have any non-UI back ground activity to perform. The function <code>onPreExecute()</code> is used to show any UI action (in most cases its showing of a dialog) before you enter the background thread. The function <code>doInBackground()</code> is used to perform the non-ui action (in most cases fetching data from server). While doing the background activity in <code>doInBackground()</code> you may wish to show some progress which you do by using <code>publishProgress()</code> which will internally call the <code>onProgressUpdate()</code> method. On completion of the background activity in <code>doInBackground()</code> you return the <code>result</code> of the activity, if you have any. After you return from the <code>doInBackground()</code> method internally there is call made to the <code>onPostExecute()</code> which will receive the <code>result</code> you have returned in <code>doInBackground()</code> as a parameter. Now <code>onPostExecute()</code> will run on a UI thread and most of the UI action like dismissing of dialog which was shown in <code>onPreExecute()</code>, displaying the <code>result</code> on some UI component etc. happens in this method.</p> <p>Now to the mistake you are doing in you code: You are showing a <code>toast</code> or a <code>notification</code> based on the result of your server data fetch using a function <code>notify</code> but you are still in the background non-ui thread. Now this result should ideally be returned and checked in the <code>onPostExecute()</code> and based on its value you can show the UI component of <code>toast</code>or <code>notification</code>.</p> <p>I hope this explanation helps you in solving your problem.</p> <p><strong>EDIT</strong></p> <p>In your case since you can send the boolean type result variable <code>ok</code> to <code>onPostExecute()</code>. For that you need to make the following changes:</p> <p>in class declaration:</p> <pre><code>public class asyncTaskUpdateProgress extends AsyncTask&lt;Void, Void, Boolean&gt; </code></pre> <p>and </p> <pre><code>protected void onPostExecute(Boolean result){ if (ok) { Toast.makeText(getApplicationContext(), "Connection Susccessfull", Toast.LENGTH_LONG) .show(); } else { Toast.makeText(getApplicationContext(), "Unable to connect", Toast.LENGTH_LONG).show(); notify(getApplicationContext(), true); } } </code></pre> <p>and finally in </p> <pre><code>protected Void doInBackground(Void... arg0) { boolean ok = false; try { SSHTest sshtest = new SSHTest(); ok = sshtest.sshconnect(UserStr, PassStr, IPStr, PortInt); } catch (Exception e) { e.printStackTrace(); Log.i("ERROR HERE", "doInBackground: IOException");} return ok; } </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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      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