gpt4 book ai didi

java - AsyncTask doInBackground 返回多个字符串

转载 作者:行者123 更新时间:2023-11-29 04:36:28 25 4
gpt4 key购买 nike

我正在尝试在 android studio 中构建一个非常基本的天气应用程序。我正在使用 AsyncClass 返回多个字符串。

正如您在代码中看到的那样,我使用了一个名为“Wrapper”的类来存储我的字符串,这样我就可以返回一个类对象并在 AsyncTask 的 onPostExecute 方法中使用它。我面临的问题是,当我测试应用程序时,所有返回的字符串都以某种方式未定义(Wrapper 类的默认值)。这意味着字符串没有在 doInBackground 方法中更新,我似乎无法弄清楚为什么!

My Activity

    @Override
public void onConnectionFailed(ConnectionResult connectionResult) {
Log.i(MainActivity.class.getSimpleName(), "Can't connect to Google Play Services!");
}
private class Wrapper
{
String Temperature = "UNDEFINED";
String city = "UNDEFINED";
String country = "UNDEFINED";
}

private class GetWeatherTask extends AsyncTask<String, Void, Wrapper> {
private TextView textView;

public GetWeatherTask(TextView textView) {
this.textView = textView;
}

@Override
protected Wrapper doInBackground(String... strings) {
Wrapper w = new Wrapper();
String Temperature = "x";
String city = "y";
String country = "z";

try {
URL url = new URL(strings[0]);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

InputStream stream = new BufferedInputStream(urlConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
StringBuilder builder = new StringBuilder();

String inputString;
while ((inputString = bufferedReader.readLine()) != null) {
builder.append(inputString);
}

JSONObject topLevel = new JSONObject(builder.toString());
JSONObject main = topLevel.getJSONObject("main");
JSONObject cityobj = topLevel.getJSONObject("city");
Temperature = String.valueOf(main.getDouble("temp"));
city = cityobj.getString("name");
country = cityobj.getString("country");

w.Temperature= Temperature;
w.city= city;
w.country=country;

urlConnection.disconnect();
} catch (IOException | JSONException e) {
e.printStackTrace();
}
return w;
}

@Override
protected void onPostExecute(Wrapper w) {
textView.setText("Current Temperature: " + w.Temperature + " C" + (char) 0x00B0
+"\n" + "Current Location: "+ w.country +"\n" + "City: "+ w.city );
}
}
}

更新:

原来我在代码中使用了错误的 url,我使用的是: http://api.openweathermap.org/data/2.5/weather?lat=%f&lon=%f&units=%s&appid=%s

相反,我应该使用:

http://api.openweathermap.org/data/2.5/forecast?lat=%f&lon=%f&units=%s&appid=%s

-aka 而不是我应该使用 forcast 的天气

最佳答案

你的错误从这里开始

JSONObject main = topLevel.getJSONObject("main");

可能是因为 topLevel 对象没有 "main" 键。

{  
"city":{ },
"cod":"200",
"message":0.1859,
"cnt":40,
"list":[ ]
}

把你的JSON丢到这里。 https://jsonformatter.curiousconcept.com/

您会注意到 "list" 元素中有很多很多 "main" 键,但是您必须解析从 开始的键>getJSONArray("列表")


基本上是这样的

String city = "undefined";
String country = "undefined";
List<Double> temperatures = new ArrayList<Double>();

try {
JSONObject object = new JSONObject(builder.toString());
JSONObject jCity = object.getJSONObject("city");
city = jCity.getString("name");
country = jCity.getString("country");

JSONArray weatherList = object.getJSONArray("list");
for (int i = 0; i < weatherList.length(); i++) {
JSONObject listObject = weatherList.getJSONObject(i);
double temp = listObject.getJSONObject("main").getDouble("temp");
temperatures.add(temp);
}

} catch (JSONException e) {
e.printStackTrace();
}

return new Wrapper(city, country, temperatures);

关于java - AsyncTask doInBackground 返回多个字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41231047/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com