Announcement Announcement Module
Collapse
No announcement yet.
first way with spring for android Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • first way with spring for android

    Hi
    i want to send an object "Tva" from my mobile to the data base
    so i creat a Rest ws which i tested it with a Servlet on a web application , so it works

    now i am traying to do the same think with android

    i have writen this to code to retreive all tva from DB and insert one tva to the DB

    Code:
     public String getAllTva()
    	 {
    		 List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
    	 acceptableMediaTypes.add(new MediaType("application","xml"));
    
    	 HttpHeaders requestHeaders = new HttpHeaders();
    	 requestHeaders.setAccept(acceptableMediaTypes);
    
    	 HttpEntity<Tva> requestEntity = new HttpEntity<Tva>(requestHeaders);
    
    	 String url = "http://192.168.1.65:8080/testRestWeb/REST/tva/findAll";
    
    	 RestTemplate restTemplate = new RestTemplate();
    	 ResponseEntity<Tva> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, Tva.class);
    	 @SuppressWarnings("unchecked")
    	List<Tva> listTva  = (List<Tva>) responseEntity.getBody();
    	return listTva.get(1).toString();
    	 		}

    Code:
     public String ajouterTva()
    	 {Tva tva = new Tva();
    	 RestTemplate restTemplate = new RestTemplate();
    
    	 tva.setLibTva("android");
    	 tva.setTauxTva("spring");
    	HttpHeaders requestHeaders = new HttpHeaders();
    	 requestHeaders.setContentType(new MediaType("application","json"));
    	 HttpEntity<Tva> requestEntity = new HttpEntity<Tva>(tva, requestHeaders);
    	 try
    	    {String url="http://192.168.1.65:8080/testRestWeb/REST/tva/save";
    	    ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, requestEntity, String.class);//(url1,HttpMethod.POST,requestEntity, String.class);
    	    return responseEntity.getBody();
    	    }
    	    catch(RestClientException rce)
    	    {
    	       
    	    }
    	
    	return null;
    	 }
    }

    is it correct : in the server side i have a class Tva wich is annotaded by @XmlRootElement and also on the client side i have the same class Tva , and my working service is :
    Code:
    @Path("/tva")
    @Produces({ "application/xml", "application/json" })
    public class TvaService {
    
    	TvaSessionRemote tvaremote;
    
    	public TvaService() {
    		try {
    			InitialContext ctx = new InitialContext();
    			tvaremote = (TvaSessionRemote) ctx.lookup("tvaRemote");
    		} catch (NamingException ne) {
    			System.out.println("\n[MyRestService]NamingException: " + ne);
    			ne.printStackTrace();
    		}
    
    	}
    
    	@GET
    	@Path("/findAll")
    	@Produces({ "application/xml", "application/json" })
    	public List<Tva> findAll() {
    
    		return tvaremote.findAll();
    	}
    
    	@GET
    	@Path("/findById")
    	@Produces({ MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON })
    	public Tva findById(@QueryParam("id") Integer id) {
    		return tvaremote.findById(id);
    
    	}
    
    	@POST
    	@Path("/save")
    	@Consumes({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    	// @Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML })
    	// @Consumes("application/x-www-form-urlencoded")
    	public void save(Tva tva) {
    		tvaremote.save(tva);
    
    	}
    
    }

    suggestion please .

  • #2
    it works i can send and retreive object and list of object
    if somone need code i can give it , just contact me

    Comment


    • #3
      Thanks for letting us know you have resolved your issue and everything is working!

      Comment


      • #4
        hello ahmed , could you send me your example , i need it in my project.

        Comment

        Working...
        X