web-dev-qa-db-fra.com

Besoin d'envoyer plusieurs demandes de Volley - dans une séquence

Je dois utiliser volley pour envoyer une demande de récupération d'un membre, puis transmettre cet identifiant d'adhésion à la deuxième demande de volée pour récupérer des statistiques sur ce membre. 

J'ai un problème avec ma première requête qui fonctionne parfaitement mais la deuxième requête semble commencer avant que la variable ne soit renvoyée pour être passée. Quelqu'un sait comment empêcher la deuxième demande de démarrer avant que la valeur ne soit renvoyée? 

8
Randy

vous ne pouvez pas simplement écrire chaque demande séquentiellement et attendre de les exécuter après chaque réponse de succès ... vous devez appeler la deuxième demande dans la première réponse de service ... c'est-à-dire

public void firstServiceCall(String url)
{
      JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                     int membershipid=response.getInt("membershipid");
                     //suppose the membershipid comes under main json with key "membershipid"
                     secondServiceCall(membershipid,<url2>);
                     // on the response of first service ... call to the second service ... and continue so on... if required
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    Volley.newRequestQueue(getApplicationContext()).add(jsonObjReq);
  }
  public void secondServiceCall(int membershipid,String url)
  {
       // use this var membershipid acc to your need ... 
       JsonObjectRequest jsonObjReq = new JsonObjectRequest(
            Request.Method.GET, url, null,
            new Response.Listener<JSONObject>() {

                @Override
                public void onResponse(JSONObject response) {
                }
            }, new Response.ErrorListener() {

        @Override
        public void onErrorResponse(VolleyError error) {
        }
    });
    Volley.newRequestQueue(getApplicationContext()).add(jsonObjReq);
  }

l'appel de demande est également asynchrone, par conséquent ... l'autre processus n'attendra pas la fin de l'appel de service ... votre deuxième service commence donc avant la première réponse de service 

9
Angad Tiwari

Code pour chaîner les demandes asynch Volley. Ce qui est important c'est:
a) L'appel de nextRequest () dans l'écouteur mJsonResp.
nextRequest () lie chaque maillon de la chaîne et est appelé par l'auditeur.
b) Le membre mLinkedRequest.
c) La fonction abstraite nécessitant le remplacement de getRequest ().
d) Comment instancier ("utilisation") les requêtes HTTP d’enregistrement:

// 1) POST registrations ->  2) PATCH registrations ->  3) GET devices

    pkRequest ChainReg3 = new LinkedRequest(pkURL.devices, null )   {
        @Override public JSONObject getRequest( final JSONObject response ){
        JSONObject retVal = new JSONObject();
        retVal.put( "MyAwesomeString", "This is the final request string in the chain" );
        return retVal;
}
    } ;

    pkRequest ChainReg2 = new LinkedRequest(pkURL.registrations2, ChainReg3  ){
        //pkRequest ChainReg2 = new pkRequest(pkURL.registrations2, null  ){
        @Override public JSONObject getRequest( final JSONObject response ){
        JSONObject retVal = new JSONObject();
        retVal.put( "MyAwesomeInt", 99 );
        return retVal;
    };

    JSONObject firstRequestBodyTest = new JSONObject( "test" );

    pkRequest ChainReg1 = new LinkedRequest(pkURL.registrations, firstRequestBodyTest , ChainReg2  ){
        @Override public JSONObject getRequest( final JSONObject response ){ return null; }
    };

    ChainReg1.submit();
}

/ * FYI: J'ai créé une énumération pour traiter toutes les URL. Vous pouvez les traiter d'une autre manière. Le but de cet article est de montrer comment enchaîner des demandes de volley asynch.

package org.Somepackage;


import Android.support.annotation.NonNull;
import Android.widget.Toast;

import com.Android.volley.*;
import com.Android.volley.Request.*;
import com.Android.volley.Response.ErrorListener;
import com.Android.volley.toolbox.JsonObjectRequest;

import org.json.*;
import org.peacekeeper.util.pkUtility;

import Java.net.*;
import Java.util.*;


