当我尝试启动我的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它永远不会等待任何帮助会很好,因为我在这个年龄并没有得到快速
谢谢
更新 对,我得到了通过并知道它获得端口,但它仍然没有等待传入的数据刚刚结束
答案 0 :(得分:0)
我注意到你正在使用
System.out.println("123"+input);
用于一种类型的调试,Log.i用于其他类型的调试。也许它正在工作,但你没有正确设置日志视图以查看Log.i。
如果您使用System.out
代替Log.i
,是否可以看到其他消息?
答案 1 :(得分:0)
答案 2 :(得分:0)
我的问题是我创建套接字的次数太多,就像一个布偶所以我只需要创建套接字一次然后调用它多次使用它现在似乎像魅力一样工作
这是我的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();
}
这是我创建套接字的地方
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形式 谢谢你的帮助