2013-06-04 72 views
0

我有一個問題,無法解決它自去年兩個星期以來。我想在這裏得到一些幫助。我其實想從HTTP網站獲取並使用一些有用的數據。這個網站實際上包含事故,事故和所有關於他們的信息。我想從網站獲取這些信息。我將在我的Android應用程序中使用它。我已經問過這個問題,但仍然無法解決它。有人告訴我你必須從JSON獲取這些數據。我以前沒有這樣做過。如果這是唯一的解決方案,那我該怎麼做。如果有其他簡單的方法,那麼請給我。其實,我用如何從HTTP網站收集(獲取和解析)所需的信息/數據?

private String DownloadText(String URL) { 
    int BUFFER_SIZE = 2000; 
    InputStream in = null; 
    try { 
     in = OpenHttpConnection(URL); 
    } catch (IOException e1) { 
     // TODO Auto-generated catch block 
     e1.printStackTrace(); 
     return "exception in downloadText"; 
    } 

    InputStreamReader isr = new InputStreamReader(in); 
    int charRead; 
    String str = ""; 
    char[] inputBuffer = new char[BUFFER_SIZE];   
    try { 
     while ((charRead = isr.read(inputBuffer))>0) 
     {      
      //---convert the chars to a String--- 
      String readString = String.copyValueOf(inputBuffer, 0, charRead); 
      str += readString; 
      inputBuffer = new char[BUFFER_SIZE]; 
     } 
     in.close(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
     return ""; 
    }  
    return str;   
} 

private InputStream OpenHttpConnection(String urlString) throws IOException { 

    InputStream in = null; 
    int response = -1; 

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection(); 

    if (!(conn instanceof HttpURLConnection))      
     throw new IOException("Not an HTTP connection"); 

    try{ 
     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 

     response = httpConn.getResponseCode();     
     if (response == HttpURLConnection.HTTP_OK) { 
      in = httpConn.getInputStream();         
     }      
    } 
    catch (Exception ex) { 
     throw new IOException("Error connecting");    
    } 
    return in;  
} 

獲得所有網站的內容,但它給了所有的內容,即所有信息+ HTML + XML +++。但我只想要所需的信息。

另一件事是,在獲取該數據之前獲得網站管理員權限是否強制?

回答