gpt4 book ai didi

Java Stack 由于某种原因变空

转载 作者:行者123 更新时间:2023-12-01 17:51:01 24 4
gpt4 key购买 nike

我正在开发一个 Android 应用程序,它将调用 DarkSky 天气 API(出于明显的原因,我已在此处编辑了我的 API key )。当我解析 JSON 数据并将其推送到我命名为 dataStack 的堆栈时,我的问题就出现了。在推送堆栈时,我记录了它的大小并且它显示正确。但是,当我的代码到达 buildGraph() 方法时,堆栈现在为空,并且所有数据都消失了。是什么原因导致堆栈为空?

编辑:发布后 30 分钟,我找到了解决方法。我现在返回字符串并在我的 MainActivity Android 类中解析它。但是,我仍然不知道为什么堆栈被删除。我很想知道:)

public class MainActivity extends AppCompatActivity {

Button button;
TextView progressLabel;

GraphView graph;

JSONObject jsonObject;

static Stack<DataPoint> dataStack = new Stack<>(); // stack for graph data points

static final String API_URL = "https://api.darksky.net/forecast/API_KEY/42.3611,-71.0570,"; // #TODO: delete API key before comitting to GitHub
static final String URL_TAIL = "?exclude=currently,flags,hourly"; // end of URL
static final long currTime = System.currentTimeMillis() / 1000L; // current UNIX time
static long unixTime = currTime;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

button = findViewById(R.id.button);
progressLabel = findViewById(R.id.progressLabel);

graph = findViewById(R.id.graph);
}

public void loadResults(View view) {
for (int x = 0; x < 2; x++) { // 7 API calls for each of 7 days
new APICall().execute();
unixTime -= 86400; // subtract 24 hours in UNIX time
dataStack.size();
}
buildGraph(); // after all data is gathered, build a graph using it
}

private void buildGraph() {
// #TODO: Method to build graph
Log.i("STACK pop", String.valueOf(dataStack.size()));
}


class APICall extends AsyncTask<Void, Void, String> { // Extend AsyncTask so we don't hijack the main UI thread

protected void onPreExecute() {
// Do stuff before executing the AsyncTask
progressLabel.setText("Fetching Data");
}

protected String doInBackground(Void... urls) {
// Execute background task here


try {
final String FULL_URL = API_URL + unixTime + URL_TAIL; // build the full URL with latest time
URL url = new URL(FULL_URL); // URL for the API call
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); // connection to URL
try {
// tools for reading API results
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
StringBuilder stringBuilder = new StringBuilder();
String line;
// accumulate results
while ((line = bufferedReader.readLine()) != null) {
stringBuilder.append(line).append("\n");
}

bufferedReader.close(); // always close buffered reader

return stringBuilder.toString(); // return results
}
finally {
// inside a finally block so that no matter what we always end a connection that has been started
urlConnection.disconnect(); // end the connection

}
}
catch(Exception ex) {
Log.e("ERROR", ex.getMessage(), ex);
return null;
}
}

protected void onPostExecute(String response) {
// Do stuff after we're finished executing

if (response == null) {
response = "AN ERROR HAS OCCURRED";
}
else {
try {
jsonObject = new JSONObject(response); // create object from our response
JSONArray arr = jsonObject.getJSONObject("daily").getJSONArray("data"); // get data Array
String arrString = arr.getString(0); // full String
String[] splitString = arrString.split(","); // split String into array by comma


String time = splitString[0].substring(8); // time is the first index of the array, use substring to cutout unecessary info
String temp = splitString[11].substring(18);

dataStack.push(new DataPoint(Integer.valueOf(time), Float.valueOf(temp))); // push our data onto the stack as a DataPoint

Log.i("STACK push", String.valueOf(dataStack.toString()));

response = "Data received"; // display this to user
}
catch(Exception ex) {
response = "ERROR DURING JSON PARSING";
}

}

progressLabel.setText(response);
// parse data here
Log.i("INFO", response);

}
}

}



最佳答案

堆栈为空,因为结果尚未输入。问题在于您的 loadResults()

    public void loadResults(View view) {
for (int x = 0; x < 2; x++) { // 7 API calls for each of 7 days
new APICall().execute();
unixTime -= 86400; // subtract 24 hours in UNIX time
dataStack.size();
}
buildGraph(); // after all data is gathered, build a graph using it
}

您发出 new APICall().execute(); 来请求数据并更新 dataStack,并且您希望获得 dataStack在同一个函数 loadResults() 中“立即”得到结果?这是不可能的。

一种解决方案是将 loadResults() 中的 buildGraph() 删除到 onPostExecute() 内部。

关于Java Stack 由于某种原因变空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60794855/

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