私はVolleyライブラリの初心者ですが、JSONで生成するAPIからデータを取得する簡単なAndroidアプリを作成しています。デバッグ後、下記のこの特定の関数fetchAccD
のみがAndroid Volleyの遅延問題を抱えています。このアクティビティ内の他の機能やJsonObjectRequest
を含むその他のアクティビティは、正常に動作します。Volley:JsonObjectRequestのonResponse遅延リクエスト
{
"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>()
に完璧なまで実行されると、それはそれはfetchAccD
方法
onCreate
に移行です
if (account != null) {
authidCoDe = account.getKey();
...
fetchAccD
メソッドのonResponse
メソッドの残りの部分に戻る前に。私はJSONを取得するために私の機能のほとんどのためのJsonObjectRequest
GETを使用しました
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("Account");
...
、この問題は、単にfetchAccD
機能のために起こります。ありがとう!
ありがとうございます!それは動作しますが、私の 'onCreate'メソッドは' fetchAccD(uName);までしか機能しません。それとも? :) –
'fetchAccD'の後にもっと多くのものを追加することができます。あなたのアクティビティが応答を待たなければならないと言うと、その間に他のことをすることはできません。ありがとうございました! –
ありがとう!本当にあなたの助けを感謝、ここに私の承認です:) –