AsyncTask没有执行多次

时间:2018-08-11 14:05:19

标签: android tcp android-asynctask

我有一个应用程序必须轮询(LAN上的)TCP服务器以获取一些数据,我正在使用AsyncTask类中的套接字来执行此操作。

它对于前几个请求非常有效。但是在某个时候,应用程序必须每2秒轮询一次服务器(使用计时器)。这是当AsyncTask停止执行且TCP消息未发送到服务器时。我不知道为什么。

代码在下面。任何帮助将不胜感激!

谢谢!

import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.CountDownTimer;
import android.util.Log;
import android.widget.Toast;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;

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

Context context;
TcpResultListener tcpResultListener;
int actionCode;
String TAG = "TcpTask";
String SERVER_IP = "192.168.1.12", SERVER_PORT = "1234";
Socket socket = null;
PrintWriter out;

int readTimeout;

// Creating listener
public void setOnTcpResultsListener(TcpResultListener tcpResultListener, int actionCode) {
    this.tcpResultListener = tcpResultListener;
    this.actionCode = actionCode;
}

// Constructor with context as parameter
public TcpTask(Context context, int timeout) {
    this.context = context;
    this.readTimeout = timeout;
}

@Override
protected String doInBackground(String... params) {
    String result = null;
    try {
        //Create a client socket and define internet address and the port of the server
        socket = new Socket(params[0], Integer.parseInt(params[1]));

        Log.d(Constants.TAG, "Socket created");

        //Setting timeout for readLine
        socket.setSoTimeout(readTimeout);

        Log.d(Constants.TAG, "Timeout set");

        //Get the output stream of the client socket
        out = new PrintWriter(socket.getOutputStream(), true);

        Log.d(Constants.TAG, "PrinterWriter created");

        //Write data to the output stream of the client socket
        out.println(params[2]);
        Log.d(Constants.TAG, "Sending TCP data: " + params[2]);

        //Get the input stream of the client socket
        InputStream is = socket.getInputStream();

        //Buffer the data coming from the input stream
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        //Read data in the input buffer
        result = br.readLine();

    } catch (NumberFormatException e) {
        Log.d("TcpException", e.toString());
    } catch (UnknownHostException e) {
        Log.d("TcpException", e.toString());
    } catch (IOException e) {
        Log.d("TcpException", e.toString());
    }

    try {
        Log.d(Constants.TAG, "Socket:: " + socket);

        if(socket != null){
            socket.close();
        }
    } catch (IOException e) {
        Log.d(Constants.TAG, e.toString());
    }

    return result;
}

@Override
protected void onPostExecute(String result) {

    Log.d(Constants.TAG, "String:: " + result);

    tcpResultListener.onResultsReceived(result, actionCode);
}


}

我用来调用AsyncTask的方法:

void sendTCP(String msg) {

    TcpTask tcpTask = new TcpTask(this, 8000);

    // Setting listener for tcpTask to send back result
    tcpTask.setOnTcpResultsListener(AddSpaceActivity.this, 1);

    // TODO: Change the data being passed below
    //Pass the server ip, port and client message to the AsyncTask
    tcpTask.execute(gwIP, gwPort, msg);
}

编辑:计时器代码:

t = new Timer();

    t.schedule(new TimerTask() {
        @Override
        public void run() {
            Log.d(Constants.TAG, "Timer fired:: " + firstReceived);

            if(firstReceived){
                sendTCP(Constants.NETWORK_STATUS_REQUEST);
            }
        }
    }, 3000, 2000);

0 个答案:

没有答案