4

当我尝试启动我的 doinbackground 以等待传入的消息时,它从未真正运行过它只是跳过它

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);
    }

}

这是我的原木猫

 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

这就是我所说的

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();
        }
    }
}

正如您在 123null 中看到的那样,它永远不会等待任何帮助,因为我在这方面已经很长时间了,而且速度很快

谢谢

更新 是的,我通过并知道它正在获取端口,但它仍然没有等待传入数据它刚刚完成

4

3 回答 3

0

我的问题是我像布偶一样多次创建套接字,所以我只需要创建一次套接字,然后多次调用它,现在它看起来就像一个魅力

这是我的异步任务

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();

    }

这是我创建套接字的地方

    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();

    }

}

然后在我的 MainActivity

   myComs.createSocket(getMyIp());

获取我的 Myip 表单我从文本字段中的输入创建的 set/get 谢谢你的帮助

于 2013-04-26T15:59:01.710 回答
0

您是否同时使用多个异步任务?因为您的代码似乎没问题。Async Task 模型在 2.3 之后发生了变化,它禁止它们并行运行。请检查这个这个SO 问题。

于 2013-04-26T04:35:09.227 回答
0

我注意到您正在使用

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

用于一种类型的调试,而 Log.i 用于其他类型的调试。也许它正在工作,但是您的日志记录视图没有正确设置以查看 Log.i。

如果您使用 aSystem.out而不是Log.i,您能看到其他消息吗?

于 2013-04-25T23:14:21.133 回答