我试图更改方法getPriority()。 手动更改了Priority.LOW ... Priority.IMMEDIATE。 但这没有帮助。
MainActivity.java
package com.example.myapplication;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ArrayList<Float> arrayList;
private static final String NUMBERS_URL = "http://10.0.3.2:80/get_number.php";
private static final String TAG = "STACKOVERFLOW";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arrayList = new ArrayList<>();
Log.d(TAG, "arrayList.size() before calling the method >> " + ((Integer) arrayList.size()).toString());
loadArrayList();
Log.d(TAG, "arrayList.size() after calling the method >> " + ((Integer) arrayList.size()).toString());
}
private void loadArrayList() {
StringRequest request;
RequestQueue requestQueue = Volley.newRequestQueue(this);
request = new StringRequest(Request.Method.GET, NUMBERS_URL, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
try {
JSONArray jsonArray = new JSONArray(response);
for(int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
arrayList.add((float) jsonObject.optDouble("number"));
Log.d(TAG, "jsonObject inside method loadArrayList() >> " + jsonObject.toString());
}
Log.d(TAG, "arrayList.size() inside method loadArrayList() >> " + ((Integer) arrayList.size()).toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
// Use VolleyLog
}
}) {
@Override
public Priority getPriority() {
return Priority.IMMEDIATE;
}
};
requestQueue.add(request);
}
}
Logcat:
03-31 12:38:49.929 5319-5319/? D/STACKOVERFLOW: arrayList.size() before calling the method >> 0
03-31 12:38:49.929 5319-5319/? D/STACKOVERFLOW: arrayList.size() after calling the method >> 0
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: jsonObject inside method loadArrayList() >> {"number":"4.0000"}
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: jsonObject inside method loadArrayList() >> {"number":"2.6667"}
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: jsonObject inside method loadArrayList() >> {"number":"4.0000"}
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: jsonObject inside method loadArrayList() >> {"number":"3.3333"}
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: jsonObject inside method loadArrayList() >> {"number":"4.3333"}
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: jsonObject inside method loadArrayList() >> {"number":"2.6667"}
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: jsonObject inside method loadArrayList() >> {"number":"3.6667"}
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: jsonObject inside method loadArrayList() >> {"number":"4.3333"}
03-31 12:38:49.989 5319-5319/? D/STACKOVERFLOW: arrayList.size() inside method loadArrayList() >> 8
以下也是来自服务器的JSON
格式的响应:
[ { “数字”:“ 4.0000” }, { “数字”:“ 2.6667” }, { “数字”:“ 4.0000” }, { “ number”:“ 3.3333” }, { “数字”:“ 4.3333” }, { “数字”:“ 2.6667” }, { “数字”:“ 3.6667” }, { “数字”:“ 4.3333” } ]