最近想要做一个关于彩票的项目,好用的彩票接口都要收费,免费的次数也有限,于是从网上找到相关开奖网页,想从网页的代码中获得想要的数据,直接上代码。
import android.app.Activity;
import android.os.Bundle;
import android.os.StrictMode;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class MainActivity extends Activity {
private String path = "http://baidu.lecai.com/lottery/draw/list/528/?agentId=5572";
ListView listview;
List<String> time=new ArrayList<>();
List<String> number=new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//加上这两句代码 好像可以在主线程中进行网络访问
/**************************************************/
StrictMode.setThreadPolicy(new StrictMode.ThreadPolicy.Builder()
.detectDiskReads().detectDiskWrites().detectNetwork()
.penaltyLog().build());
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder()
.detectLeakedSqlLiteObjects().detectLeakedClosableObjects()
.penaltyLog().penaltyDeath().build());
/**************************************************/
listview = (ListView) this.findViewById(R.id.ls);
try {
String htmlContent = HtmlService.getHtml(path);
//彩票开奖时间
Pattern p = Pattern.compile("<td class=\"td1\">(.*?)</td>");
Matcher m = p.matcher(htmlContent);
while(m.find()){
time.add(m.group(1));
}
time.remove(time.get(0));
ArrayAdapter<String> timeAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,time);
listview.setAdapter(timeAdapter);
} catch (Exception e) {
System.out.println("程序发生了异常");
}
//彩票开奖号码
// Pattern p = Pattern.compile("<span class=\"ball_1\">(.*?)</span>");
// Matcher m = p.matcher(htmlContent);
// while(m.find()){
// number.add(m.group(1));
// }
// ArrayAdapter<String> timeAdapter=new ArrayAdapter<String>(MainActivity.this,android.R.layout.simple_list_item_1,number);
// listview.setAdapter(timeAdapter);
//// textView.setText(m.group(1));
//
// } catch (Exception e) {
// // textView.setText("程序出现异常:"+e.toString());
// }
}
}
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class HtmlService {
public static String getHtml(String path) throws Exception {
// 通过网络地址创建URL对象
URL url = new URL(path);
// 根据URL
// 打开连接,URL.openConnection函数会根据URL的类型,返回不同的URLConnection子类的对象,这里URL是一个http,因此实际返回的是HttpURLConnection
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
// 设定URL的请求类别,有POST、GET 两类
conn.setRequestMethod("GET");
//设置从主机读取数据超时(单位:毫秒)
conn.setConnectTimeout(5000);
//设置连接主机超时(单位:毫秒)
conn.setReadTimeout(5000);
// 通过打开的连接读取的输入流,获取html数据
InputStream inStream = conn.getInputStream();
// 得到html的二进制数据
byte[] data = readInputStream(inStream);
// 是用指定的字符集解码指定的字节数组构造一个新的字符串
String html = new String(data, "utf-8");
return html;
}
/**
* 读取输入流,得到html的二进制数据
*
* @param inStream
* @return
* @throws Exception
*/
public static byte[] readInputStream(InputStream inStream) throws Exception {
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int len = 0;
while ((len = inStream.read(buffer)) != -1) {
outStream.write(buffer, 0, len);
}
inStream.close();
return outStream.toByteArray();
}
}
还需要导入几个jar包,jsuop.jar,org.apache.http jar包