当我尝试启动我的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,它从未等待任何帮助。因为我已经花费了很长时间却没有任何进展,所以希望您能提供帮助。
谢谢!
更新 好的,我现在知道它正在获取端口,但仍然没有等待传入数据,而是直接结束了。
Log.i("ddd","Got here before try");
显示了吗? - wtsang02at com.example.handy.myComs.sending_data(myComs.java:23)
。 - SimonSaysSystem.out.println("Got here before try");
。如果它显示出来,一切都应该很清楚了。 - wtsang02