[英]Volley: onResponse in JsonObjectRequest delayed request
Am a newbie to the Volley library, I'm creating a simple Android app that takes data from an API that generates in JSON. 作为Volley库的新手,我正在创建一个简单的Android应用,该应用从JSON生成的API中获取数据。 After debugging, only this particular function fetchAccD
mentioned below have delay issues with Android Volley. 调试后,只有以下提到的特定功能fetchAccD
才具有Android Volley的延迟问题。 Other functions within this Activity and other Activities which implies the JsonObjectRequest
works perfectly fine. 这个Activity和其他Activity中的其他函数暗示JsonObjectRequest
可以正常工作。
{
"Account": {
"remarks": "NIL"
"uname": "admin"
"pword": null // value not to be shown
"key": 1316451
"name": "msummons sudo"
}
}
Here's my current code 这是我当前的代码
@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;
}
When invoking fetchAccD
, the code runs perfect up to 调用fetchAccD
,代码可以完美运行到
JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, urlConstruct,
new Response.Listener<JSONObject>()
Then it proceeds to onCreate
where it runs after the fetchAccD
method 然后继续进行onCreate
,在fetchAccD
方法之后运行
if (account != null) {
authidCoDe = account.getKey();
...
Before it goes back to the rest of the onResponse
method in the fetchAccD
method. 回到onResponse
方法中的fetchAccD
方法的其余部分。
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Account");
...
I've used JsonObjectRequest
GET for most of my functions to fetch JSON, this problem only happens just for the fetchAccD
function. 我已将JsonObjectRequest
GET用于大多数函数来获取JSON,此问题仅在fetchAccD
函数中才会发生。 Thanks! 谢谢!
onResponse
is asynchronous, it will be invoked when the server returns a response. onResponse
是异步的,它将在服务器返回响应时被调用。
So you should wait the response before doing this: 因此,您应该先等待响应,然后再执行以下操作:
if (account != null) {
authidCoDe = account.getKey();
...
}
onCreate
should do: 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);
}
In onResponse
you can do: 在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();
}
}
Define a method useAccount
: 定义一个方法useAccount
:
private void useAccount(Account account) {
if (account != null) {
authidCoDe = account.getKey();
...
}
}
And fetchAccD
does not need to return a value, you can make it void
. 而且fetchAccD
不需要返回值,可以将其设为void
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.