主要问题是我无法返回两个值帮助。我已经尝试了很多时间但没有成功。而且我是新手,所以请提前感谢我的代码。
这是我的代码
public class calculate extends AsyncTask<String, String, String> {
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
try {
uss = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22INRUSD%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject usjObj;
usjObj = new JSONObject(uss);
usResult = usjObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
eurr = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20(%22INREUR%22)&format=json&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject eurjObj;
eurjObj = new JSONObject(eurr);
eurResult = eurjObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return eurResult + usResult;
////PROBLEM IS HERE ACTUALLY I DON'T KNOW HOW TO RETURN TWO OR MORE VALUE/////"
}
@Override
protected void onPostExecute(String usResult) {
valueus = Double.parseDouble(usResult);
inputus = lengthvalue * valueus;
us.setText("" + inputus);
valueeur = Double.parseDouble(eurResult);
inputeur = lengthvalue * valueeur;
eur.setText("" + inputeur);
}
}
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();
}
}
答案 0 :(得分:0)
你不应该试图把所有东西塞进String
。有更好的数据结构来保存多个值:array,Vector,List等。将AsyntTask声明为:
public class calculate extends AsyncTask<String, String, String[]>
然后你的doInBackgorund
方法会是这样的:
@Override
protected String doInBackground(String... params) {
String[] result = new String[numResults];
...
result[0] = usjObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
...
result[1] = usjObj.getJSONObject("query").getJSONObject("results").getJSONObject("rate").getString("Rate");
...
return result;
}
最后你的onPostExecute
将是
@Override
protected void onPostExecute(String[] usResult) {
...
}