AsyncTask, doInBackground never starts in android

When I try to run my doinbackground to wait for an incoming message, it never starts through it, just skips 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 cat magazine

  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 

That's where I call him

 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 from 123null, he never expects any help, it will be great, because I am in this with age and do not get fast pace

thanks

UPDATE That's right, I went through and found out that it is receiving a port, but it still does not wait for incoming data to simply end

+4
source share
3 answers

My problem was that I created the socket too many times, like muppet, so I just had to create the socket once and then call it many times, and now it works 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 create 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 of my / get set, which I create from input in a text box thanks for the help

0
source

I noticed that you are using

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

for one type of debugging and Log.i for the rest. It may work, but you are viewing a view that is not configured correctly to see Log.i.

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

0
source

Do you use multiple asintets at the same time? Because your code looks fine. The Async Task model changes after 2.3, and it disables them for parallel operation. Check out this and this SO question.

0
source

All Articles