4

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<Void,Void,Void>
{
    Socket s  ;
    String input;

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

    @Override
    protected Void doInBackground(Void... params) 
    {
        Log.i("ddd","Got here before try");
        try
        {   
            InputStream in = s.getInputStream();
            Scanner r = new Scanner(in);
            Log.i("Info",s.getPort()+"");
            Log.i("ddd","Got here");
            while(s.isConnected() && r.hasNext())
            {
                String input =r.nextLine(); 
            }
        }
        catch (UnknownHostException e) 
        {
               e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        catch(NoSuchElementException e)
        {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    protected void onPostExecute(Void input)
    {
        System.out.println("123"+input);
    }

}

And here is my log cat

 java.net.Socket.startupSocket(Socket.java:724)
04-25 23:50:26.659: W/System.err(28882):    at java.net.Socket.<init>(Socket.java:263)
04-25 23:50:26.659: W/System.err(28882):    at com.example.handy.myComs.sending_data(myComs.java:23)
04-25 23:50:26.659: W/System.err(28882):    at com.example.handy.MainActivity.readSms(MainActivity.java:277)
04-25 23:50:26.659: W/System.err(28882):    at com.example.handy.MainActivity$1.onClick(MainActivity.java:92)
04-25 23:50:26.659: W/System.err(28882):    at android.view.View.performClick(View.java:2485)
04-25 23:50:26.659: W/System.err(28882):    at android.view.View$PerformClick.run(View.java:9080)
04-25 23:50:26.659: W/System.err(28882):    at android.os.Handler.handleCallback(Handler.java:587)
04-25 23:50:26.659: W/System.err(28882):    at android.os.Handler.dispatchMessage(Handler.java:92)
04-25 23:50:26.659: W/System.err(28882):    at android.os.Looper.loop(Looper.java:130)
04-25 23:50:26.659: W/System.err(28882):    at android.app.ActivityThread.main(ActivityThread.java:3687)
04-25 23:50:26.659: W/System.err(28882):    at java.lang.reflect.Method.invokeNative(Native Method)
04-25 23:50:26.659: W/System.err(28882):    at java.lang.reflect.Method.invoke(Method.java:507)
04-25 23:50:26.659: W/System.err(28882):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-25 23:50:26.659: W/System.err(28882):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-25 23:50:26.659: W/System.err(28882):    at dalvik.system.NativeStart.main(Native Method)
04-25 23:50:26.729: I/System.out(28882): 123null

This is where i am calling it

public class myComs 
{
    private static int i = 0;

    public static void sending_data(String ipAddress, String message)
    {
        try
        {
            InetAddress inet = InetAddress.getByName(ipAddress);
            Socket s = new Socket(inet, 2000);
            OutputStream o = s.getOutputStream();
            PrintWriter p = new PrintWriter(o);
            while(i<1)
            {
                new Incomingdata(s).execute();
                i++;
            }
            p.println(message);
            p.flush();
        }
        catch (UnknownHostException e)
        {
            e.printStackTrace();
        }
        catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

As you can see by the 123null it never waits any help would be great as i am at this with ages and getting no where fast

Thank you

UPDATE Right i got going thru and know its getting the port and but its still not waiting for incoming data its just finishing

t0mm13b
  • 34,087
  • 8
  • 78
  • 110
  • show your calling portion of the code. – minhaz Apr 25 '13 at 23:03
  • is `Log.i("ddd","Got here before try");` showing? – wtsang02 Apr 25 '13 at 23:05
  • 1
    Looks like your code fails before you even call the AsyncTask. `at com.example.handy.myComs.sending_data(myComs.java:23)` when you create the Socket. – SimonSays Apr 25 '13 at 23:10
  • you see the thing is i dont create the socket there and it always seem to throw a warning there every before i tried to this ASYNCTASK. The program was doing everything i asked it too like it was suppose too – Paul Pandaboy Dennehy Apr 25 '13 at 23:11
  • 1
    @PaulPandaboyDennehy Try to use `System.out.println("Got here before try");` in that line. If it shows, everything should make sense. – wtsang02 Apr 25 '13 at 23:16
  • please check if your `InetAddress` object is created properly. Furthermore, Asynctask instance need to be created on UI thread. Are you doing that? – minhaz Apr 25 '13 at 23:17
  • InetAddress is being created correctly but the AsyncTask is not being created on the main UI – Paul Pandaboy Dennehy Apr 25 '13 at 23:44
  • What if you moved the `OutputStream o = s.getOutputStream(); PrintWriter p = new PrintWriter(o);` into the actual `doInBackground`, I suspect there's a blocking in there? What does line 23 actually say from the logcat `at com.example.handy.myComs.sending_data(myComs.java:23)`? **pro-tip** Please format your code to make it easier for us to read! :) – t0mm13b Apr 26 '13 at 00:26

3 Answers3

0

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?

HalR
  • 11,411
  • 5
  • 48
  • 80
0

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.

Community
  • 1
  • 1
Parvaz Bhaskar
  • 1,367
  • 9
  • 29
0

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