2017-07-20 14 views
1

私はVolleyを使用して複数のリクエストにヒットしようとしていますが、すべてのリクエストに対して応答を得ています。私の問題は、応答を特定する方法がどのAPIに属しているかです。Androidボレーの複数のリクエストを処理します

mQueue = CustomVolleyRequest.getInstance(this.getApplicationContext()) 
      .getRequestQueue(); 

    final CustomJSONObjectrequest jsonRequest = new CustomJSONObjectrequest(Request.Method 
      .GET, url, 
      new JSONObject(), this, this); // 
    jsonRequest.setTag(REQUEST_TAG); 

    final CustomJSONObjectrequest jsonRequest2 = new CustomJSONObjectrequest(Request.Method 
      .GET, url2, 
      new JSONObject(), this, this); 
    jsonRequest2.setTag(REQUEST_TAG); 
    mButton.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      mQueue.add(jsonRequest); 
      mQueue.add(jsonRequest2); // Both the request will have different API request 
     } 
    }); 
} 
@Override 
public void onErrorResponse(VolleyError error) { 
    mTextView.setText(error.getMessage()); 
} 

@Override 
public void onResponse(Object response) { 

    // How to identify, which response is belong to which api request 
    mTextView.setText("Response is: " + response); 

} 

答えて

2

1回のリクエストでこれを行うことができます。 2番目のリクエストにも同じことが適用されます。この方法で、どのリクエストがあなたに応答を与えているかを知ることができます。

