javaandroidibm-mobilefirstworklight-adaptersmobilefirst-adapters

How to use WLResourceRequest in worklight 8.0 android


I'm new to worklight, currently working on hybrid android project and following worklight 8.0 documentation.

For Java adapters, use /adapters/{AdapterName}/{path}. The path depends on how you defined your @Path annotations in your Java code. This would also include any @PathParam you used.

My questions are, 1. What is the path? 2. I did't find @Path annotation?

Can anyone guide me to how to use WLResourceRequest in android.

//Here I'm using javascript adapters
    URI adapterPath = new URI("/adapters/adapter name/procedure name");
        WLResourceRequest request = new WLResourceRequest(adapterPath, WLResourceRequest.POST);
                    request.setHeaders(getHeaders());
        request.setTimeout(30000);
                    request.send(jSONString, wlResponseListener);

Solution

  • @Path annotations in your Java code : It is being referred to JAVA Adapter code.

    For example, if you have adapter code like below:

    package com.sample.adapter;
    
    import java.util.logging.Logger;
    
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.PathParam;
    
    @Path("/")
    public class JavaAdapterResource {
    
      //Define logger (Standard java.util.Logger)
        static Logger logger = Logger.getLogger(JavaAdapterResource.class.getName());
    
     //Path for method: "<server address>/Adapters/adapters/JavaAdapter/{username}"
        @GET
        @Path("/{username}")
        public String helloUser(@PathParam("username") String name){
            return "Hello " + name;
        }
    }
    

    @Path("/") before the class definition determines the root path of this resource. If you have multiple resource classes, you should set each resource a different path.

    For example, if you have a UserResource with @Path("/users") to manage users of a blog, that resource is accessible via http(s)://host:port/ProjectName/adapters/AdapterName/users/.

    That same adapter may contain another resource PostResource with @Path("/posts") to manage posts of a blog. It is accessible via the http(s)://host:port/ProjectName/adapters/AdapterName/posts/ URL.

    In the example above, because there it has only one resource class, it is set to @Path("/") so that it is accessible via http(s)://host:port/Adapters/adapters/JavaAdapter/.

    Each method is preceded by one or more JAX-RS 2.0 annotations, for example an annotation of type “HTTP request” such as @GET, @PUT, @POST, @DELETE, or @HEAD. Such annotations define how the method can be accessed.

    Another example is @Path("/{username}"), which defines the path to access this procedure (in addition to the resource-level path). As you can see, this path can include a variable part. This variable is then used as a parameter of the method, as defined @PathParam("username") String name

    Take a look at the below links for more details

    i.Resource request from JavaScript (Cordova, Web) applications

    ii.Creating JAVA Adapters

    iii.ResourceRequestCordova sample app

    iv.Sample Adapter code