Volley: onResponse in JsonObjectRequest 延迟请求



我是 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>()

然后它继续onCreatefetchAccD方法之后运行的位置

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

相关内容

  • 没有找到相关文章

最新更新