我需要執行Volley請求並等待響應來解析並返回它,但不知道如何實現此目的。 有人可以幫忙嗎? 這裏是我的代碼:Android - 等待Volley響應完成並繼續執行
public void getWord(){
final JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, "http://ent-ifsi.com/Projet/Application_Android/pendu_android.php",
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONObject lesMots = response.getJSONObject("lesMots");
Iterator<?> keys = lesMots.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
if (lesMots.get(key) instanceof JSONObject) {
JSONObject obj = (JSONObject) lesMots.get(key);
//Récupération des deux mots, anglais et français
String wordToFind = obj.getString("wordFrench").toUpperCase();
String wordEnglish = obj.getString("wordEnglish").toUpperCase();
//Mots mis dans un array respectif
listOfWordEnglish.add(wordEnglish);
listOfWordFrench.add(wordToFind);
container.removeAllViews();
}
numberOfWord++;
}
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(), e + "", Toast.LENGTH_LONG).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e("Volley", "ERROR");
Toast.makeText(getApplicationContext(), error + "", Toast.LENGTH_LONG).show();
}
}
);
queue.add(jsonObjectRequest);
}
有7字每個陣列中,但是當我試圖讓接取到我initGame功能,它說,他們是空的,我想這是因爲截擊要求不是這個快! 如果有人能解釋我該怎麼做
這裏是我的initGame功能
`public void initGame() {
win = false;
found = 0;
typed_word.setText("");
image.setBackgroundResource(R.drawable.first);
listOfLetters = new ArrayList<>();
listOfWordFrench = new ArrayList<>();
listOfWordEnglish = new ArrayList<>();
getWord();
for (int i = 0; i < listOfWordEnglish.size(); i++){
System.out.println(i);
String wordEnglish = listOfWordEnglish.get(i);
wordEnglishPendu.setText(wordEnglish);
word = listOfWordFrench.get(i);
for (int x = 0; x < word.length(); x++) {
TextView oneLetter = (TextView) getLayoutInflater().inflate(R.layout.textview, null);
container.addView(oneLetter);
}
}
}`
是您的要求解僱?是叫'''onResponse''嗎? – danypata
我只是把我的'getWord()'變成一個必須啓動我的遊戲的函數,我剛剛添加了我的InitGame() –