package com.zxjzzg.myReader.bookcity;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Vector;
public class getData {
//@SuppressWarnings("null")
public static Vector<String []> getData(String urls, String start, String end, String start1, String end1, int a)throws Exception{
Vector<String []> mydatas = new Vector<String[]>();
String []data_list = null ;//
String []data_list1 = null;
URL url = new URL(urls);
HttpURLConnection huc = (HttpURLConnection) url.openConnection();
huc.setRequestMethod("GET");
huc.setConnectTimeout(10 * 1000);
InputStream inStream = huc.getInputStream();
byte[] data = readInputStream(inStream);
String html = new String(data);
System.out.println("开始ASC2转换");
// html = new String(html.getBytes("UTF-8"));
// System.out.println(html);
int b1 = html.indexOf(start) + start.length()+5;
int b2 = html.indexOf(end,b1);
System.out.println("b1="+b1+",b2="+b2);
String filename = html.substring(b1,b2);
if(a == 1){
filename = filename.replace("<br>", "");
filename = filename.replace("</p>", "");
//filename = filename.replace("<p>", "");
System.out.println("filename==="+filename);
data_list = filename.split("<p>");
mydatas.add(data_list);
// for(int i = 0; i < data_list.length; i ++){
// System.out.println("data_===="+data_list[0][i]);
// }
}else if(a == 2){
filename = filename.replace("</p>", "");
filename = filename.replace("<p>", "");
filename = filename.replace(" ", "");
filename = filename.replace("<br>", "");
filename = filename.replace(" ", "");
System.out.println("filename=="+filename);
data_list = filename.split("书名:");
mydatas.add(data_list);
int c1 = html.indexOf(start1) + start1.length()+5;
int c2 = html.indexOf(end1,c1);
System.out.println("c1="+c1+",c2="+c2);
String myurl = html.substring(c1, c2);
//System.out.println("myurl=="+myurl);
myurl = myurl.replace("</p>", "");
myurl = myurl.replace("<p>", "");
myurl = myurl.replace("<br>", "");
myurl = myurl.replace(" ", "");
data_list1 = myurl.split("\n");
mydatas.add(data_list1);
}
return mydatas;
}
public static byte[] readInputStream (InputStream inStream)throws Exception{
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*1024];
int len = 0;
while((len = inStream.read(buffer)) != -1){
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
Android开发从零开始之HTML解析-Jsoup
最新推荐文章于 2013-04-04 11:08:07 发布