2

我有以下代码在异步任务中运行客户端套接字连接:

@Override
protected Boolean doInBackground(Void... params) { //This runs on a different thread
    boolean result = false;
    try {
        Log.i("AsyncTask", "doInBackground: Creating socket");
        SocketAddress sockaddr = new InetSocketAddress("192.168.1.115", 9090);
        nsocket = new Socket();
        nsocket.connect(sockaddr, 5000); //10 second connection timeout
        if (nsocket.isConnected()) { 
            nis = nsocket.getInputStream();
            wr = new BufferedWriter(new OutputStreamWriter(nsocket.getOutputStream()));
            Log.i("AsyncTask", "doInBackground: Socket created, streams assigned");
            Log.i("AsyncTask", "doInBackground: Waiting for inital data...");
            sockState = true;
            byte[] buffer = new byte[4096];
            int read = nis.read(buffer, 0, 4096); //This is blocking
            while(read != -1){
                byte[] tempdata = new byte[read];
                System.arraycopy(buffer, 0, tempdata, 0, read);
                publishProgress(tempdata);
                Log.i("AsyncTask", "doInBackground: Got some data");
                read = nis.read(buffer, 0, 4096); //This is blocking
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: IOException");
        result = true;
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: Exception");
        result = true;
    } finally {
        try {
            nis.close();
            wr.close();
            nsocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("AsyncTask", "doInBackground: Finished");
    }
    return result;
}

@Override
protected void onProgressUpdate(byte[]... values) {
     Log.d("KMC.NetworkTask", String.valueOf(values[0]));
    if (values.length > 0) {
        Log.d("KMC.NetworkTask", "onProgressUpdate: " + values[0].length + " bytes received.");
        result = new String(values[0]);
    }
}

插座确实有效。然而,onProgressUpdate 没有被调用,即使后台任务告诉我数据进来了。

有人给我一些指点吗??我在谷歌上找不到任何东西:|

4

3 回答 3

0

Values 是一个表,所以当你写 values[0] 时,你会得到字节表,所以我认为你必须做这样的事情: String.valueOf(values[0][0])

于 2012-04-19T15:45:48.627 回答
0

现在我这样解决了:

@Override
protected Boolean doInBackground(Void... params) { //This runs on a different thread
    try {
        Log.i("AsyncTask", "doInBackground: Creating socket");
        SocketAddress sockaddr = new InetSocketAddress("192.168.1.115", 9090);
        nsocket = new Socket();
        nsocket.connect(sockaddr, 5000); //10 second connection timeout
        if (nsocket.isConnected()) { 

            in = new BufferedReader(new InputStreamReader(nsocket.getInputStream()));
            wr = new BufferedWriter(new OutputStreamWriter(nsocket.getOutputStream()));
            Log.i("AsyncTask", "doInBackground: Socket created, streams assigned");
            sockState = true;

            String inputLine = "";
            while ((inputLine = in.readLine()) != null) {
                result = inputLine;
                Log.d("AsyncTask", "doInBackground: Got some data:" + inputLine);
            }
        }
    } catch (IOException e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: IOException");
    } catch (Exception e) {
        e.printStackTrace();
        Log.i("AsyncTask", "doInBackground: Exception");
    } finally {
        try {
            nis.close();
            wr.close();
            nsocket.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.i("AsyncTask", "doInBackground: Finished");
    }
    return true;
}

public String getResult(){
    String r = result;
    result = null;
    return r;
}

主线程等待结果,然后用 getResult() 检索结果:

while(null == networktask.result){}
Log.d("KMC.onCreate", networktask.getResult());
于 2012-04-23T20:35:58.183 回答
0

我有一个类似的问题,因为我为我的 AsyncTask 子类实现了一个构造函数,并且没有在其中调用 super()。

public class TNailTask extends AsyncTask<File, Integer, HashMap<Integer, BitmapDrawable>> {

private DirListingAdapter dir_listing;
private Context context;

public TNailTask(DirListingAdapter dir_listing, Context context) {
    super(); // call super to make sure onProgressUpdate works
    this.dir_listing = dir_listing;
    this.context = context;
}

protected HashMap<Integer, BitmapDrawable> doInBackground(File... image_files) {
  ....
}

protected void onProgressUpdate(Integer... progress) {
  ....
}

}

于 2015-08-30T02:09:04.723 回答