Implementing Youtube Auto complete

94 views
Skip to first unread message

allar...@gmail.com

unread,
Sep 24, 2013, 6:05:51 PM9/24/13
to codenameone...@googlegroups.com
Hello,

I am developing a youtube app. The app allows user to search for music videos on youtube. Youtube has an auto complete feature and  I wish to provide that feature in my app as well. 

This is what I am doing:

1)I have a text field. I have added a dataChangeListener to it. Whenever the text in the textfield is changed, an event is fired. 

2)When the event is fired, I capture the text from the textfield, and pass it to the google API to get autocomplete suggestions. I use a connection request for this.

3)Whenever, I get new text from the text field, I try to kill the previous connection request object. Because the response for the previous text is not important. 

4)Now, it works fine when I type slowly. But when I make several modifications to the text in the text field, I get a null pointer exception. I don't know why . 

5)I have boldened the comments below. Also, is this the right way to do it? Is there a better way?



Here is my code:


 public void autoSuggest(String queryTerm) {
        //gets XML response from Server and then returns a vector of strings

        queryTerm = Util.encodeUrl(queryTerm.trim());
        String queryForServer = AutoSuggestQueryString + "q=" + queryTerm + "&client=toolbar&ds=yt";

        //   queryForServer=Util.encodeUrl(queryForServer);
        if (c != null) {
            c.kill();
            
        }


        c = new ConnectionRequest() {

            protected void readResponse(InputStream input) throws IOException {


                ByteArrayOutputStream bs = new ByteArrayOutputStream();

                int ch;

               if(input==null)
               {
                   return;
               }
                while ((ch = input.read()) != -1) {

                    bs.write(ch);
                }
                
                serverOutput = new String(bs.toByteArray());
                //  System.out.println(serverOutput);

                bs.close();

                InputStream is = new ByteArrayInputStream(serverOutput.getBytes());
                InputStreamReader isr = new InputStreamReader(is);
                XMLParser xml_parser = new XMLParser();
                Element elem;
                elem = xml_parser.parse(isr);

                Vector<String> autoSuggestResult = new Vector<String>();
                int no_of_children = elem.getChildrenByTagName("completesuggestion").size();
                for (int i = 0; i < no_of_children; i++) {
                    String str = elem.getChildAt(i).getChildAt(0).getAttribute("data");
                    autoSuggestResult.add(str);
                }
                displayAutoSuggestResults(autoSuggestResult); // another method to display contents of the vector

            }

            protected void handleErrorResponseCode(int code, String message) {
                //  System.out.println(code+" "+message);
                //  Dialog.setCommandsAsButtons(true);
                Dialog.show("Error", message, "OK", null);
                // System.out.println("The server is acting stubborn");
            }

            protected void handleException(Exception err) {
                //    Dialog.setCommandsAsButtons(true);
                Dialog.show("Error", err.toString(), "OK", null); //I get null pointer exception here
                // System.out.println("The server is acting stubborn");
            }
            
        };


        c.setUrl(queryForServer);
        c.setHttpMethod("GET");
        c.setContentType("xml");


        NetworkManager.getInstance().shutdown(); //to refresh the NetworkManager...remove previoud connection requests
        NetworkManager.getInstance().start();
        NetworkManager.getInstance().addToQueueAndWait(c);
        // System.out.println(serverOutput);



    }

Thanks,
Nikhil

S. Dale Morrey

unread,
Sep 24, 2013, 6:47:28 PM9/24/13
to codenameone...@googlegroups.com, allar...@gmail.com
There is a new AutoCompleteTextField class.  You should use that and simplify your life :)
http://www.codenameone.com/3/post/2013/08/completion-ios-7-update-and-the-20m-mark.html

I can't wait for this thing to make it into GUI builder so I can scrap my own bodged together implementation.

allar...@gmail.com

unread,
Sep 24, 2013, 7:25:29 PM9/24/13
to codenameone...@googlegroups.com, allar...@gmail.com
Cool..I'll use that but I still need help with the connection request thing. :)

S. Dale Morrey

unread,
Sep 24, 2013, 8:17:35 PM9/24/13
to codenameone...@googlegroups.com, allar...@gmail.com
Look carefully.  You're calling the .toString method on your exception.  It should be err.getMessage()
Also do a null pointer check before you pop that dialog.  By the time your request to show a dialog is executed by the event dispatch thread, it's possible that the exception has already been garbage collected.
String msg = err.getMessage();
Dialog.Show("Error",msg,"OK",null);

allar...@gmail.com

unread,
Sep 24, 2013, 8:20:41 PM9/24/13
to codenameone...@googlegroups.com, allar...@gmail.com
But my questions is, why am I getting a null pointer exception. ? Also, how do I correctly kill a connection request or the Networkmanager thread? and how should i implement youtube auto complete?

I am not concered about how to handle the exception right now. That is entirely secondary.

Shai Almog

unread,
Sep 24, 2013, 11:07:29 PM9/24/13
to codenameone...@googlegroups.com, allar...@gmail.com
You can use toString, its better.
The exception is probably from this line: String str = elem.getChildAt(i).getChildAt(0).getAttribute("data");
Either data is missing or the children hierarchy is null.
input will never be null.
Reading one char at a time is REALLY slow.
You are reading into a string, converting to byte array then sending again to a parser which seems like really awkward code. Why not send directly to the parser?

nikhil...@hotmail.com

unread,
Sep 27, 2013, 12:04:51 AM9/27/13
to codenameone...@googlegroups.com, allar...@gmail.com
Hi Shai,

You are probably right about the data element not containing anything. The thing is I was testing the input randomly and typed in some random text like "asdasdasd" .I am guessing You tube did not have any recommendations for that text and returned empty data element as a result of which I got a null exception.

I'll improve the code related to the byte array being converted to string...

Thanks!
Reply all
Reply to author
Forward
0 new messages