cancel
Showing results for 
Search instead for 
Did you mean: 

Help REST java

leandro1
Champ in-the-making
Champ in-the-making
Hello everyone . I am new to this and have a problem with Activiti Rest.
I show them what I have:

1-

   public static void main(String[] args) throws Exception {
      
        DefaultHttpClient httpclient = new DefaultHttpClient();
        try {

           HttpGet httpGet = new HttpGet("http://localhost:8080/activiti-rest/service/runtime/tasks/20005");   
          
           
           UsernamePasswordCredentials creds = new UsernamePasswordCredentials("kermit", "kermit");
           httpGet.addHeader(new BasicScheme().authenticate(creds, httpGet));
      
            System.out.println("executing request" + httpGet.getRequestLine());
           
            HttpResponse response = httpclient.execute(httpGet);
            HttpEntity entity = response.getEntity();

            System.out.println("—————————————-");
            System.out.println(response.getStatusLine());
        
           
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
            httpclient.getConnectionManager().shutdown();
        }
    }


This only tells me that works. I would love to know how to get the data from that task and manipulate from java

2-

public static void main(String[] args) throws Exception {
      
      //crea un httpClient
        DefaultHttpClient httpClient = new DefaultHttpClient();
        try {
        //indica donde se va a conectar
           HttpPost httpPost = new HttpPost("http://localhost:8080/activiti-rest/service/runtime/tasks/20005");   
       //Acepta json
           httpPost.setHeader("Accept", "application/json");
       //Identificacion para usar rest    
           UsernamePasswordCredentials creds = new UsernamePasswordCredentials("kermit", "kermit");
           httpPost.addHeader(new BasicScheme().authenticate(creds, httpPost));
      
       //Parammetros para enviar por post    
           List<NameValuePair> arguments = new ArrayList();
           BasicNameValuePair action = new BasicNameValuePair("action","complete");
           arguments.add(action);
                  
          
       //Envia los parametros   
           httpPost.setEntity(new UrlEncodedFormEntity(arguments));
        
          
       //Me muestra el metodo y la url que estoy usando   
            System.out.println("executing request " + httpPost.getRequestLine());
           
       //Ejecuta todo lo anterior    
            HttpResponse response = httpClient.execute(httpPost);
      
       //No se
            HttpEntity entity = response.getEntity();

       //Me muestra el tipo de error o si salio bien
            System.out.println("—————————————-");
            System.out.println(response.getStatusLine());
           
    
           
           
            if (entity != null) {
                System.out.println("Response content length: " + entity.getContentLength());
            }
            EntityUtils.consume(entity);
        } finally {
            // When HttpClient instance is no longer needed,
            // shut down the connection manager to ensure
            // immediate deallocation of all system resources
           
           //Cierra la coneccion
            httpClient.getConnectionManager().shutdown();
        }
      
   
   }


this returns error
HTTP/1.1 415 Media Type Not Supported

thank you!
Sorry for my bad english
4 REPLIES 4

vasile_dirla
Star Contributor
Star Contributor
Hi,
For getting the content of the response you should consume the response entity.
just inspect the values inside this entity and you'll find a json containing the serialised response of the invoked method.
About 415 error. You have to be sure you post data using the proper format.
Verify what type should have the object expected as parameter  in the invoked method and post such an object.

bharatsinghrath
Champ in-the-making
Champ in-the-making
can you explain it with example

leandro1
Champ in-the-making
Champ in-the-making
Thank you very much for the advice!

Solution 1:
<java>
package clasesParte2;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.api.client.filter.LoggingFilter;

public class test1GetFunciona {

private static String REST_URI = "http://localhost:8080/activiti-rest/service/";

   public static void main(String[] args) {
   try {
    Client client = Client.create();
  
    WebResource webResource = client.resource(REST_URI+"runtime/tasks/20005");
   
    final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter("kermit", "kermit");
       client.addFilter(authFilter);
       client.addFilter(new LoggingFilter());
   
    ClientResponse response = webResource.accept("application/json")
                     .get(ClientResponse.class);
   
    if (response.getStatus() != 200) {
       throw new RuntimeException("Failed : HTTP error code : "
     + response.getStatus());
    }
  
    String output = response.getEntity(String.class);
   
      //Me muestra el tipo de error o si salio bien
             System.out.print(response.getStatus());
             System.out.println(" "+response.getStatusInfo());
             System.out.println("—————————————-");
            
    System.out.println("Output from Server …. \n");
    System.out.println(output);
  
     } catch (Exception e) {
            e.printStackTrace();
     }
   }
  }
</java>

Solution 2 complete task:
<java>
package clasesParte2;

import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.config.ClientConfig;
import com.sun.jersey.api.client.config.DefaultClientConfig;
import com.sun.jersey.api.client.filter.HTTPBasicAuthFilter;
import com.sun.jersey.api.client.filter.LoggingFilter;

public class test2PostNoFunciona {

private static String REST_URI = "http://localhost:8080/activiti-rest/service/";
  
   public static void main(String[] args) {
   try {
    ClientConfig config = new DefaultClientConfig();
    Client client = Client.create();
  
    WebResource webResource = client.resource(REST_URI+"runtime/tasks/20005");
   
    webResource.setProperty("Content-Type", "application/json; charset=UTF-8");
   
    final HTTPBasicAuthFilter authFilter = new HTTPBasicAuthFilter("kermit", "kermit");
       client.addFilter(authFilter);
       client.addFilter(new LoggingFilter());
   
//            FORMULARIO
       String input = "{\"action\":\"complete\"}";

       ClientResponse response = webResource.accept("application/json")
                  .type("application/json").post(ClientResponse.class, input);    
  
    if (response.getStatus() != 200) {
       throw new RuntimeException("Failed : HTTP error code : "
     + response.getStatus());
    }
  
    String output = response.getEntity(String.class);
  
    System.out.println("Output from Server …. \n");
    System.out.println(output);
  
     } catch (Exception e) {
    e.printStackTrace();
     }
   }
}
</java>

leandro1
Champ in-the-making
Champ in-the-making
Sorry for posting in this forum . Recently I realized that was wrong.