android初級之Android擷取網頁資料的方法總結

來源:互聯網
上載者:User

本文總結了三種擷取網頁資料的代碼,是自己在用的時候隨手整理出來的。此處僅貼出函數段,不貼出import了,用的時候可以用eclipse自動import一下就行了。函數的詳細用途描述請看代碼中注釋。調用的時候請對應函數需要的參數。


//第一種
/**擷取參數(ArrayList<NameValuePair> nameValuePairs,String url)後post給遠程伺服器
 * 將獲得的返回結果(String)返回給調用者
 * 本函數適用於查詢數量較少的時候
*/
public String posturl(ArrayList<NameValuePair> nameValuePairs,String url){
    String result ="";
    String tmp="";
    InputStream is =null;
    try{
        HttpClient httpclient =new DefaultHttpClient();
        HttpPost httppost =new HttpPost(url);
        httppost.setEntity(newUrlEncodedFormEntity(nameValuePairs));
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        return"Fail to establish http connection!";
    }
 
    try{
        BufferedReader reader =new BufferedReader(newInputStreamReader(is,"utf-8"));
        StringBuilder sb =new StringBuilder();
        String line =null;
        while((line = reader.readLine()) != null) {
            sb.append(line +"\n");
        }
        is.close();
 
        tmp=sb.toString();
    }catch(Exception e){
        return"Fail to convert net stream!";
    }
 
    try{
        JSONArray jArray =new JSONArray(tmp);
        for(inti=0;i<jArray.length();i++){
            JSONObject json_data = jArray.getJSONObject(i);
            Iterator<?> keys=json_data.keys();
            while(keys.hasNext()){
                result += json_data.getString(keys.next().toString());
            }
        }
    }catch(JSONException e){
        return"The URL you post is wrong!";
    }
 
    returnresult;
}
 
//第二種
/**擷取參數指定的網頁代碼,將其返回給調用者,由調用者對其解析
 * 返回String
*/
public String posturl(String url){
    InputStream is =null;
    String result ="";
 
    try{
        HttpClient httpclient =new DefaultHttpClient();
        HttpPost httppost =new HttpPost(url);
        HttpResponse response = httpclient.execute(httppost);
        HttpEntity entity = response.getEntity();
        is = entity.getContent();
    }catch(Exception e){
        return"Fail to establish http connection!"+e.toString();
    }
 
    try{
        BufferedReader reader =new BufferedReader(newInputStreamReader(is,"utf-8"));
        StringBuilder sb =new StringBuilder();
        String line =null;
        while((line = reader.readLine()) != null) {
            sb.append(line +"\n");
        }
        is.close();
 
        result=sb.toString();
    }catch(Exception e){
        return"Fail to convert net stream!";
    }
 
    returnresult;
}
 
//第三種
/**擷取指定地址的網頁資料
 * 返回資料流 www.2cto.com
*/
public InputStream streampost(String remote_addr){
    URL infoUrl =null;
    InputStream inStream =null;
    try{
        infoUrl =new URL(remote_addr);
        URLConnection connection = infoUrl.openConnection();
        HttpURLConnection httpConnection = (HttpURLConnection)connection;
        intresponseCode = httpConnection.getResponseCode();
        if(responseCode == HttpURLConnection.HTTP_OK){
            inStream = httpConnection.getInputStream();
        }
    }catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    returninStream;
}


摘自 虛懷若穀

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.