public abstract class LinkedRequest{

protected static org.slf4j.Logger mLog;
protected final static pkUtility mUtility = pkUtility.getInstance();
protected final static RequestQueue mRequestQueue = mUtility.getRequestQueue();
protected final static Toast mToast = Toast.makeText( mUtility.getBaseContext(), "", Toast.LENGTH_LONG );

static private HashMap< String, String > newHeader(){
    HashMap< String, String > newHeader = new HashMap<>();
    final String applicationjson = "application/json";

    newHeader.put( "Accept", applicationjson );
    newHeader.put( "Content-Type", applicationjson );
    return newHeader;
}

static private HashMap<String, String> mHeaders = newHeader()
        , registrations2Hdr = newHeader();
static{
    // http://stackoverflow.com/questions/19797842/patch-request-Android-volley
    registrations2Hdr.put( "X-HTTP-Method-Override", "PATCH" );
}


//end static


private UUID msg_id = UUID.randomUUID();

//each enum needs priority and method, url, request, response, errlsnr

protected final Response.Listener< JSONObject > mJsonResp =  new Response.Listener< JSONObject >(){
    @Override public void onResponse( JSONObject response ){
        String respStr = "response:\t" + ((response == null)? "NULL" : response.toString() );
        mToast.setText( respStr );
        mToast.show();
        mLog.debug( "onResponse\t url:\t" + mPkURL.toString() + "\t:Response:\t" + respStr );

        nextRequest( response );
    }
};



protected final ErrorListener mErrLsnr = new ErrorListener(){
    @Override public void onErrorResponse( VolleyError error ){
        mToast.setText( "Error:\t" + error.getMessage() );
        mToast.show();
        mLog.error( "Error!:\t" + error.getMessage() );

        if( error.networkResponse != null){
            NetworkResponse response = error.networkResponse;
            int statusCode = error.networkResponse.statusCode;
            mLog.error( "statuscode:\t" + statusCode + ":\t" + new String(response.data) );
        }
    }
};


private HashMap< String, String > getAuthorizationHeader(){
    HashMap< String, String > newHeader = newHeader();
    //newHeader.put( "Authorization", SecurityGuard.getAuthToken() );
    newHeader.put( "Authorization", "test getAuthToken()" );
    mLog.debug( newHeader.toString() );
    return newHeader;
}

//PLACE ALL URL NAMES HERE
public enum pkURL{
    //each link in the chain should be of ever increasing priority to reflect its progression.
    devices(Method.GET, Priority.IMMEDIATE)
    // http://stackoverflow.com/questions/28314160/error-in-volley-patch
    // Java.net.ProtocolException: Unknown method 'PATCH'; must be one of [OPTIONS, GET, HEAD, POST, PUT, DELETE, TRACE]
    // http://stackoverflow.com/questions/19797842/patch-request-Android-volley
    , registrations2( Method.POST, Priority.HIGH, LinkedRequest.registrations2Hdr)
    , registrations(Method.POST, Priority.NORMAL )
    , status
    ;

    //each enum needs priority and method, url, request, response, errlsnr
    protected Priority mPriority;
    protected int mMethod;
    public HashMap< String, String > mHeader;
    protected String URLstr;

    pkURL(){
        mMethod = Method.GET;
        mPriority = Priority.LOW;
        mHeader = LinkedRequest.mHeaders;
    }

    pkURL(int aMethod, Priority aPriority ){
        mMethod = aMethod;
        mPriority = aPriority;
        mHeader = LinkedRequest.mHeaders;
    }

