Volley:onResponse在JsonObjectRequest中延迟了请求

时间:2016-06-11 13:25:07

标签: java android json get android-volley

作为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();
    ...

在返回onResponse方法中fetchAccD方法的其余部分之前。

@Override
public void onResponse(JSONObject response) {
    try {
        JSONArray jsonArray = response.getJSONArray("Account");
        ...

我已经使用JsonObjectRequest GET来获取JSON的大多数函数,这个问题只发生在fetchAccD函数中。谢谢!

1 个答案:

答案 0 :(得分:0)

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