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

public class Incomingdata extends AsyncTask<Void,Void,String>
{

    Socket s  ;
    String input;

    public Incomingdata(Socket socket)
    {   
        super();
        this.s = socket;


    }





    @Override
    protected String doInBackground(Void... params) 
    {Log.i("ddd","Got here before try");
        try
        {   System.out.println("Got here");
            InputStream in = s.getInputStream();
            Scanner r = new Scanner(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 ;
    }

    @Override
    protected void onPostExecute(String input)
    {

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

    }

This is where i am creating the socket

    public class myComs
{
    private static int i = 0;
    private static InetAddress inet;
    private static Socket s;
    private static OutputStream o;
    private static PrintWriter p;

    public static void createSocket(String ipAddress)
    {
        try
        {
            inet = InetAddress.getByName(ipAddress);

            s = new Socket(inet, 2000);
            o = s.getOutputStream();
            p = new PrintWriter(o);
            new Incomingdata(s).execute();
        }
        catch (Exception e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    public static void sending_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"