2011-06-13 40 views
1

我有,填補了我的SQLite數據庫與HTTP查詢的條目的功能:Android活動太忙而無法設置TextView文本?

try { 
     stringEntity = new StringEntity(SQL); 
     httpPost.setEntity(stringEntity); 
     httpResponse = httpClient.execute(httpPost); 
     httpEntity = httpResponse.getEntity(); 
     bos = new ByteArrayOutputStream(); 
     httpEntity.writeTo(bos); 

     data = bos.toString(); 

     reader = new BufferedReader(
        new StringReader(data)); 

     try { 
      //SAVE DATA IN MY DB || WORKS 
     } catch(IOException e) { 
      e.printStackTrace(); 
     } 

    } catch (IOException e3) { 
     // TODO Auto-generated catch block 
     e3.printStackTrace(); 
    } 

什麼,我試圖做的是過程開始之前(在前面設置我的活動的TextView的文本第一個「嘗試{..」在我的代碼後postet)。 但文字不會改變,因爲我的活動太忙而無法獲取數據(我想我沒有其他解釋..)

有什麼建議嗎?

感謝, prexx

UPDATE '' '從的AsyncTask獲取的數據'

txtAction.setText("Loading..."); 

    AsyncTask<String, String, String> test = new cAsyncTask(); 

    try { 
     data = test.execute(URL).get(); 

     reader = new BufferedReader(
        new StringReader(data)); 

     while ((line = reader.readLine()) != null) { 
      //SAVE DATA IN DB || WORKS 
      } 
     } 

    } catch(IOException e) { 
     e.printStackTrace(); 
    } catch (InterruptedException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (ExecutionException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

我的異步任務:

class cAsyncTask extends AsyncTask<String, String, String> { 

protected String doInBackground(String... urls) { 
    int count = urls.length; 
    String data = ""; 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost; 
    StringEntity stringEntity; 
    HttpResponse httpResponse; 
    HttpEntity httpEntity; 
    ByteArrayOutputStream bos; 
    String line; 
    BufferedReader reader; 
    for (int i = 0; i < count; i++) { 
     httpPost = new HttpPost(urls[i].toString()); 
     try { 
      stringEntity = new StringEntity(SQL); 
      httpPost.setEntity(stringEntity); 
      httpResponse = httpClient.execute(httpPost); 
      httpEntity = httpResponse.getEntity(); 
      bos = new ByteArrayOutputStream(); 
      httpEntity.writeTo(bos); 

      data = bos.toString(); 

     } catch (IOException e3) { 
      // TODO Auto-generated catch block 
      e3.printStackTrace(); 
     } 
    } 
    return data; 
} 

protected void onProgressUpdate(String... progress) { 

} 

protected void onPostExecute(String result) { 
    String test = result; 
} 
+0

爲什麼你使用AsyncTask.get()?從文檔手冊'AsyncTask.get()':如果需要,等待計算完成,然後檢索其結果。所以基本上你們在那兒玩! – 2011-06-13 17:00:31

回答

4

把你的代碼的繁華地段成一個單獨的線程。

看那AsyncTask實用

呼叫AsyncTask.execute()剛過textview.setText("foo")和u將被罰款:)

問候

更新代碼示例:

txtAction.setText("Loading..."); 
AsyncTask<String, String, String> test = new cAsyncTask(); 
test.execute("http://..."); 

class cAsyncTask extends AsyncTask<String, String, String> { 

protected String doInBackground(String... urls) { 
    int count = urls.length; 
    String data = ""; 
    DefaultHttpClient httpClient = new DefaultHttpClient(); 
    HttpPost httpPost; 
    StringEntity stringEntity; 
    HttpResponse httpResponse; 
    HttpEntity httpEntity; 
    ByteArrayOutputStream bos; 
    String line; 
    BufferedReader reader; 
    for (int i = 0; i < count; i++) { 
     httpPost = new HttpPost(urls[i].toString()); 
     try { 
      stringEntity = new StringEntity(SQL); 
      httpPost.setEntity(stringEntity); 
      httpResponse = httpClient.execute(httpPost); 
      httpEntity = httpResponse.getEntity(); 
      bos = new ByteArrayOutputStream(); 
      httpEntity.writeTo(bos); 

      data = bos.toString(); 

     } catch (IOException e3) { 
      // TODO Auto-generated catch block 
      e3.printStackTrace(); 
     } 
    } 
    reader = new BufferedReader(
        new StringReader(data)); 
     String line = ""; 
     while ((line = reader.readLine()) != null) { 
      //SAVE DATA IN DB || WORKS 
    } 
    return data; 
} 


protected void onPostExecute(String result) { 
    String test = result; 
    textView.setText("Done!"); 
} 

} 

的關鍵是將所有繁忙代碼放入將在單獨線程中運行的doInBackGround方法中。所有的UI修改都必須在同一個UI線程中,這可以在onPostExecute方法中完成,該方法將在同一UI線程中執行。

+0

我試過沒有成功......我做錯了什麼? (我會在那裏更新我的代碼) – Prexx 2011-06-13 16:38:50

+0

看看我更新的答案 – 2011-06-13 17:10:31

+0

作品。 (: 非常感謝! – Prexx 2011-06-13 18:51:42

0

您可以嘗試在TextView上調用invalidate()。但使用異步任務是重負載數據方法的最佳實踐。這不會中斷操作UI控件中的用戶。

0

它與「太忙」無關,只是在方法返回時纔會設置文本。隨着你的網絡,這將被延遲。

Btw。在UI線程上的蜂窩網絡將拋出一個異常並殺死你的應用程序。