提问者:小点点

在时间任务完成时使用Asynctask与服务器通信


我正在使用TimerTask获取特定间隔的Wi-Fi扫描结果。我想将扫描结果发送到server.So我使用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
    }

这是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
}

共1个答案

匿名用户

我解决了这个问题,所以我发布了答案,以防其他人遇到同样的问题。问题是不应该从TimerTask调用AsyncTask。应该使用处理程序来运行AsyncTask像这样:

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

这是解决这个问题的方法。