    pkURL(int aMethod,
          Priority aPriority,
          HashMap< String, String > aHeader){
        this(aMethod, aPriority);
        mHeader = aHeader;
    }

}//enum pkURL



public pkURL mPkURL;

private JsonObjectRequest mJsonRequest = null;//The current request of this link. For future Request override getRequest().
private LinkedRequest mLinkedRequest = null;
//This constructor is only used for intermediate and last links in the chain.
public LinkedRequest( @NonNull pkURL aPkURL, LinkedRequest aLinkedRequest ){
    mLog = org.slf4j.LoggerFactory.getLogger( getClass() );
    mPkURL = aPkURL;

    if ( aLinkedRequest != null ){
        mLinkedRequest = aLinkedRequest;
        mLinkedRequest.msg_id = this.msg_id;
    }
}//cstr



//This constructor is  only used for the first link in the chain or singleton requests.
public LinkedRequest( @NonNull final pkURL aPkURL, @NonNull JSONObject aRequestBody, LinkedRequest aLinkedRequest ){
    this( aPkURL, aLinkedRequest );

    try{ aRequestBody.put( "_id", msg_id.toString() ); } catch ( Exception ignore ){}

    mJsonRequest = //this is the current request of this link. For future Requests override getRequest().
    new JsonObjectRequest( aPkURL.mMethod, toURL(), aRequestBody, mJsonResp, mErrLsnr ){
        @Override public Priority getPriority() { return mPkURL.mPriority; }
        @Override public Map<String, String> getHeaders() throws AuthFailureError{ return aPkURL.mHeader; }
    };

    mJsonRequest.setShouldCache( false );
}//cstr


private static final String URLhead = "http://192.168.1.156/";
private String toURL(){ //} throws JSONException{
//private < E extends Enum< E > > URL toURL( E URLPostOrGet ){
    URL url;

    switch ( mPkURL){
    case registrations2:
        this.mPkURL.URLstr = "registrations/" + this.msg_id;
        this.mPkURL.mHeader = registrations2Hdr;
        break;

    case devices:
        final String deviceID = "testdeviceId" //SecurityGuard.getEntry( entryType.deviceId )
                    , keeperID = "testkeeperId" //SecurityGuard.getEntry( entryType.keeperId )
                ;

//GET http://192.168.1.156:80/devices/bbdef07b-9360-4d7b-8448-21c4daca4711?where=keeperId=="b0c486e5-13b1-4555-ba5f-d54bb1f0a6f7"
        this.mPkURL.URLstr = new StringBuilder( "devices/" )
                .append( deviceID )
                .append( "?where=keeperId==\"" )
                .append( keeperID + "\"" )
                .toString();

        this.mHeaders = getAuthorizationHeader();
        break;

    default: this.mPkURL.URLstr = mPkURL.name();
    }//switch

    try{
        url =  new URL(URLhead + this.mPkURL.URLstr + "/" );
    }
    catch ( MalformedURLException e ){
        mLog.error( "Error!:\t" + e.getMessage() );
        url = null;
    }
    return url.toString();
}//toURL


@Override public String toString(){
    String toURL = toURL();

    if (mJsonRequest == null) return "toString() NULL mJsonRequest:\t" + toURL;
    String hdrs;
    try{ hdrs = this.mJsonRequest.getHeaders().toString(); }
    catch ( AuthFailureError aAuthFailureError ){ hdrs = "ERROR: getHeaders()"; }


    return new StringBuilder( "\nmethod:\t:" + mJsonRequest.getMethod() )
            .append( "\nheaders:\t" + hdrs )
            .append( "\nbody:\t" +  new String( mJsonRequest.getBody()) )
            .append( "\nBodyContentType:\t" +  mJsonRequest.getBodyContentType() )
            .append( "\nPriority:\t" +  mJsonRequest.getPriority() )
            .append( "\ngetUrl():\t" +  mJsonRequest.getUrl() )
            .append( "\ntoURL():\t" + toURL )
            .append( "\n" )
            .toString();

}//toString()


//getRequest is used for the FUTURE request of mLinkedRequest.
//for CURRENT request of this() use the constructor
abstract public JSONObject getRequest( final JSONObject response );

private Request nextRequest(final JSONObject aResponse ){//nextRequest() ties each link in the chain together and is called by the listener.
    mLog.debug( "nextRequest:\t" + aResponse.toString() );
    Request retVal = null;
// * * * * * * This is how and where the the "future" request must be called/constructed. * * * * * *
    if ( mLinkedRequest != null ){
        final int mMethod = mLinkedRequest.mPkURL.mMethod;
        final String aURL = mLinkedRequest.toURL();

        final JSONObject requestBody = mLinkedRequest.getRequest( aResponse );

        final Priority priority = mLinkedRequest.mPkURL.mPriority;
        final Map<String, String> header = mLinkedRequest.mPkURL.mHeader;

        this.mJsonRequest = new JsonObjectRequest( mMethod, aURL,
                                                      requestBody,
                                                      mJsonResp, mErrLsnr ){
            @Override public Priority getPriority() { return priority; }
            @Override public Map<String, String> getHeaders() throws AuthFailureError{ return header; }
        };

        mJsonRequest.setShouldCache( false );
        this.mLinkedRequest = mLinkedRequest.mLinkedRequest;
        retVal =  submit();
    }//if
// * * * * * *  * * * * * * * * * * * *  * * * * * * * * * * * *  * * * * * * * * * * * * * * * * * *

return retVal;
}//nextRequest()


public Request submit(){
    mLog.debug( "submit:\n" );
    mLog.debug( this.toString() );
    if ( mLinkedRequest != null ) mLog.debug( "mLinkedRequest:\t" + mLinkedRequest.toString() );
    if ( mJsonRequest!= null ) return mRequestQueue.add( mJsonRequest );
    return null;
}//submit()
}//class LinkedRequest
0
JDOaktown