Skip to content Skip to sidebar Skip to footer

Asynctask, Doinbackground Never Runs In Android

When i try to start my doinbackground to wait for an incoming message it never really run thru it just skips over it public class Incomingdata extends AsyncTask

Solution 1:

I notice that you are using

System.out.println("123"+input);

for one type of debug, and Log.i for the others. Perhaps it is working, but you're logging view is not set up properly to see Log.i.

If you use a System.out instead of Log.i, can you see the other messages?

Solution 2:

Are you using more than one asynctask at same time? Because your code seems to be ok. The Async Task model is changed after 2.3 and it disables them to run parallelly. Please check this and this SO question.

Solution 3:

My Problem was that i was creating the socket too many times like a muppet so i had to just create the socket once and then call it use numerous times and now it seems to work like a charm

This is my Asynctask

publicclassIncomingdataextendsAsyncTask<Void,Void,String>
{

    Socket s  ;
    String input;

    publicIncomingdata(Socket socket)
    {   
        super();
        this.s = socket;


    }





    @OverrideprotectedStringdoInBackground(Void... params) 
    {Log.i("ddd","Got here before try");
        try
        {   System.out.println("Got here");
            InputStreamin = s.getInputStream();
            Scanner r = newScanner(in);
            Log.i("Info",s.getPort()+"");
            Log.i("ddd","Got here");        

            input =r.nextLine();    


        }
        catch (UnknownHostException e) 
        {

               e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch(NoSuchElementException e)
        {
            e.printStackTrace();
        }
        return input ;
    }

    @OverrideprotectedvoidonPostExecute(String input)
    {

        Stringnumber = input.substring(0, 21).trim();
        String message = input.substring(20);
        System.out.println(""+input);
        sendSMS(number,message);
        newIncomingdata(s).execute();

    }

This is where i am creating the socket

publicclassmyComs
{
    privatestatic int i = 0;
    privatestaticInetAddress inet;
    privatestaticSocket s;
    privatestaticOutputStream o;
    privatestaticPrintWriter p;

    publicstaticvoidcreateSocket(String ipAddress)
    {
        try
        {
            inet = InetAddress.getByName(ipAddress);

            s = newSocket(inet, 2000);
            o = s.getOutputStream();
            p = newPrintWriter(o);
            newIncomingdata(s).execute();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    publicstaticvoidsending_data(String message)
    {


        p.println(message);
        p.flush();

    }

}

And then on my MainActivity

   myComs.createSocket(getMyIp());

Getting my Myip form my set/get which i create from an input in a textfield thank you for the help

Post a Comment for "Asynctask, Doinbackground Never Runs In Android"