解析html的解析步骤代码
package com.example.httputil;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.HttpParams;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import com.example.myapplication.myApplication;
import android.graphics.AvoidXfermode.Mode;
import android.os.Handler;
import android.os.Message;
import android.provider.OpenableColumns;
import android.util.Log;
public class FoodsItemBiz {
public List<FoodsItem> foodsItems = new ArrayList<FoodsItem>();
public FoodsItemBiz() {
// TODO Auto-generated constructor stub
}
public void jsoup_jiexi(String msg) {
FoodsItem foodsItem = null;
FileOutputStream fos = null;
org.jsoup.nodes.Document doc = Jsoup.parse(msg);
// Log.i("msg", msg.obj.toString());
Elements units = doc.getElementsByClass("pic");
Elements detail_units = doc.getElementsByClass("detail");
Log.i("detail_units", detail_units.size() + "");
Log.i("num", units.size() + "");
try {
fos = myApplication.GetContext().openFileOutput("food.html", 0);
try {
fos.write(msg.getBytes());
fos.flush();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
for (int i = 0; i < units.size(); i++) {
foodsItem = new FoodsItem();
Element detail_ele = detail_units.get(i);
Element unit_ele = units.get(i);
Elements detail_content = detail_ele
.getElementsByClass("subcontent");
String content = detail_content.get(0).text();
Elements personame_ele = detail_ele.getElementsByClass("subline");
String PersonnameAndDate = personame_ele.get(0).text();
String[] data = PersonnameAndDate.split("发");
String Personname = data[0];
String Date = "发" + data[1];
Element h1_a_ele = unit_ele.child(0);
String title = h1_a_ele.attr("title");
String link = h1_a_ele.attr("href");
Element imagelink_ele = h1_a_ele.getElementsByTag("img").get(0);
String imagelink = imagelink_ele.attr("data-src");
foodsItem.setTitle(title);
foodsItem.setLink(link);
foodsItem.setWriter(Personname);
foodsItem.setDate(Date);
foodsItem.setImgLink(imagelink);
foodsItem.setContent(content);
Log.i("title", title);
Log.i("link", link);
Log.i("Date", Date);
Log.i("personname", Personname);
Log.i("imagelink", imagelink);
Log.i("content", content);
foodsItems.add(foodsItem);
}
}
}
未写先上传保存。