2012-12-08 48 views
0

在某些方面,我確實瞭解Handler,但我不確定該如何處理參數,以及如何讓代碼等待,直到完成作業背景。我希望UI能夠正常工作,並且在後臺我想進行匯率計算。抓住doInBackground(String ... params)

我有以下幾點:

我打電話new getOnlineExchangeRate().execute(""); //Get Exchange Rate in BG

之後,我希望能有一個結果=量* EXCHANGERATE,但代碼不等待結果。 有人可以告訴我計算如何等待,直到我們有一個交換。我是否需要發送一些參數,看起來如何?

. 
. 
. 
. 
. 
public double getYahooExchangeRate(String ER){ 

     double exchangerate=0; 

    try { 

     s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+ER+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");      
     //s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22"+val[from]+val[to]+"%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");      

     JSONObject jObj; 
     jObj = new JSONObject(s); 
     String exResult = jObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate"); 

     exchangerate=Double.parseDouble(exResult); 

     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      ALS.Toast(myContext.getString(R.string.conversionerror), false); 
     } 
     catch (ClientProtocolException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      ALS.Toast(myContext.getString(R.string.conversionerror), false); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
      ALS.Toast(myContext.getString(R.string.conversionerror), false); 
     }         
    return exchangerate; 

}  



public String getJson(String url)throws ClientProtocolException, IOException { 

StringBuilder build = new StringBuilder(); 
HttpClient client = new DefaultHttpClient(); 
HttpGet httpGet = new HttpGet(url); 
HttpResponse response = client.execute(httpGet); 
HttpEntity entity = response.getEntity(); 
InputStream content = entity.getContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
String con; 
while ((con = reader.readLine()) != null) { 
    build.append(con); 
} 
return build.toString(); 
} 



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


     @Override 

     protected void onPostExecute(String result) { 

     // execution of result of Long time consuming operation 
      ALS.Toast(myContext.getString(R.string.exchangeratesupdated), true); 


     } 


    @Override 
    protected String doInBackground(String... params) { 
     // TODO Auto-generated method stub 
     // perform long running operation operation 

     getYahooExchangeRate(USDEUR); 
     return null; 
    } 
+0

ALS.Toast方法爲您的代碼做了什麼? –

+0

這只是一個Toast – Diego

回答

2

我覺得你的問題是在這條線:

@Override 
protected String doInBackground(String... params) { 
    getYahooExchangeRate(USDEUR); 
    return null; 

要返回getYahooExchangeRate的結果,而不是空:) 所以改變這種和返回值應該是一個雙。因此,將其更改爲:

@Override 
protected Double doInBackground(String... params){ 
    return getYahooExchangeRate(USDEUR); 
} 

你也必須改變你的類的頭:

public class getOnlineExchangeRate extends AsyncTask<String, Void, Double> { 


AsyncTask<Params, Progress, Result> 

通用部分講述其Informationstypes被處理的的AsyncTask。 第一個是爲doInBackground(Params...) 第二的PARAMS種類的進展-信息 最後解釋了哪些類型由doInBackground()返回的類型,所以它改變從

protected Result doInBackground(Params... params){ }; 

到 該方法的頭protected double doInBackground(Params ... params){};

要帶回結果我會使用和觀察者或回調模式。

編輯:將double更改爲Double,因爲原語不能用於泛型。

+1

(upvote)這是個好建議,但是你不能使用'AsyncTask ',因爲它會產生一個錯誤。原始數據類型'double'與泛型類定義'Double'不同。你需要使用'AsyncTask '並且更新依賴方法。 – Sam

+0

當然這是完全正確的,我會糾正這一點。 – Murmel

1

該代碼沒有等待結果。有人可以告訴我計算如何等待,直到我們交換。我是否需要發送一些參數,看起來如何?

可以使用AsyncTask#get()迫使碼等,但這個塊主線程,直到完成的AsyncTask無視其使用異步任務的目的。

最好設計你的活動,不用匯率,就像我的郵件應用程序加載允許我在提取新郵件時編寫郵件和閱讀舊郵件一樣。加載異步數據時,您可以使用新信息更新UI。 (我相信這是你正在嘗試做的。)


要添加到user1885518代碼,你應該用你的AsyncTask像這樣在你的Activity子類:

public class MainActivity extends Activity { 
    private class getOnlineExchangeRate extends AsyncTask<Void, Void, Double> { 
     @Override 
     protected Double doInBackground(Void... params) { 
      return getYahooExchangeRate(params[0]); 
     } 

     @Override 
     protected void onPostExecute(Double rate) { 
      // Do something with rate 
     }  
    } 

    ... 
} 

一旦你知道哪些Exchange想要率,請致電:

new getOnlineExchangeRate().execute(USDEUR); //Get Exchange Rate in BG 

現在,當您從網上獲得費率時,代碼將以您想要的費率撥打onPostExecute()。在onPostExceute()的內部,您可以調用您所需的任何方法來計算result=amount*exchangerate並在合適的地方顯示result