java - Java Stack 因某种原因变为空
问题描述
我正在开发一个将调用 DarkSky 天气 API 的 Android 应用程序(出于显而易见的原因,我在这里编辑了我的 API 密钥)。当我解析 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()
?这是不可能的。
一种解决方案是将buildGraph()
in loadResults()
to inside删除onPostExecute()
。
推荐阅读
- javascript - 我希望当我点击父母(div)时,它应该计算我点击了多少次,帮助我解决这个问题
- linux - 使用 IFS 在 bash 中将字符串拆分为带分隔符的数组
- c# - Unity 2D代码在手机中重复但在电脑中没有问题
- firebase - 我的 Flutter Web 在本地构建得很好,但在 Firebase 托管中不起作用
- phpstorm - 如何在 PhpStorm 中为 Harp.js 命令行命令“harp compile”设置文件观察器?
- amazon-cognito - 如何配置 Cognito 以获取 Facebook 登录以传回包含的图片 url?
- angular - 取消选中Angular的ngx-datatable中的复选框
- javascript - 让 React Bootstrap 表单出现在同一行
- javascript - 空间复杂度中的 O(1) 与 O(n)
- python - Python数据框根据具有条件的另一列分配值