Results 1 to 3 of 3
  1. #1
    vitaly87 is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

    Default problem with InputStreamReader

    Hello all!
    I have a function search(String target) that return me the correct link that I need.
    I use this link for this function
    Java Code:
    public static String  htmlContentParser(URL address) throws MalformedURLException, IOException{
    String sourceLine;
                    String content = "";
     
                    // The URL address of the page to open.
                    
     
                    // Open the address and create a BufferedReader with the source code.
                    InputStreamReader pageInput = new InputStreamReader(address.openStream());
                    BufferedReader source = new BufferedReader(pageInput);
    But get error on:
    InputStreamReader pageInput = new InputStreamReader(address.openStream());

    The errors are:
    Exception in thread "main" java.io.FileNotFoundException: http://www.gsmarena.com/samsung galaxy-3294.php
    at sun.net.http://www.protocol.http.HttpURLConn...tion.java:1311)
    at java.net.URL.openStream(URL.java:1010)
    at datamining.html.extratorNodes.htmlContentParser(ex tratorNodes.java:116)
    at datamining.html.extratorNodes.<init>(extratorNodes .java:41)
    at datamining.html.getTraningDate.main(getTraningDate .java:34)

    when I dont use search(target) and sent to htmlContentParser the link manually it's all work fine.
    Why is that?

    the code of search is:
    Java Code:
    public  String search(String target) {
    //Convert the target to lowerCase
        target=target.toLowerCase();
    if (target.length()==0)return "model is empty";
    target = target.trim( );
    //QueryString query = new QueryString("search", target);
    try
    {//Open the URL for reading
      URL address = new URL("http://www.gsmarena.com/results.php3?sQuickSearch=yes&sName=" + target);
     
    System.out.println("the links to search is: " +address);
    //Get links from the page,links-all the links from the correct page
    HTMLLinkExtrator links = new HTMLLinkExtrator();
    ArrayList correctLinks = new ArrayList();
    links.LinkExtrator(address.toString());
     
     
     
     
       InputStream in = new BufferedInputStream(address.openStream());
        // Chain the InputStream to a Reader
        InputStreamReader theHTML = new InputStreamReader(in);
        //System.out.println(StreamToString(theHTML));
     
     
     
        //Find in the links the target word like (iphone 4)-->(iphone_4)
    correctLinks=links.findCorrectLinks(target.replaceAll(" ","_"),links.listLinks);
     
     
    //If the model(target) not found the print empty
       if (correctLinks.isEmpty()) System.out.println("the correctLinks is empty ");else
       {
    //ModelId is the id of the model in the web http://www.gsmarena.com/
       String modelId=links.link(correctLinks.get(0).toString(),"-",".php");
       if ((modelId.length()!=0)){
       //Creating the link for the model
      URL corectModel = new URL("http://www.gsmarena.com/"+target+modelId+".php");
      //Return the correct link of the target model
     System.out.println("the correct link of model is: "+correctLinks.get(0));
     theHTML.close();
     in.close();
      return corectModel.toString();
        }else  System.out.println("not found");}
    //System.out.println("the first link: "+correctLinks.get(0));
    }
    catch (IOException e)
    {
        System.err.println(e);
    }
     
    return "not found";
    }
    thanks a lot

  2. #2
    eRaaaa is offline Senior Member
    Join Date
    Oct 2010
    Location
    Germany
    Posts
    787
    Rep Power
    6

    Default

    i think the problem is the space between samsung and galaxy !
    replace it with '+' sign, or use URLEncoder.encode.
    Java Code:
    URL address = new URL("http://www.gsmarena.com/"+URLEncoder.encode("samsung galaxy-3294.php", "UTF-8"));
    works fine.

  3. #3
    vitaly87 is offline Member
    Join Date
    Mar 2010
    Posts
    10
    Rep Power
    0

Similar Threads

  1. Replies: 0
    Last Post: 05-19-2010, 06:38 PM
  2. BufferedReader/InputStreamReader - NON-TECHNICAL
    By Abder-Rahman in forum New To Java
    Replies: 4
    Last Post: 09-27-2009, 11:49 PM
  3. Problem with in InputStreamReader, pls help
    By hyperkossan in forum New To Java
    Replies: 0
    Last Post: 12-10-2008, 09:50 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •