0

I'm using a TimerTask to get the result of a Wi-Fi scan on a specific interval. I want to send the scanning results to a server.So I used AsyncTask to send the results. When I call the AsyncTask from inside the TimerTask my application crashes. Can anyone tell me why this is happening? Also in my code what is the best way to send the result to a server?

Here is my code:

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
    }

This is the code of the 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
}
Alaa
  • 539
  • 3
  • 8
  • 29
  • 1
    How does you application crashes ? Do you have an exception in logcat ? – Nicolas Defranoux Apr 30 '14 at 11:53
  • @NicolasDefranoux I run it directly on my phone because I'm using Wifi in the application so I don't have any logcat message – Alaa Apr 30 '14 at 12:52
  • Thats why you use FileWriter as your own logger. Well how far does it come in the asynctask? – greenapps Apr 30 '14 at 15:28
  • @greenapps it crashes once its called, it does't do anything in the asyntask – Alaa Apr 30 '14 at 15:49
  • And it does not crash if you instantiate and excecute() your task say from a button onclick handler? – greenapps Apr 30 '14 at 15:56
  • I didn't try that, but I copied the asynctask code into another project and called from onCreate and it worked – Alaa Apr 30 '14 at 15:59
  • From where do you call Locate()? Locate() is a member of? Add a function like Locate() in witch you only start the asynctask. – greenapps Apr 30 '14 at 18:11
  • @greenapps locate is called when an item from the action bar is clicked I added a function that only starts the asynctask but the problem was not solved – Alaa Apr 30 '14 at 18:30
  • You said that you did it on your phone using wifi. But if you use eclipse and connected the phone with usb and eclipse installed the apk and started it you see the logcat with the stacktraces. – greenapps Apr 30 '14 at 18:40
  • If the problem is not solved then you know now that you only have to look at your asynctask. Remove sock and all with FileWriter there. – greenapps Apr 30 '14 at 18:45
  • Try with out.println(scanRes[0]); – greenapps Apr 30 '14 at 19:00
  • I'm using eclipse under linux and my phone is samsung is that doable? – Alaa Apr 30 '14 at 22:51

1 Answers1

0

I solved the problem, so I'm posting the answer in case someone else faced the same problem. The problem was the AsyncTask should not be called from TimerTask. A handler should be used to run the AsyncTask Like this:

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

This is solution is by arthvading on this question.

Community
  • 1
  • 1
Alaa
  • 539
  • 3
  • 8
  • 29