我是 Volley 库的新手,我正在创建一个简单的 Android 应用程序,该应用程序从以 JSON 格式生成的 API 获取数据。调试后,只有下面提到的这个特定函数fetchAccD
Android Volley 存在延迟问题。此活动和其他活动中的其他功能,这意味着JsonObjectRequest
工作正常。
{
"Account": {
"remarks": "NIL"
"uname": "admin"
"pword": null // value not to be shown
"key": 1316451
"name": "msummons sudo"
}
}
这是我当前的代码
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
uName = getIntent().getStringExtra("username");
// reset session
authidCoDe = 0;
// invoke session
Account account = fetchAccD(uName);
if (account != null) {
authidCoDe = account.getKey();
...
// init user details before loading
private final Account fetchAccD (String userN) {
final Account account = new Account();
requestQueue = null;
requestQueue = Volley.newRequestQueue(this);
String urlConstruct = *json url*;
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlConstruct,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Account");
JSONObject jsonObject = jsonArray.getJSONObject(0);
account.setUname(jsonObject.optString("uname"));
// account.setPword(jsonObject.optString("pword"));
account.setKey(jsonObject.optInt("key"));
account.setName(jsonObject.optString("name"));
account.setRemarks(jsonObject.optString("remarks"));
requestQueue.stop();
}
catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
requestQueue.stop();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
requestQueue.stop();
}
});
requestQueue.add(jsonObjectRequest);
return account;
}
当调用fetchAccD
时,代码运行完美,直到
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlConstruct,
new Response.Listener<JSONObject>()
然后它继续onCreate
在fetchAccD
方法之后运行的位置
if (account != null) {
authidCoDe = account.getKey();
...
在返回到fetchAccD
方法中的onResponse
方法的其余部分之前。
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Account");
...
我已经对大多数函数使用了JsonObjectRequest
GET来获取JSON,这个问题只发生在fetchAccD
函数上。谢谢!
onResponse
是异步的,当服务器返回响应时,它将被调用。
因此,您应该在执行此操作之前等待响应:
if (account != null) {
authidCoDe = account.getKey();
...
}
onCreate
应该做的:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
uName = getIntent().getStringExtra("username");
// reset session
authidCoDe = 0;
// invoke session
fetchAccD(uName);
}
在onResponse
中,您可以执行以下操作:
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Account");
JSONObject jsonObject = jsonArray.getJSONObject(0);
account.setUname(jsonObject.optString("uname"));
// account.setPword(jsonObject.optString("pword"));
account.setKey(jsonObject.optInt("key"));
account.setName(jsonObject.optString("name"));
account.setRemarks(jsonObject.optString("remarks"));
useAccount(account);
requestQueue.stop();
}
catch (JSONException e) {
Toast.makeText(getApplicationContext(), "Fetch failed!", Toast.LENGTH_SHORT).show();
requestQueue.stop();
}
}
定义一个方法useAccount
:
private void useAccount(Account account) {
if (account != null) {
authidCoDe = account.getKey();
...
}
}
fetchAccD
不需要返回值,您可以将其设为void
。