get rest string (using jersey and tomcat) the correct way

15 views
Skip to first unread message

lorenzov...@gmail.com

unread,
Sep 21, 2015, 11:47:19 AM9/21/15
to CodenameOne Discussions
Hi all, i searched some example for getting resources (in my case i'm trying to get some strings but soon i must get json objects) and the example i found was this one:

String service="http://localhost:8080/com.vogella.jersey.first/rest/hello";
    
        ConnectionRequest req = new ConnectionRequest(){

              @Override
              protected void postResponse() {
                
              }
               @Override
              protected void readResponse(InputStream input) throws IOException {
                   JSONParser p = new JSONParser();
                 
                //  System.out.println(Util.readToString(input));
                  Dialog dd=new Dialog(Util.readToString(input));
                  hi.addComponent(dd);
              }
            
          };

  req.setUrl(service);
          req.setPost(false);
          req.setContentType("text/xml");
          InfiniteProgress prog = new InfiniteProgress();
          Dialog dlg = prog.showInifiniteBlocking();
          req.setDisposeOnCompletion(dlg);
          NetworkManager.getInstance().addToQueue(req);

the fact is this: i get the html resource because req.setContentType is declared after...how can i configure the connection without the override methods inside the constructor and getting the resource outside the constructor? thank you

IDE: Eclipse
Desktop OS windows xp
Simulator
Device
Message has been deleted

Shai Almog

unread,
Sep 22, 2015, 12:05:55 AM9/22/15
to CodenameOne Discussions, lorenzov...@gmail.com
Hi,
sorry I flashed thru the code and didn't see the ConnectionRequest mentioned above.

The connection request doesn't do anything until you add it to the queue so setting the content type will have the full effect. You can open the network monitor in the simulator and see the outgoing/incoming requests.

You are requesting text/xml which will return an XML response yet you are parsing JSON. I'm guessing you'd want to pass the JSON mime type or use the XML parser.

lorenzov...@gmail.com

unread,
Sep 22, 2015, 3:04:36 AM9/22/15
to CodenameOne Discussions, lorenzov...@gmail.com
Yes but if you see the json parser is a refuse i'm not using it, infact i'm using Util.readToString(input) which is returning always the html and not the xml or text, i post the ws:

@Path("/hello")
public class Hello {

  // This method is called if TEXT_PLAIN is request
  @GET
  @Produces(MediaType.TEXT_PLAIN)
  public String sayPlainTextHello() {
    return "Hello Jersey";
  }

  // This method is called if XML is request
  @GET
  @Produces(MediaType.TEXT_XML)
  public String sayXMLHello() {
    return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
  }

  // This method is called if HTML is request
  @GET
  @Produces(MediaType.TEXT_HTML)
  public String sayHtmlHello() {
    return "<html> " + "<title>" + "Hello Jersey" + "</title>"
        + "<body><h1>" + "Hello Jersey" + "</body></h1>" + "</html> ";
  }

}

so i don't understand why writing text/xml i don't get the xml. Thank you

lorenzov...@gmail.com

unread,
Sep 22, 2015, 3:21:26 AM9/22/15
to CodenameOne Discussions, lorenzov...@gmail.com
Even if i use:
        ConnectionRequest req2=new ConnectionRequest();
        req2.setUrl(service);
        req2.setPost(false);
        req2.setContentType("text/xml");

        InfiniteProgress prog = new InfiniteProgress();
       
        Dialog dlg = prog.showInifiniteBlocking();
        req2.setDisposeOnCompletion(dlg);

        NetworkManager.getInstance().addToQueueAndWait(req2);
        String str=new String(req2.getResponseData());

i get always the html...and this method is so much slow.

lorenzov...@gmail.com

unread,
Sep 22, 2015, 4:42:15 AM9/22/15
to CodenameOne Discussions, lorenzov...@gmail.com
Solved using req.addRequestHeader("Accept", "text/plain");


On Monday, September 21, 2015 at 5:47:19 PM UTC+2, lorenzov...@gmail.com wrote:
Reply all
Reply to author
Forward
0 new messages