を動作しません。私はここで新しいJsonObjectRequest
リクエスト(GET)Androidのボレー要求が初めてで
を送りたいもらうには、以下の私のコードです:
final VolleyApplication volleyApplication = VolleyApplication.getInstance();
volleyApplication.init(getApplicationContext());
JsonArrayRequest req = new JsonArrayRequest("http://localhost:8080/webapi/", new Response.Listener<JSONArray>() {
@Override
public void onResponse(JSONArray response) {
try {
VolleyLog.v("Response:%n %s", response.toString(4));
System.out.print(response.toString());
Gson gson = new GsonBuilder().setDateFormat("yyyy-MM-dd HH:mm:ss").create();
Type listType = new TypeToken<List<MyEntity>>() {
}.getType();
myList = gson.fromJson(response.toString(), listType);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
VolleyLog.e("Error: ", error.getMessage());
System.out.print(error.getMessage());
}
}
);
RequestQueue requestQueue = volleyApplication.getRequestQueue();
requestQueue.add(req);
これはonCreate
上で動作し、いくつかのオブジェクトを一覧表示しますしかしそれはしません。私がデバッグモードで見るように、プロセスはこのメソッドに対して2回動作します。初めてRequestQueue requestQueue = volleyApplication.getRequestQueue(); requestQueue.add(req);
....になったときには、 の行でメソッドの最後に飛びます。しかし、それは動作し、2回目のデータを取得します。これは私のコードを混乱させた。
そして
public final class VolleyApplication {
private static VolleyApplication instance = null;
public static final VolleyApplication getInstance() {
if (instance == null) {
instance = new VolleyApplication();
}
return instance;
}
private RequestQueue requestQueue;
private ImageLoader imageLoader;
private boolean initialized = false;
private VolleyApplication() {
}
public void init(final Context context) {
if (initialized) {
return;
}
requestQueue = Volley.newRequestQueue(context);
int memory = ((ActivityManager) context
.getSystemService(Context.ACTIVITY_SERVICE)).getMemoryClass();
int cacheSize = 1024 * 1024 * memory/8;
// imageLoader = new ImageLoader(requestQueue, new BitmapLruCache(cacheSize));
}
public RequestQueue getRequestQueue() {
if (requestQueue == null) {
throw new RuntimeException("Init first");
}
return requestQueue;
}
public ImageLoader getImageLoader() {
if (imageLoader == null) {
throw new RuntimeException("Init first");
}
return imageLoader;
}
}
をそのコードを追加します。大爆笑! – seradd
@Guarav Sir私は同じ問題に直面しています。あなたはこれをより詳細に説明できますか?あなたのお見積もり: "あなたがRequestQueueにタスクを追加した後にあなたがやっている作業は、onResponse()のコードを呼び出してください" –
@xamMTS 'onResponse()'はサーバーからの応答を受け取った後に呼び出されます。したがって、応答に依存している場合は、そのコードを追加します。 'onResponse()'メソッドに – Gaurav