2012-04-19 70 views
2

我有下面的代碼運行在的AsyncTask客戶端套接字連接:的Android的AsyncTask publishProgress不叫onProgressUpdate

@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不被調用,即使後臺任務告訴我數據進來。

任何人都有一些指針給我?我在google上找不到任何東西:|

+0

多少數據,你在看什麼?如果它不是很多,那麼後臺線程可能在第一次進度更新發生之前完成。 – 2012-04-22 10:27:08

+0

只有幾個字節... somethimes只有「真正」發送..順便說一句,後臺線程是一個循環,套接字保持打開,任務沒有完成 – 2012-04-23 12:24:57

回答

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()); 
0

值是一個表,所以,當你寫值[0]你得到字節的表格,所以我認爲你必須做這樣的事情: String.valueOf(values[0][0])

+0

我會告訴它,當我回家 – 2012-04-19 17:05:02

+0

我試過了:沒有成功。我也嘗試使用字符串作爲更新類型,但沒有成功。就好像該方法沒有被調用。 – 2012-04-19 18:31:30

0

我也有類似的問題,因爲我公頃d實現了我的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) { 
    .... 
} 

}