gpt4 book ai didi

java - jsonbin.io 获取 Json 数据滞后问题

转载 作者:行者123 更新时间:2023-12-01 18:18:28 25 4
gpt4 key购买 nike

我正在开发一个简单游戏的应用程序,所以这里有一个 picture对于我的 UI,问题是当我使用 Json Web 服务获取级别时,第一次从 activity_level_list 选择级别,图片和关卡编号不显示,只有4个字有效,但图像和关卡编号均不显示,如 this .

NOTE: The image URL variable is null (because of the lag that happens the first time I open first level activity) so an error happen (that's why it shows ic_launcher instead of showing the level picture).

所以,我说问题是“滞后”,因为当我返回 LevelList 并单击级别 1 时,会获取图片和级别编号,一切都会变得很好。

如何让图像和关卡 ID 在第一次打开 Activity 时同时出现?

NOTE: I use jsonbin.io for data fetching.

这是我的 Json 数据获取类:


import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class FetchData extends AsyncTask<Void,Void,Void> {

String update = "4";

String data ="";

String id;
String img;
String w1;
String w2;
String w3;
String w4;


@Override
protected Void doInBackground(Void... voids) {
try {
URL url = new URL("https://api.jsonbin.io/b/5e42776dd18e4016617690ce/" + update);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
String line = "";
while(line != null){
line = bufferedReader.readLine();
data = data + line;
}

JSONArray JA = new JSONArray(data);

// for(int i = 0 ;i < JA.length(); i++) {

JSONObject JO = (JSONObject) JA.get(1);

id = (String) JO.get("id");
img = (String) JO.get("img");
w1 = (String) JO.get("w1");
w2 = (String) JO.get("w2");
w3 = (String) JO.get("w3");
w4 = (String) JO.get("w4");

// }

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

return null;
}

@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);

int levelId = Integer.parseInt(id);
levelId++;

LevelActivity.levelID = String.valueOf(levelId);
LevelActivity.imageURL = img;
LevelActivity.button1Word = w1;
LevelActivity.button2Word = w2;
LevelActivity.button3Word = w3;
LevelActivity.button4Word = w4;

}


}

谢谢^-^

最佳答案

I just needed to update the image and level text view number in onPostExecute method to make it load from the first time. Answer by NIKHIL AGGARWAL

关于java - jsonbin.io 获取 Json 数据滞后问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60323095/

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