2016-05-05 9 views
0

を動作しません。私はここで新しい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; 
    } 

} 

答えて

1

以下、ここでまた私のVolleyApplicationクラスは、あなたはそれが間違って解釈されている

を@seradd。

実際には1回だけ実行されています。 デバッグモードで表示されているのは、

最初にrequestObjectを作成し、RequestQueueに追加しています。 RequestQueueこれを実行すると、URLから応答が返されると、それぞれonResponse()onErrorResponse()Response.ListenerResponse.ErrorListenerのインターフェイスから実行されます。

ですから、REQUESTQUEUE呼び出しにタスク を追加した後に行っているものは何でもタスク私はあなたを示唆することは、あなたは間違いなく正しいですonResponse()方法に

+0

をそのコードを追加します。大爆笑! – seradd

+0

@Guarav Sir私は同じ問題に直面しています。あなたはこれをより詳細に説明できますか?あなたのお見積もり: "あなたがRequestQueueにタスクを追加した後にあなたがやっている作業は、onResponse()のコードを呼び出してください" –

+0

@xamMTS 'onResponse()'はサーバーからの応答を受け取った後に呼び出されます。したがって、応答に依存している場合は、そのコードを追加します。 'onResponse()'メソッドに – Gaurav

関連する問題