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

  • Home
  • SEARCH
  • 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 8920925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:24:11+00:00 2026-06-15T06:24:11+00:00

I am having some trouble using AsyncTask as I have never come across it

  • 0

I am having some trouble using AsyncTask as I have never come across it before and have no clue what I am doing with it.

Basically I am getting a force close because I am trying to run the connection on a main class. Could someone possibly help me with adding AsyncTask into the code:

package com.smarte.smartipcontrol;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class IPControl extends Activity {

  private Socket socket;
  private String serverIpAddress;
  private static final int REDIRECTED_SERVERPORT = 32;
  public PrintWriter out;
  public BufferedReader in ;

    @Override
      public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Get the message from the intent
        Intent intent = getIntent();
        serverIpAddress = intent.getStringExtra(IPEntry.ACTUALSMARTIP);
        createConnection();

      }

  public void getModel(View view) {
    try {
      out.println("[m\r\n");
      //System.out.print("root\r\n");
      while (! in .ready());
      String textStatus = readBuffer();

    } catch (IOException e) {}
  }

  public void createConnection() {
    try {
      InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
      socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
    } catch (UnknownHostException e1) {
      e1.printStackTrace();
    } catch (IOException e1) {
      e1.printStackTrace();
    }
    try {
      out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true); in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
      while (! in .ready());
      readBuffer();
      out.println("root\r\n");
      //System.out.print("root\r\n");
      while (! in .ready());
      readBuffer();
      out.println("root\r\n");
      //System.out.print("root\r\n");
      while (! in .ready());
      readBuffer();
    } catch (IOException e) {}


    //R.id.textStatus
  }

  private String readBuffer() throws IOException {
    String msg = "";

    while ( in .ready()) {
      msg = msg + (char) in .read();
    }
    //System.out.print(msg);
    if (msg.indexOf("SNX_COM> ") != -1) return msg.substring(0, msg.indexOf("SNX_COM> "));
    else return msg;
  }

}
  • 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-15T06:24:12+00:00Added an answer on June 15, 2026 at 6:24 am

    Just one quick example of how it migtht look like:

    public class IPControl extends Activity {
        private ProgressDialog pd = null;
        private String data = null;
        private Socket socket;
        private String serverIpAddress;
        private static final int REDIRECTED_SERVERPORT = 32;
        public PrintWriter out;
        public BufferedReader in ;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            setContentView(R.layout.main);
    
            try{   
                this.pd = ProgressDialog.show(this, "Loading..", "Please Wait...", true, false);
                new AsyncAction().execute();
    
            }catch (Exception e) {
                e.printStackTrace();
            }
        }
    
        private class AsyncAction extends AsyncTask<String, Void, String> {
            protected Void doInBackground(String... args) { 
                try {
                    InetAddress serverAddr = InetAddress.getByName(serverIpAddress);
                    socket = new Socket(serverAddr, REDIRECTED_SERVERPORT);
                } catch (UnknownHostException e1) {
                    e1.printStackTrace();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                try {
                    out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())), true);
                    in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
                    while (! in.ready());
                    readBuffer();
                    out.println("root\r\n");
                    //System.out.print("root\r\n");
                    while (! in .ready());
                    readBuffer();
                    out.println("root\r\n");
                    //System.out.print("root\r\n");
                    while (! in .ready());
                    String msg = "";
    
                    while (in.ready()) {
                        msg = msg + (char) in .read();
                    }
                } catch (IOException e) {}
    
            return null;//returns what you want to pass to the onPostExecute()
        }
    
        protected void onPostExecute(String result) {
            //resultis the data returned from doInbackground
            IPControl.this.data = result;
    
            if (IPControl.this.pd != null) {
                IPControl.this.pd.dismiss();
            }
        } 
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having some trouble loading external javascripts using google app engine. I basically have
I'm having some trouble using constraints correctly. I have three tables, 'item', 'store' and
Basically having some trouble with using Hover to hide or show an item. The
I'm having some trouble using sessions with php.. I think I have figured out
I am having some trouble with using h:selectOneRadio . I have a list of
I'm having some trouble with navigation when using Facelets. I have my master template
I am having some trouble with getting a certain plot layout when using the
I'm having some trouble using mySQL and Spring JDBCTemplate. I have an INSERT ...
I am having some trouble getting a button clicked after submitting a form using
I am having some trouble using the GTK Expander widget. I have several expanders

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.