在某种程度上,我确实了解Handler,但我不确定如何处理参数,以及如何让代码在后台等待工作完成。我希望UI正常工作,并在后台进行汇率计算。
我有以下几点:
我打电话给new getOnlineExchangeRate().execute(""); //Get Exchange Rate in BG
在那之后,我想要一个result=amount*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;
}发布于 2012-12-09 01:50:49
我想你的问题出在这行:
@Override
protected String doInBackground(String... params) {
getYahooExchangeRate(USDEUR);
return null;您希望返回getYahooExchangeRate和not null :)的结果,因此更改它,返回值应该是双精度值。因此,将其更改为:
@Override
protected Double doInBackground(String... params){
return getYahooExchangeRate(USDEUR);
}您还必须更改您的类头:
public class getOnlineExchangeRate extends AsyncTask<String, Void, Double> {
AsyncTask<Params, Progress, Result>泛型部分告诉AsyncTask处理哪些信息类型。第一个是doInBackground(Params... )参数的类型,第二个是进度信息的类型,最后一个解释doInBackground()返回的类型,因此它将method-header从
protected Result doInBackground(Params... params){ };至受保护的双doInBackground(参数...params){};
为了返回结果,我将使用和观察者顺序回调模式。
编辑:将double更改为Double,因为基元不能用于泛型。
发布于 2012-12-09 01:57:34
代码不等待结果。谁能告诉我如何计算,直到我们有了一个汇率。我需要派人来吗?那会是什么样子?
您可以使用AsyncTask#get()强制代码等待,但这会阻塞主线程,直到AsyncTask完成,这与使用异步任务的目的背道而驰。
最好将您的活动设计为在没有汇率的情况下继续进行,就像我的邮件应用程序加载允许我在获取新消息时撰写消息和阅读旧消息一样。当异步数据加载时,您可以使用新信息更新您的UI。(我相信这就是您要做的。)
要添加到user1885518代码中,您应该在活动中使用AsyncTask作为子类,如下所示:
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
}
}
...
}一旦您知道您想要的汇率,请拨打:
new getOnlineExchangeRate().execute(USDEUR); //Get Exchange Rate in BG现在,当您从在线获取汇率时,代码将使用您所需的汇率调用onPostExecute()。在onPostExceute()内部,您可以在ACtivity中调用任何方法来计算result=amount*exchangerate并在适当的地方显示result。
https://stackoverflow.com/questions/13779918
复制相似问题