Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/187.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android timertask完成时使用Asynctask与服务器通信_Android_Sockets_Android Asynctask_Timertask - Fatal编程技术网

Android timertask完成时使用Asynctask与服务器通信

Android timertask完成时使用Asynctask与服务器通信,android,sockets,android-asynctask,timertask,Android,Sockets,Android Asynctask,Timertask,我正在使用TimerTask获取特定间隔的Wi-Fi扫描结果。我想将扫描结果发送到服务器。因此我使用AsyncTask发送结果。当我从TimerTask内部调用AsyncTask时,我的应用程序崩溃。谁能告诉我为什么会这样?在我的代码中,将结果发送到服务器的最佳方式是什么 这是我的密码: public class ServerComm extends AsyncTask<String, Void, String>{ int port=9999; String IP="192.168

我正在使用TimerTask获取特定间隔的Wi-Fi扫描结果。我想将扫描结果发送到服务器。因此我使用AsyncTask发送结果。当我从TimerTask内部调用AsyncTask时,我的应用程序崩溃。谁能告诉我为什么会这样?在我的代码中,将结果发送到服务器的最佳方式是什么

这是我的密码:

public class ServerComm extends AsyncTask<String, Void, String>{

int port=9999;
String IP="192.168.2.100";
BufferedReader input;

@Override
protected String doInBackground(String... scanRes) {
    // TODO Auto-generated method stub
    Socket socket;
    String loc="";
    FileWriter writer;
    File sock=new File(Environment.getExternalStorageDirectory()+"/network.txt");
     try {
            writer = new FileWriter(sock, true);
            writer.append("in async task.\n");
            writer.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    try {
        InetAddress serverAddr = InetAddress.getByName(IP);
        socket = new Socket(serverAddr, port);// socket is created

        input = new BufferedReader(new InputStreamReader(socket.getInputStream()));
        Log.d("ser","socket");

        // now send 
        PrintWriter out = new PrintWriter(new BufferedWriter(new OutputStreamWriter(socket.getOutputStream())),true);       
        out.println(scanRes);                    
        // get the reply
        loc = input.readLine();
        //close
        socket.close();
        return loc;

    } catch (UnknownHostException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return loc;



}


 @Override
    protected void onPostExecute(String result) {
     // draw the new location on the map
    }
公共类ServerComm扩展异步任务{
int端口=9999;
字符串IP=“192.168.2.100”;
缓冲读取器输入;
@凌驾
受保护的字符串背景(字符串…扫描){
//TODO自动生成的方法存根
插座;
字符串loc=”“;
文件编写器;
File sock=新文件(Environment.getExternalStorageDirectory()+“/network.txt”);
试一试{
writer=newfilewriter(sock,true);
append(“在异步任务中。\n”);
writer.close();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
试一试{
inetAddressServerAddr=InetAddress.getByName(IP);
套接字=新套接字(serverAddr,port);//已创建套接字
输入=新的BufferedReader(新的InputStreamReader(socket.getInputStream());
Log.d(“ser”、“socket”);
//现在发送
PrintWriter out=新的PrintWriter(新的BufferedWriter(新的OutputStreamWriter(socket.getOutputStream()),true);
out.println(扫描);
//得到答复
loc=input.readLine();
//接近
socket.close();
返回loc;
}捕获(未知后异常e){
//TODO自动生成的捕捉块
e、 printStackTrace();
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
返回loc;
}
@凌驾
受保护的void onPostExecute(字符串结果){
//在地图上画出新的位置
}
这是TimerTask的代码:

public void Locate()
{

    if(isScanning)// if a previous scan is running cancel it
{

    timer.cancel();
}
else
{

    timer.schedule(new TimerTask() { // start new scanner

        @Override
        public void run() {
            // TODO Auto-generated method stub

            if(counter==numOfScans)
            {

                try {

                    writer = new FileWriter(file, true);
                    writer.append(RSS+" \n");
                    writer.append("Finished Collecting RSSIs.\n");
                    writer.close();

            new ServerComm().execute(RSS);  


                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


                timer.cancel();

            }
            else // not finished
            {
                isScanning=true;


                if(result!=null)
                {

                if(result.size()!=0)
                {

                    for(int i=0;i< result.size();i++)
                    {
                         RSS=RSS+result.get(i).BSSID+" "+result.get(i).level+" ";


                    }//end for


                    counter++;

                    } // end of if
                }//end of else
                }

        }
    }, 0,Interval);
}



    //get location
}
public void Locate()
{
if(isScanning)//如果上一次扫描正在运行,请取消它
{
timer.cancel();
}
其他的
{
timer.schedule(新TimerTask(){//启动新扫描仪
@凌驾
公开募捐{
//TODO自动生成的方法存根
如果(计数器==numoScans)
{
试一试{
writer=newfilewriter(file,true);
writer.append(RSS+“\n”);
writer.append(“已完成收集RSSIs。\n”);
writer.close();
新建ServerComm().execute(RSS);
}捕获(IOE异常){
//TODO自动生成的捕捉块
e、 printStackTrace();
}
timer.cancel();
}
否则//
{
IsScaning=真;
如果(结果!=null)
{
如果(result.size()!=0)
{
对于(int i=0;i
我解决了这个问题,所以我会发布答案,以防其他人遇到同样的问题。 问题是不应从
TimerTask
调用
AsyncTask
。应使用处理程序运行
AsyncTask
,如下所示:

    Handler h = new Handler(Looper.getMainLooper());
h.post(new Runnable() {
  public void run() {
    new ServerComm().execute(RSS);
  }
});

这就是问题的解决方案。

应用程序是如何崩溃的?logcat中是否存在异常?@Nicolasdernoux我直接在手机上运行它,因为我在应用程序中使用Wifi,所以我没有任何logcat消息。这就是为什么您使用FileWriter作为自己的日志记录器。那么在asynctask中它能走多远?@greenapps它一旦被调用就会崩溃,它在AsynTask中不会做任何事情,如果你实例化并执行()你的任务,比如说从button onclick处理程序执行,它也不会崩溃?