通过套接字传输文件,文件传输每个预定的分钟

时间:2013-09-11 11:08:40

标签: java android sockets android-asynctask

我有一个项目,我需要将文件从服务器(PC)传输到客户端(Android),例如每5分钟一次。我的问题是我只能第一次发送文件,其次是跳过我的方法。我设置了一个按钮,onClick将从服务器获取我的文件。我第一次点击它,它工作,其次我有一个例外

09-11 11:02:55.578: E/AndroidRuntime(13472): FATAL EXCEPTION: main
09-11 11:02:55.578: E/AndroidRuntime(13472): java.lang.IllegalStateException: Cannot execute task: the task is already running.
09-11 11:02:55.578: E/AndroidRuntime(13472):    at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:538)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at android.os.AsyncTask.execute(AsyncTask.java:499)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at com.aaandroid.MainActivity$3.onClick(MainActivity.java:63)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at android.view.View.performClick(View.java:3100)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at android.view.View$PerformClick.run(View.java:11644)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at android.os.Handler.handleCallback(Handler.java:587)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at android.os.Handler.dispatchMessage(Handler.java:92)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at android.os.Looper.loop(Looper.java:126)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at android.app.ActivityThread.main(ActivityThread.java:3997)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at java.lang.reflect.Method.invokeNative(Native Method)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at java.lang.reflect.Method.invoke(Method.java:491)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
09-11 11:02:55.578: E/AndroidRuntime(13472):    at dalvik.system.NativeStart.main(Native Method)

我的服务器:

public class TCPServer  {
  public static void main(String args[]) throws IOException {
    // create socket
    ServerSocket servsock = new ServerSocket(64500);
    // declaring files
    while (true) {
      System.out.println("Waiting...");
      Socket sock = servsock.accept();
      System.out.println("Accepted connection : " + sock);
      // sendfile
      File myFile = new File("text.txt");

      // File
      int count;
      byte[] buffer = new byte[1024];
      OutputStream out = sock.getOutputStream();
      BufferedInputStream in = new BufferedInputStream(new FileInputStream(myFile));
      while ((count = in.read(buffer)) > 0) {
        out.write(buffer, 0, count);
        out.flush();
        out.close();
   //     sock.close();
      }
    }
  }
}

我的客户

class TCPClient extends AsyncTask <Void, Void, Void>{

  private final static String serverIP = "10.96.x.zzz";

  private final static int serverPort = 64500;
  private final static String inputFile = "/mnt/sdcard/Text.txt";

  /**
   * @see android.os.AsyncTask#doInBackground(Params[])
   */
  protected Void doInBackground(Void... params) {
    int filesize = 512; // filesize temporary hardcoded
    long start = System.currentTimeMillis();
    int bytesRead = 0;
    int current = 0;
    // localhost for testing
    try {
    Socket sock = new Socket (serverIP,serverPort);
    FileOutputStream fos = new FileOutputStream(inputFile);
    BufferedOutputStream out = new BufferedOutputStream(fos);
    byte[] buffer = new byte[1024];
    int count;
    InputStream in = sock.getInputStream();
    while((count=in.read(buffer)) >0){
        fos.write(buffer);
    }
    out.close();
    fos.flush();
    fos.close();
    sock.close();
    } catch (IOException e) {
      // TODO Add your own exception handling here, consider logging
      e.printStackTrace();
    }
    return null;
  }
}

有什么想法吗?我重复我的问题是我无法在预定的时间发送我的文件,它第一次发送我的文件,但其次我得到了我的任务已经运行的例外。 在MainActivity 63上,我正在运行我的doinbackground。但我使用execute(); 谢谢你的建议!

1 个答案:

答案 0 :(得分:0)

根据AsyncTask javadoc

  

该任务只能执行一次(如果尝试第二次执行则会抛出异常。)

我希望您尝试多次执行TCPClient课程的实例。

(这是猜测......但你没有在你的问题中包含运行任务的代码。)