我有一个项目,我需要将文件从服务器(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 。但我使用执行();谢谢指教!