2012-04-19 45 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) { 
    .... 
} 

}

相关问题