1

我是 android 编程新手。我想知道如何解析网页并将特定内容提取到 ListView 中。最简单和最好的方法是什么?有人可以用下面给出的例子给我看一个例子吗?

URL = "Something.com"。

我想提取每个城市的名称和href链接。

安娜堡
巴特尔克里克密歇根州
中部
底特律 地铁
弗林特
大急流城

谢谢你们,很抱歉提出这个基本问题。

4

3 回答 3

3

查看下面的代码,如果您有任何疑问,请告诉我,并查看此链接,它可能会对您有所帮助

http://wptrafficanalyzer.in/blog/android-lazy-loading-images-and-text-in-listview-from-http-json-data/

public class MainActivity extends Activity {

    ListView mListView;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // URL to the JSON data
        String strUrl = "ur url/countries";

        // Creating a new non-ui thread task to download json data
        DownloadTask downloadTask = new DownloadTask();

        // Starting the download process
        downloadTask.execute(strUrl);

        // Getting a reference to ListView of activity_main
        mListView = (ListView) findViewById(R.id.lv_countries);

    }

    /** A method to download json data from url */
    private String downloadUrl(String strUrl) throws IOException{
        String data = "";
        InputStream iStream = null;
        try{
            URL url = new URL(strUrl);

            // Creating an http connection to communicate with url
            HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();

            // Connecting to url
            urlConnection.connect();

            // Reading data from url
            iStream = urlConnection.getInputStream();

            BufferedReader br = new BufferedReader(new InputStreamReader(iStream));

            StringBuffer sb  = new StringBuffer();

            String line = "";
            while( ( line = br.readLine())  != null){
                sb.append(line);
            }

            data = sb.toString();

            br.close();

        }catch(Exception e){
            Log.d("Exception while downloading url", e.toString());
        }finally{
            iStream.close();
        }

        return data;
    }

    /** AsyncTask to download json data */
    private class DownloadTask extends AsyncTask<String, Integer, String>{
        String data = null;
        @Override
        protected String doInBackground(String... url) {
            try{
                data = downloadUrl(url[0]);
            }catch(Exception e){
                Log.d("Background Task",e.toString());
            }
            return data;
        }

        @Override
        protected void onPostExecute(String result) {

            // The parsing of the xml data is done in a non-ui thread
            ListViewLoaderTask listViewLoaderTask = new ListViewLoaderTask();

            // Start parsing xml data
            listViewLoaderTask.execute(result);
        }
    }

    /** AsyncTask to parse json data and load ListView */
    private class ListViewLoaderTask extends AsyncTask<String, Void, SimpleAdapter>{

        JSONObject jObject;
        // Doing the parsing of xml data in a non-ui thread
        @Override
        protected SimpleAdapter doInBackground(String... strJson) {
            try{
                jObject = new JSONObject(strJson[0]);
                CountryJSONParser countryJsonParser = new CountryJSONParser();
                countryJsonParser.parse(jObject);
            }catch(Exception e){
                Log.d("JSON Exception1",e.toString());
            }

            // Instantiating json parser class
            CountryJSONParser countryJsonParser = new CountryJSONParser();

            // A list object to store the parsed countries list
            List<HashMap<String, Object>> countries = null;

            try{
                // Getting the parsed data as a List construct
                countries = countryJsonParser.parse(jObject);
            }catch(Exception e){
                Log.d("Exception",e.toString());
            }

            // Keys used in Hashmap
            String[] from = { "country"

            // Ids of views in listview_layout
            int[] to = { R.id.tv_country};

            // Instantiating an adapter to store each items
            // R.layout.listview_layout defines the layout of each item
            SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), countries, R.layout.lv_layout, from, to);

            return adapter;
        }

        /** Invoked by the Android on "doInBackground" is executed */
        @Override
        protected void onPostExecute(SimpleAdapter adapter) {

            // Setting adapter for the listview
            mListView.setAdapter(adapter);

            for(int i=0;i<adapter.getCount();i++){
                HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(i);
                                   HashMap<String, Object> hmDownload = new HashMap<String, Object>();
                hm.put("flag_path",imgUrl);
                hm.put("position", i);


            }
        }
    }

             @Override
        protected void onPostExecute(HashMap<String, Object> result) {
            // Getting the path to the downloaded image
            String path = (String) result.get("flag");

            // Getting the position of the downloaded image
            int position = (Integer) result.get("position");

            // Getting adapter of the listview
            SimpleAdapter adapter = (SimpleAdapter ) mListView.getAdapter();

            // Getting the hashmap object at the specified position of the listview
            HashMap<String, Object> hm = (HashMap<String, Object>) adapter.getItem(position);

            // Overwriting the existing path in the adapter
            hm.put("flag",path);

            // Noticing listview about the dataset changes
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}
于 2013-01-04T06:37:35.627 回答
2

使用类似http://jsoup.org/的东西来获取 html 内容。

然后使用类似的东西

http://jsoup.org/cookbook/extracting-data/selector-syntax

提取网址。

然后

:matches(regex):查找文本与指定正则表达式匹配的元素;例如 div:matches((?i)login)

为您要查找的网址做一个正则表达式。

我不确定这是否是你想要的。

于 2013-01-04T06:35:07.223 回答
0

我记得我做过一次,幸运的是我找到了那个代码。您只需要从您的活动中调用此 Intentservice 并且您需要在顶部指定网站名称(在 url 变量中)

    public class parser extends IntentService {

public String url="http://www.mywebsite.com";

@Override
public void onDestroy() {
    // TODO Auto-generated method stub
    super.onDestroy();
//  shutdown();
}



@Override
public int onStartCommand(Intent intent, int flags, int startId) {
    // TODO Auto-generated method stub


    return super.onStartCommand(intent, flags, startId);
}


private Timer t = new Timer();
byte[] buffer;


public timeCell() {
    super("name"); 
    // TODO Auto-generated constructor stub
}


@Override
protected void onHandleIntent(Intent arg0) {
    // TODO Auto-generated method stub




        t.schedule(new TimerTask(){
            @Override
            public void run() {
                // TODO Auto-generated method stub
                  BufferedReader reader=null;
    try {
        reader = new BufferedReader(
                new InputStreamReader(
                    new URL(url).openStream()));
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    String line = null;
    try {
        line = reader.readLine();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }


    Log.v("line was ", line);  // printing is done here ;)


    }

    }
于 2013-01-04T06:46:42.467 回答