Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

XML parser java not working, why?

  • 12-04-2009 2:02pm
    #1
    Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭


    I'm currently working with GWT trying to pull my gmail contacts and display them, my question is why doesn't this piece of code work
    public void displayMyItems(String token) {
    	    
    	  try{
    		  System.out.println("display items try");
    	  HttpURLConnection connection = (HttpURLConnection)(new URL(ITEMS_FEED)).openConnection() ;
    	    // Set properties of the connection
    	    
    	    connection.setRequestMethod("GET");
    	    connection.setRequestProperty("Authorization", "GoogleLogin auth=" + token);
    	    connection.setRequestProperty("X-Google-Key", "key=" + "");
    	    
    	    int responseCode = connection.getResponseCode();
    	    InputStream inputStream;
    	    if (responseCode == HttpURLConnection.HTTP_OK) {
    	      inputStream = connection.getInputStream();
    	      System.out.println("ipstream");
    	    } else {
    	      inputStream = connection.getErrorStream();
    	      System.out.println("ipstream error");
    	    }
    	    System.out.println("error11");
    	    
    	    System.out.println("here we go " + toString(inputStream));
    	    String xml = toString(inputStream);
    	    System.out.println("STRINGGFGHFGH");
    	    Document xmlDoc = null;
    	    System.out.println("doc");
    	    xmlDoc = XMLParser.parse(xml);
    	    System.out.println("XXXXXXXXXXXXMMMMMMMMMMMMLLLLLLLLLLLDDDDDDDDDOCCCCCCC");
    	    Element root = xmlDoc.getDocumentElement();
    	    System.out.println("root");
    	    XMLParser.removeWhitespace(xmlDoc);
            System.out.println("after white space removal");
            NodeList URLs = (NodeList) root.getChildNodes();
            System.out.println("Urls" + URLs);
            System.out.println("lenght" + URLs.getLength());
            String html = "<ol>";
            for(int i = 0; i < URLs.getLength(); i++){
    
                Element thisElement = (Element) URLs.item(i);
                System.out.println("Element" + thisElement);
                if (thisElement.getNodeName().equals("email")){
    
                   Element linkElement = (Element) thisElement.getFirstChild();
                   //String link = linkElement.getFirstChild().getNodeValue();
    
                   Element titleElement = (Element) linkElement.getNextSibling();
                   //String title = titleElement.getFirstChild().getNodeValue();
                   Element linkElement2 = (Element) linkElement.getNextSibling();
                   System.out.println("WWWWWWWWWWWWWWWWWWWWW" + linkElement2.getNodeValue().toString());
                }
            }
    

    it gets to here xmlDoc = XMLParser.parse(xml); and then stops, String xml seems to just be empty for some reason?

    But this piece of code does work
    public void onCompletion(String responseText) {
            	System.out.println("Gmail Initiated");
                Document xmlDoc = XMLParser.parse(responseText);
                System.out.println("document got");
                Element root = xmlDoc.getDocumentElement();
                XMLParser.removeWhitespace(xmlDoc);
                System.out.println("after white space removal");
                NodeList URLs = (NodeList) root.getChildNodes();
                System.out.println("Urls" + URLs);
                System.out.println("lenght" + URLs.getLength());
                String html = "<ol>";
                for(int i = 0; i < URLs.getLength(); i++){
    
                    Element thisElement = (Element) URLs.item(i);
                    System.out.println("Element" + thisElement);
                    if (thisElement.getNodeName().equals("email")){
    
                       Element linkElement = (Element) thisElement.getFirstChild();
                       //String link = linkElement.getFirstChild().getNodeValue();
    
                       Element titleElement = (Element) linkElement.getNextSibling();
                       String title = titleElement.getFirstChild().getNodeValue();
                       html += "<li><a href=\"" + title + "\">" + title + "hello"+ "</a></li>";
                       System.out.println("title" + title);
                    }
                }
    


    Its really wrecking my heading and google has yielded nothing, I can print the input stream out as a string so it is getting set to a string but when i try to use the Xml Parser on it it just doesn't display anything


Comments

  • Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭Atomic Pineapple


    The problem seems to be that toString(inputStream) is returning a null when I try to set it as a variable or use it within the XMLParser.

    heres the toString() code
      public String toString(InputStream inputStream) throws IOException {
        String string;
        //System.out.println("Into toString()");
        StringBuilder outputBuilder = new StringBuilder();
        if (inputStream != null) {
          BufferedReader reader =
              new BufferedReader(new InputStreamReader(inputStream));
          while (null != (string = reader.readLine())) {
            outputBuilder.append(string).append('\n');
          }
        }
        return outputBuilder.toString();
      }
    

    but it works when I just print it out, like this : System.out.println("here we go " + toString(inputStream));

    So I'm a bit confused as to why its having trouble, can anyone spot the error?

    System.out.println("here we go " + toString(inputStream)); - This line works, it prints out the whole xml file
    String xml = toString(inputStream); - this doesn't, xml never gets set to anything, its just null
    Document xmlDoc = null;
    xmlDoc = XMLParser.parse(xml); - so this doesn't work either as xml is null


    Why is xml not getting set to the string of input stream?


  • Registered Users, Registered Users 2 Posts: 4,287 ✭✭✭NotMe


    I think the problem is that you're calling toString(inputStream) twice. The first time it reads the input and you print it out. The second time there's nothing left to read.

    Try changing
    System.out.println("here we go " + toString(inputStream));
    String xml = toString(inputStream);
    
    to
    String xml = toString(inputStream);
    System.out.println("here we go " + xml);
    
    
    


  • Registered Users, Registered Users 2 Posts: 18,272 ✭✭✭✭Atomic Pineapple


    Thanks NotMe,

    I had figured that out and changed it but it still doesn't work, this is the line its having trouble with


    Document xmlDoc = XMLParser.parse(xml);

    this is the Error I get
    [WARN] StandardContext[]Exception while dispatching incoming RPC call
    com.google.gwt.user.server.rpc.UnexpectedException: Service method 'public abstract void org.developerworks.stocks.client.StocksService.displayMyItems(java.lang.String)' threw an unexpected exception: java.lang.ExceptionInInitializerError
    	at com.google.gwt.user.server.rpc.RPC.encodeResponseForFailure(RPC.java:360)
    	at com.google.gwt.user.server.rpc.RPC.invokeAndEncodeResponse(RPC.java:546)
    	at com.google.gwt.user.server.rpc.RemoteServiceServlet.processCall(RemoteServiceServlet.java:164)
    <Text Removed>
    Caused by: java.lang.ExceptionInInitializerError: null
    	at com.google.gwt.xml.client.XMLParser.<clinit>(XMLParser.java:28)
    	at org.developerworks.stocks.server.StocksServiceImpl.displayMyItems(StocksServiceImpl.java:359)
    Caused by: java.lang.UnsupportedOperationException: ERROR: GWT.create() is only usable in client code!  It cannot be called, for example, from server code.  If you are running a unit test, check that your test case extends GWTTestCase and that GWT.create() is not called from within an initializer or constructor.
    <Text Removed>
    	at com.google.gwt.core.client.GWT.create(GWT.java:91)
    	at com.google.gwt.xml.client.impl.XMLParserImpl.<clinit>(XMLParserImpl.java:29)
    	at com.google.gwt.xml.client.XMLParser.<clinit>(XMLParser.java:28)
    	at org.developerworks.stocks.server.StocksServiceImpl.displayMyItems(StocksServiceImpl.java:359)
    


Advertisement