Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8132151
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:16:37+00:00 2026-06-06T09:16:37+00:00

First of all, i have to state that i am new to Java in

  • 0

First of all, i have to state that i am new to Java in general & Android. I do however have the basics, or at least getting there.

The Purpose of my Application: At the company, we have a Remote server that we SSH to, and do some work. At some times, the server is unreachable and therefore disrupts our work.

My application is suppose to do the following:

1- Using Jsch, i SSH to the server, if there is a response, then, i will attempt again in 15 minutes, if there is no response, i want to notify.

i have successfully done the above in non android version of Java, and was able to do it in Android version, however on the main thread, thus i cannot update anything on the UI. In essence the Progress Bar..
In the regular version, the UI freezes, and in the AsyncTask version provided below. i get an exception as soon as i hit the button

Below is the code i am using, to be honest, i read all over that the best solution is AsyncTask, but since i am new to that, i am not sure were my wrong is. I honestly assume its may be in the AsyncTask and AsyncTask .

I am not sure what to use there…
Below is my code, hopefully someone can point out my mistake.

package com.example.myapp;

import java.io.IOException;

import android.app.Activity;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.HandlerThread;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.Toast;
import android.os.Handler;

public class VedasServerMonitorActivity extends Activity {
    /** Called when the activity is first created. */


    Button button;
    EditText IP;
    EditText UserName;
    EditText Password;
    EditText Port;
    ProgressBar progressBar1;

    String UserStr;
    String PassStr;
    String IPStr;
    int PortInt;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        button = (Button) findViewById(R.id.button1);
        IP = (EditText) findViewById(R.id.serverIp);
        UserName = (EditText) findViewById(R.id.userName);
        Password = (EditText) findViewById(R.id.password);
        Port = (EditText) findViewById(R.id.port);
        progressBar1 = (ProgressBar) findViewById(R.id.progressBar1);

        StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                .permitAll().build();

        StrictMode.setThreadPolicy(policy);

        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {



                new asyncTaskUpdateProgress().execute();

            }

        });

    }

    public class asyncTaskUpdateProgress extends AsyncTask<Void, Void, Void> {



        @Override
        protected void onPostExecute(Void result) {
            // TODO Auto-generated method stub







        }

        @Override
        protected void onPreExecute() {
            // TODO Auto-generated method stub
            progressBar1.setVisibility(View.VISIBLE);
            UserStr = UserName.getText().toString();
            PassStr = Password.getText().toString();
            IPStr = IP.getText().toString();
            PortInt = Integer.parseInt(Port.getText().toString());

            button.setClickable(true);
            progressBar1.setVisibility(View.INVISIBLE);



        }

        @Override
        protected void onProgressUpdate(Void... values) {
            // TODO Auto-generated method stub
            progressBar1.setVisibility(View.INVISIBLE);

        }

        @Override
        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");}
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);

}
return null;


        }


        protected void notify(Context context, Boolean on) {
            NotificationManager nm = (NotificationManager) context
                    .getSystemService(Context.NOTIFICATION_SERVICE);
            ComponentName comp = new ComponentName(context.getPackageName(),
                    getClass().getName());
            Intent intent = new Intent().setComponent(comp);
            PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,
                    intent, Intent.FLAG_ACTIVITY_NEW_TASK);
            Notification n = new Notification(R.drawable.warning, "Message",
                    System.currentTimeMillis());
            n.setLatestEventInfo(context, "Vedas Server Monitor",
                    "Port Un-rechable", pendingIntent);
            nm.notify(22, n);
        }

    }

}

android× 194760

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:16:38+00:00Added an answer on June 6, 2026 at 9:16 am

    There are some correction that you need to make in the doInBackground(). But before that a small detailing about the AsyncTask.

    AsyncTask is used when you have any non-UI back ground activity to perform. The function onPreExecute() is used to show any UI action
    (in most cases its showing of a dialog) before you enter the background thread. The function doInBackground() is used to perform the non-ui action (in most cases fetching data from server). While doing the background activity in doInBackground() you may wish to show some progress which you do by using publishProgress() which will internally call the onProgressUpdate() method. On completion of the background activity in doInBackground() you return the result of the activity, if you have any. After you return from the doInBackground() method internally there is call made to the onPostExecute() which will receive the result you have returned in doInBackground() as a parameter. Now onPostExecute() will run on a UI thread and most of the UI action like dismissing of dialog which was shown in onPreExecute(), displaying the result on some UI component etc. happens in this method.

    Now to the mistake you are doing in you code:
    You are showing a toast or a notification based on the result of your server data fetch using a function notify but you are still in the background non-ui thread. Now this result should ideally be returned and checked in the onPostExecute() and based on its value you can show the UI component of toastor notification.

    I hope this explanation helps you in solving your problem.

    EDIT

    In your case since you can send the boolean type result variable ok to onPostExecute(). For that you need to make the following changes:

    in class declaration:

    public class asyncTaskUpdateProgress extends AsyncTask<Void, Void, Boolean>
    

    and

    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);
    
    }
    }
    

    and finally in

    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;
    
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

First of all, let me just state that I'm new to .NET so I
First of all I have to say I am new to iOS development. My
First of all i have frames on my SD Card and frames are there
First of all i want to say that i have searched each and every
First of all I would like to state that I am a beginner at
I have an android app that is setup to start a Java activity (call
First of all, I'll state that this is a university assignment so I'm not
I have a problem with my Android app. I have three Activities that all
First of all I have many Django instances setup and running like this. In
First of all I have searched many times in this forum but none is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.