final CustomJSONObjectrequest jsonRequest = new CustomJSONObjectrequest(Request.Method 
     .GET, url, 
     new JSONObject(), this, new Response.Listener<Object>() { 
       @Override 
      public void onResponse(Object response) { 

// How to identify, which response is belong to which api request 
mTextView.setText("Response is: " + response); 

}); 

EDITED:

あなたが好きなインタフェースを作り始めることができ

public class CustomJSONObjectRequest implements Response.Listener<JSONObject>, Response.ErrorListener { 

private VolleyResponse volleyResponse; 
private String tag; 
private JsonObjectRequest jsonObjectRequest; 


public CustomJSONObjectRequest(int method, String url, JSONObject jsonObject, String tag, VolleyResponse volleyResponse) { 
    this.volleyResponse = volleyResponse; 
    this.tag= tag; 
    jsonObjectRequest = new JsonObjectRequest(method, url, jsonObject, this, this); 
} 

@Override 
public void onResponse(JSONObject response) { 
    volleyResponse.onResponse(response, tag); 
} 

@Override 
public void onErrorResponse(VolleyError error) { 
    volleyResponse.onError(error, tag); 
} 

public JsonObjectRequest getJsonObjectRequest() { 
    return jsonObjectRequest; 
} 
} 

:次に、あなたのようなボレー要求のためのカスタムハンドラを作成することができます

public interface VolleyResponse { 

void onResponse(JSONObject object, String tag); 

void onError(VolleyError error, String tag); 
} 

あなたのクラスでそれを呼び出すには、次のように使用します:

CustomJSONObjectRequest request1 = new CustomJSONObjectRequest(Request.Method.GET, url, 
      new JSONObject(), "YOUR REQUEST TAG", this); 

クラスに応答とタグを取得するVolleyResponseインターフェイスを実装させるようにしてください。

@Override 
public void onResponse(JSONObject object, String tag) { 
    Log.i("Response :", object.toString() + " " + tag); 
} 

@Override 
public void onError(VolleyError error, String tag) { 

} 

使用できるボレーキューにリクエストを追加するには:

mQueue.add(request1.getJsonObjectRequest()); 

PS:このコードはテストされていませんが、それは動作するはずです。

+0

を含めたいあなたのクラスで

に取得するためのインタフェースを使用してください。私の親クラスのリスナーと私はmのOnResponseメソッドをオーバーライドしますy parent.thenタグなどを使用して識別する方法はありますか? –

+0

'Response.Listener'インタフェースを実装する抽象クラスを作成し、2つのパラメータを取るようにすることができます.2つ目はタグです。 –

+0

私はそのようにしています。 OnResponse(Obj)には、1つのパラメータしかありませんでした。私たちがリクエストのためにTAGを特定する方法や、間違っている場合、少し詳しく説明することができます。 –

2

Generic Volleyクラスとインターフェイスを作成するインターフェイスを使用して、成功と失敗の応答を取得します。

  • ステップ1ステップ2 クラスの新しいオブジェクトを作成し、必要なパラメータを
を送信ボレークラスから
  • ステップ3応答にアクセスするためのインタフェースを作成
  • 別ボレークラスを作成

    新しいPostVolleyJsonRequest(TestVolley.this、TestVolley。この(interfcae)、 "Submit"、url、params);成功と失敗を送信するためのクラスの

    1. コンテキスト
    2. インタフェースはGET不要のために成功
    3. URL
    4. (必須)のParam(別売)に識別するための要求の
    5. タイプを応答します

    ジェネリックボレークラス

    public class PostVolleyJsonRequest { 
    private String type; 
    private Activity act; 
    private VolleyJsonRespondsListener volleyJsonRespondsListener; 
    private String networkurl; 
    private JSONObject jsonObject = null; 
    private JSONObject params; 
    
    
    public PostVolleyJsonRequest(Activity act, VolleyJsonRespondsListener volleyJsonRespondsListener, String type, String netnetworkUrl,JSONObject params) { 
        this.act = act; 
        this.volleyJsonRespondsListener = volleyJsonRespondsListener; 
        this.type = type; 
        this.networkurl = netnetworkUrl; 
        this.params = params; 
        sendRequest(); 
    } 
    
    private void sendRequest() { 
    
        Log.d("url", "url" + networkurl); 
        JsonObjectRequest jsObjRequest = new JsonObjectRequest(Request.Method.POST,networkurl,params, 
          new Response.Listener<JSONObject>() { 
           @Override 
           public void onResponse(JSONObject response) { 
            Log.e("response", "response " + response); 
            volleyJsonRespondsListener.onSuccessJson(response, type); 
           } 
          }, 
          new Response.ErrorListener() { 
           @Override 
           public void onErrorResponse(VolleyError error) { 
            try { 
             NetworkResponse response = error.networkResponse; 
             Log.e("response", "response " + response); 
             if (response != null) { 
              int code = response.statusCode; 
    
              String errorMsg = new String(response.data); 
              Log.e("response", "response" + errorMsg); 
              try { 
               jsonObject = new JSONObject(errorMsg); 
              } catch (JSONException e) { 
               e.printStackTrace(); 
              } 
              String msg = jsonObject.optString("message"); 
              volleyJsonRespondsListener.onFailureJson(code, msg); 
             } else { 
              String errorMsg = error.getMessage(); 
              volleyJsonRespondsListener.onFailureJson(0, errorMsg); 
             } 
            } catch (Exception e) { 
             e.printStackTrace(); 
            } 
           } 
          }); 
    
        jsObjRequest.setRetryPolicy(new DefaultRetryPolicy(
          600000, 
          DefaultRetryPolicy.DEFAULT_MAX_RETRIES, 
          DefaultRetryPolicy.DEFAULT_BACKOFF_MULT)); 
    
        RequestQueue requestqueue = Volley.newRequestQueue(act); 
        requestqueue.add(jsObjRequest); 
    
    } 
    

    }

    は、応答メッセージ

    public interface VolleyJsonRespondsListener { 
    
    public void onSuccessJson(JSONObject result, String type); 
    public void onFailureJson(int responseCode, String responseMessage); 
    

    }あなたは、私が応答を実装する場合は、複数の要求

    public class TestVolley extends AppCompatActivity implements VolleyJsonRespondsListener{ 
    
    //Your class code goes here 
    
    
    //network request 
    
    try { 
         //parameters 
         //Context,Interface,Type(to indentify your responds),URL,parameter for your request 
    
         //request 1 
         new PostVolleyJsonRequest(TestVolley.this, TestVolley.this, "Submit", url, params); 
    
         //request 2 
         new PostVolleyJsonRequest(TestVolley.this, TestVolley.this, "AccessData", url_2, params_2); 
    
    
    
    
    } catch (Exception e) { 
    
    e.printStackTrace() 
    } 
    
    //Methods from Interface 
    
        @Override 
    public void onSuccessJson(JSONObject result, String type) { 
    
        //Based on the Type you send get the responds and parse it 
        switch (type) { 
         case "Submit": 
          try { 
           parseSubmit(result); 
          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
          break; 
    
         case "AccessData": 
          try { 
           parseAccessData(result); 
          } catch (Exception e) { 
           e.printStackTrace(); 
          } 
          break; 
        }