使用Jsoup帮助解析具有html标签的数据和解析网页

下载Jsoup包后导入

/**
* Created by YGW on 2016/4/17.
* 显示公告的详细内容
*/
public class
PressFragment extends Fragment{

@Override
public View onCreateView (LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {

View view=inflater.inflate (R.layout.ygw_announcement_item_specific,
null);
String content=getArguments ().getString
("content");
String title=getArguments ().getString
("title");
String time=getArguments ().getString ("time");

String apartment=getArguments ().getString ("apartment");

TextView tv_PressTitle=(TextView)view.findViewById
(R.id.PressTitle_item);
TextView
tv_PressContent=(TextView)view.findViewById (R.id.PressContent_Item);

tv_PressTitle.setText (Gonggaohtml(title));

tv_PressContent.setText (Gonggaohtml(content));

return view;
}
//
使用Jsoup解析,这里解决的为题是,解析<p>标签,把数据转化成String类型的
private String
Gonggaohtml(String html) {
Document doc = Jsoup.parse
(html);
Elements ps = doc.getElementsByTag ("p");
String
content = "";
for(Element p : ps) {
content = content
+ p.text ();
}
return content;
}

}

解析网页

下载Jsoup包后导入

/**
* Created by YGW on 2016/4/17.
* 显示公告的详细内容
*/
public class
PressFragment extends Fragment{

@Override
public View onCreateView (LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {

View view=inflater.inflate (R.layout.ygw_announcement_item_specific,
null);
String content=getArguments ().getString
("content");
String title=getArguments ().getString
("title");
String time=getArguments ().getString ("time");

String apartment=getArguments ().getString ("apartment");

TextView tv_PressTitle=(TextView)view.findViewById
(R.id.PressTitle_item);
TextView
tv_PressContent=(TextView)view.findViewById (R.id.PressContent_Item);

tv_PressTitle.setText (Gonggaohtml(title));

tv_PressContent.setText (Gonggaohtml(content));

return view;
}
//
使用Jsoup解析,这里解决的为题是,解析<p>标签,把数据转化成String类型的
private String
Gonggaohtml(String html) {
Document doc = Jsoup.parse
(html);
Elements ps = doc.getElementsByTag ("p");
String
content = "";
for(Element p : ps) {
content = content
+ p.text ();
}
return content;
}

}

解析网页



public class MainActivity extends AppCompatActivity {

private String url = "http://解析网页的Url";
    private Handler handler;
    private List<String> list_title;
    private List<String> list_content;
    private ListView lv_press;

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

lv_press=(ListView)findViewById (R.id.lv_press);
        new Thread(new Runnable() {

@Override
            public void run() {
                parseHtml();

}

}).start ();

list_title = new ArrayList<String> ();
        list_content= new ArrayList<String> ();
        handler = new Handler (){

@Override
            public void handleMessage (Message msg) {
                super.handleMessage (msg);
                if (msg.what == 0x123){

String title_value=msg.getData ().getString ("title_key");
                    String href_value=msg.getData ().getString ("href_key");
                      list_title.add (title_value);
                      list_content.add (href_value);
                    Log.i ("info", title_value + ":" + href_value);
                //    Log.i ("info", list_title.toString () + ":" + href_value);
                    lv_press.setAdapter (new PressListView (list_title,list_content, MainActivity.this));

}
            }
        };
        lv_press.setOnItemClickListener (new AdapterView.OnItemClickListener () {
            @Override
            public void onItemClick (AdapterView<?> parent, View view, int position, long id) {

String path=list_content.get (position);
                Intent intent = new Intent(Intent.ACTION_VIEW);

intent.setData(Uri.parse (path));

startActivity(intent);

}
        });

}
    protected void parseHtml() {
        try {
            Document doc = Jsoup.connect(url).get();
            Elements elements = doc.select("div.unit");//解析这个大布局里的div布局名unit

for (Element ele : elements) {
                String title = ele.getElementsByTag("h1").first().text();//解析所有h1标签
                String href = ele.getElementsByTag("a").first().attr("href");//所有href标签

Log.i ("info","-----------------"+href);
                if (!title.isEmpty ()){
                    Message msg= new Message ();
                    msg.what=0x123;
                    Bundle bundle = new Bundle ();
                    bundle.putString ("title_key",title);
                    bundle.putString ("href_key", href);
                    msg.setData (bundle);
                    handler.sendMessage (msg);
                }

}

} catch (IOException e) {

e.printStackTrace();
        }
    }
}

上一篇:Java——IO类,字符流读数据


下一篇:安卓通用shell大全