【移动应用开发技术】时间戳以及jsoup应用_第1页
【移动应用开发技术】时间戳以及jsoup应用_第2页
【移动应用开发技术】时间戳以及jsoup应用_第3页
【移动应用开发技术】时间戳以及jsoup应用_第4页
【移动应用开发技术】时间戳以及jsoup应用_第5页
已阅读5页,还剩17页未读 继续免费阅读

下载本文档

版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领

文档简介

【移动应用开发技术】时间戳以及jsoup应用

以下是测试demo代码,可以下载附件自己测试

package

com.example.androidtest;

import

java.io.BufferedInputStream;

import

java.io.BufferedReader;

import

java.io.ByteArrayOutputStream;

import

java.io.DataInputStream;

import

java.io.File;

import

java.io.FileInputStream;

import

java.io.IOException;

import

java.io.InputStream;

import

java.io.InputStreamReader;

import

java.io.OutputStream;

import

.HttpURLConnection;

import

.MalformedURLException;

import

.URL;

import

.URLConnection;

import

org.apache.http.HttpResponse;

import

org.apache.http.client.HttpClient;

import

org.apache.http.client.methods.HttpPost;

import

org.apache.http.impl.client.DefaultHttpClient;

import

org.apache.http.util.ByteArrayBuffer;

import

org.apache.http.util.EncodingUtils;

import

org.jsoup.Jsoup;

import

org.jsoup.nodes.Document;

import

android.support.v7.app.ActionBarActivity;

import

android.util.Log;

import

android.view.LayoutInflater;

import

android.view.View;

import

android.view.View.OnClickListener;

import

android.webkit.WebView;

import

android.widget.TextView;

import

android.annotation.SuppressLint;

import

android.os.Bundle;

public

class

MainActivity

extends

ActionBarActivity

implements

OnClickListener

{

private

static

final

String

SUDOKU_RANKING_TXT

=

"15:8080/sudoku/ranking.txt";

URL

myUrl;

DataInputStream

dis;

View

main;

private

Document

doc;

private

TextView

t1;

@SuppressLint("NewApi")

@Override

public

void

onClick(View

v)

{

int

i

=

main.getSystemUiVisibility();

if

(i

==

View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)

{

main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_VISIBLE);

}

else

if

(i

==

View.SYSTEM_UI_FLAG_VISIBLE)

{

main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LOW_PROFILE);

}

else

if

(i

==

View.SYSTEM_UI_FLAG_LOW_PROFILE)

{

main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

}

}

@Override

protected

void

onCreate(Bundle

savedInstanceState)

{

super.onCreate(savedInstanceState);

main

=

LayoutInflater.from(this).inflate(R.layout.activity_main,

null);

//

main.setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);

//

main.setOnClickListener(this);

setContentView(main);

t1

=

(TextView)

findViewById(R.id.t1);

eee();

// WebView

broser

=

new

WebView(this);

}

private

void

ddd()

{

//

try

{

//

String

spec

=

"";

//

myUrl

=

new

URL(spec);

//

dis

=

new

DataInputStream(myUrl.openStream());

//

while

(dis.readLine()

!=

null)

{

//

String

readLine

=

dis.readLine();

//

System.out.println(readLine);

//

}

//

//

getHtmlString(spec);

//

dis.close();

//

}

catch

(Exception

e)

{

//

System.out.println("Error");

//

}

load();

//

SpannableString

ss

=

new

SpannableString("Click

here

to

");

//

ss.setSpan(new

StyleSpan(Typeface.BOLD),

0,

6,

//

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//

ss.setSpan(new

URLSpan(""),

14,

23,

//

Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);

//

t1.setMovementMethod(LinkMovementMethod.getInstance());

}

private

void

eee()

{

new

Thread(new

Runnable()

{

@Override

public

void

run()

{

HttpClient

httpClient

=

new

DefaultHttpClient();

HttpPost

request;

try

{

request

=

new

HttpPost(SUDOKU_RANKING_TXT);

HttpResponse

response

=

httpClient.execute(request);

if

(response.getStatusLine().getStatusCode()

==

200)

{

StringBuilder

builder

=

new

StringBuilder();

BufferedReader

reader

=

new

BufferedReader(new

InputStreamReader(response.getEntity().getContent()));

String

str2

=

"";

for

(String

s

=

reader.readLine();

s

!=

null;

s

=

reader.readLine())

{

builder.append(s);

}

Log.i("TAG",

"data

:"

+

builder.toString());

//

从网站获取信息

try

{

String

readParse

=

readParse(SUDOKU_RANKING_TXT);

Log.e("tag",

readParse);

URL

url

=

new

URL(SUDOKU_RANKING_TXT);

String

string

=

getUrl(url);

Log.e("tag",

string);

}

catch

(Exception

e)

{

e.printStackTrace();

}

}

}

catch

(Exception

e)

{

e.printStackTrace();

}

}

}).start();

}

/**

*

从指定的URL中获取数组

*

*

@param

urlPath

*

@return

*

@throws

Exception

*/

public

static

String

readParse(String

urlPath)

throws

Exception

{

ByteArrayOutputStream

outStream

=

new

ByteArrayOutputStream();

byte[]

data

=

new

byte[1024];

int

len

=

0;

URL

url

=

new

URL(urlPath);

HttpURLConnection

conn

=

(HttpURLConnection)

url.openConnection();

InputStream

inStream

=

conn.getInputStream();

while

((len

=

inStream.read(data))

!=

-1)

{

outStream.write(data,

0,

len);

}

inStream.close();

return

new

String(outStream.toByteArray());//

通过out.Stream.toByteArray获取到写的数据

}

private

String

getUrl(URL

url)

{

String

str

=

null;

try

{

URLConnection

uc

=

url.openConnection();

InputStream

is

=

uc.getInputStream();

BufferedInputStream

bis

=

new

BufferedInputStream(is);

ByteArrayBuffer

baf

=

new

ByteArrayBuffer(2048);

int

cun

=

0;

while

((cun

=

bis.read())

!=

-1)

{

baf.append((byte)

cun);

}

str

=

EncodingUtils.getString(baf.toByteArray(),

"UTF-8");

}

catch

(MalformedURLException

e)

{

e.printStackTrace();

}

catch

(IOException

e)

{

e.printStackTrace();

}

return

str;

}

protected

void

load()

{

try

{

doc

=

Jsoup.parse(new

URL(""),

5000);

Log.e("load",

doc.toString());

}

catch

(MalformedURLException

e1)

{

e1.printStackTrace();

}

catch

(IOException

e1)

{

e1.printStackTrace();

}

}

/**

*

@param

urlString

*

@return

*/

public

String

getHtmlString(String

urlString)

{

try

{

URL

url

=

null;

url

=

new

URL(urlString);

URLConnection

ucon

=

null;

ucon

=

url.openConnection();

InputStream

instr

=

null;

instr

=

ucon.getInputStream();

BufferedInputStream

bis

=

new

BufferedInputStream(instr);

ByteArrayBuffer

baf

=

new

ByteArrayBuffer(500);

int

current

=

0;

while

((current

=

bis.read())

!=

-1)

{

baf.append((byte)

current);

}

return

EncodingUtils.getString(baf.toByteArray(),

"gbk");

}

catch

(Exception

e)

{

return

"";

}

}

/**

*

jsoup提取src路径,下载网站图片

*

*

@author

Administrator

*

*/

/*

*

class

DownImages

{

private

static

int

COUNT

=

0;

private

static

int

*

DOWN_COUNT

=

0;

*

*

public

static

void

jsoupHTML(String

urlPath)

throws

Exception

{

Document

*

doc

=

Jsoup.connect(urlPath).timeout(1000000).get();

//

:当前页中的图片

Elements

*

srcLinks

=

doc.select("img[src$=.jpg]");

for

(Element

link

:

srcLinks)

{

*

//

:剔除标签,只剩链接路径

String

p_w_picpathsPath

=

link.attr("src");

*

System.out.println("当前访问路径:"

+

p_w_picpathsPath);

getImages(p_w_picpathsPath,

*

"d://p_w_picpaths//0000"

+

++COUNT

+

".jpg");

}

*

*

//

:提取网站中所有的href连接

Elements

linehrefs

=

doc.select("a[href]");

*

*

for

(Element

linehref

:

linehrefs)

{

String

lihr

=

linehref.attr("href");

*

if

(lihr.length()

>

4)

{

String

ht

=

lihr.substring(0,

4);

String

htt

=

*

lihr.substring(0,

1);

if

(!ht.equals("http")

&&

htt.equals("/"))

{

lihr

=

*

urlPath

+

lihr;

}

if

(lihr.substring(0,

4).equals("http"))

{

Document

*

docs

=

Jsoup.connect(lihr).timeout(1000000).get();

Elements

links

=

*

docs.select("img[src$=.jpg]");

for

(Element

link

:

links)

{

//

*

:剔除标签,只剩链接路径

String

p_w_picpathsPath

=

link.attr("src");

*

System.out.println("当前访问路径:"

+

p_w_picpathsPath);

getImages(p_w_picpathsPath,

*

"d://p_w_picpaths//0000"

+

COUNT++

+

".jpg");

}

}

}

}

}

*//**

*

@param

urlPath

*

图片路径

*

@throws

Exception

*/

/*

*

public

static

void

getImages(String

urlPath,

String

fileName)

throws

*

Exception

{

URL

url

=

new

URL(urlPath);//

:获取的路径

//

:http协议连接对象

*

HttpURLConnection

conn

=

(HttpURLConnection)

url.openConnection();

*

conn.setRequestMethod("GET");

conn.setReadTimeout(6

*

10000);

if

*

(conn.getResponseCode()

<

10000)

{

InputStream

inputStream

=

*

conn.getInputStream();

byte[]

data

=

readStream(inputStream);

if

*

(data.length

>

(1024

*

10))

{

FileOutputStream

outputStream

=

new

*

FileOutputStream(fileName);

outputStream.write(data);

*

System.err.println("第"

+

++DOWN_COUNT

+

"图片下载成功");

outputStream.close();

*

}

}

*

*

}

*//**

*

读取url中数据,并以字节的形式返回

*

*

@param

inputStream

*

@return

*

@throws

Exception

*/

/*

*

public

static

byte[]

readStream(InputStream

inputStream)

throws

Exception

*

{

ByteArrayOutputStream

outputStream

=

new

ByteArrayOutputStream();

*

byte[]

buffer

=

new

byte[1024];

int

len

=

-1;

while

((len

=

*

inputStream.read(buffer))

!=

-1)

{

outputStream.write(buffer,

0,

len);

}

*

outputStream.close();

inputStream.close();

return

*

outputStream.toByteArray();

}

*

*

public

static

void

main(String[]

args)

{

try

{

String

urlPath

=

*

"http://www.22mm.cc/";

jsoupHTML(urlPath);

}

catch

(Exception

e)

{

*

e.printStackTrace();

}

finally

{

System.out.println("共访问"

+

COUNT

+

*

"张图片,其中下载"

+

DOWN_COUNT

+

"张图片");

}

}

}

*

*

*

jsoup

httpclient

爬取网页并下载google图标

*

*

博客分类:

java

jsouphttpclient

jsoup下载地址

httpclient下载地址

*

/downloads.cgi

其他jar包见附件

*//**

*

google

logo

下载程序

*/

/*

*

public

abstract

class

Crawler

{

*//**

*

使用google

翻译api

*

*

@param

en

*

@return

*/

/*

*

public

String

translateEnToCinese(String

en)

{

*

Translate.setHttpReferrer("");

try

{

return

*

Translate.execute(en,

Language.ENGLISH,

Language.CHINESE);

}

catch

*

(Exception

e)

{

e.printStackTrace();

}

return

"";

}

*//**

*

获取一个Map

*

*

@return

*/

final

String

ENCORDING

=

"UTF-8";

public

boolean

upload(String

filepath)

throws

Exception

{

String

boundary

=

"7db1c523809b2";//

+java.util.UUID.randomUUID().toString();//

//

分割线

File

file

=

new

File(filepath);

String

fileName

=

new

String("哈哈嗨".getBytes(),

"ISO-8859-1");

//

用来解析主机名和端口

URL

url

=

new

URL("20/dev/index.php/Device/UploadFile?filename="

+

fileName

+

"&filetype=IMAGE");

//

用来开启连接

StringBuilder

sb

=

new

StringBuilder();

//

用来拼装请求

/*

*

//

username字段

sb.append("--"

+

boundary

+

"\r\n");

*

sb.append("Content-Disposition:

form-data;

name=\"username\""

+

*

"\r\n");

sb.append("\r\n");

sb.append(username

+

"\r\n");

*

*

//

password字段

sb.append("--"

+

boundary

+

"\r\n");

*

sb.append("Content-Disposition:

form-data;

name=\"password\""

+

*

"\r\n");

sb.append("\r\n");

sb.append(password

+

"\r\n");

*/

//

文件部分

sb.append("--"

+

boundary

+

"\r\n");

sb.append("Content-Disposition:

form-data;

name=\"file\";

filename=\""

+

filepath

+

"\""

+

"\r\n");

sb.append("Content-Type:

application/octet-stream"

+

"\r\n");

sb.append("\r\n");

//

将开头和结尾部分转为字节数组,因为设置Content-Type时长度是字节长度

byte[]

before

=

sb.toString().getBytes(ENCORDING);

byte[]

after

=

("\r\n--"

+

boundary

+

"--\r\n").getBytes(ENCORDING);

//

打开连接,

设置请求头

HttpURLConnection

conn

=

(HttpURLConnection)

url.openConnection();

conn.setConnectTimeout(10000);

conn.setRequestMethod("POST");

conn.setRequestProperty("Content-Type",

"multipart/form-data;

boundary="

+

boundary);

conn.setRequestProperty("Content-Length",

before.length

+

file.length()

+

after.length

+

"");

conn.setDoOutput(true);

conn.setDoInput(true);

//

获取输入输出流

OutputStream

out

=

conn.getOutputStream();

FileInputStream

fis

=

new

FileInputStream(file);

//

将开头部分写出

out.write(before);

//

写出文件数据

byte[]

buf

=

new

byte[1024

*

5];

int

len;

while

((len

=

fis.read(buf))

!=

-1)

out.write(buf,

0,

len);

//

将结尾部分写出

out.write(after);

InputStream

in

=

conn.getInputStream();

InputStreamReader

isReader

=

new

InputStreamReader(in);

BufferedReader

bufReader

=

new

BufferedReader(isReader);

String

line

=

null;

String

data

=

"getResult=";

while

((line

=

bufReader.readLine())

!=

null)

data

+=

line;

Log.e("fromServer",

"result="

+

data);

boolean

sucess

=

conn.getResponseCode()

==

200;

in.close();

fis.close();

out.close();

conn.disconnect();

return

sucess;

}

/*

*

public

Map<String,

Object>

getMap()

{

return

new

HashMap<String,

*

Object>(0);

}

*//**

*

下载文件

*

*

@param

url

*

文件http地址

*

@param

dir

*

目标文件

*

@throws

IOException

*/

/*

*

public

void

downloadFile(String

url,

String

dir)

throws

Exception

{

*

DefaultHttpClient

httpClient

=

new

DefaultHttpClient();

*

HttpProtocolParams.setUserAgent(httpClient.getParams(),

*

"Mozilla/5.0

(Windows;

U;

Windows

NT

5.1;

zh-CN;

rv:)

Gecko/20100315

Firefox/3.5.9"

*

);

HttpGet

httpGet

=

new

HttpGet();

httpGet.setURI(new

*

.URI(url));

*

*

InputStream

input

=

null;

FileOutputStream

output

=

null;

try

{

*

HttpResponse

response

=

httpClient.execute(httpGet);

HttpEntity

entity

=

*

response.getEntity();

input

=

entity.getContent();

File

file

=

new

*

File(dir);

output

=

FileUtils.openOutputStream(file);

IOUtils.copy(input,

*

output);

}

catch

(Exception

e){

e.printStackTrace();

}

finally

{

*

IOUtils.closeQuietly(output);

IOUtils.closeQuietly(input);

}

}

*/

/**

*

处理GET请求,返回整个页面

*

*

@param

url

*

访问地址

*

@param

params

*

编码参数

*

@return

*

@throws

Exception

*/

/*

*

public

synchronized

String

doGet(String

url,

String...

params)

throws

*

Exception

{

DefaultHttpClient

httpClient

=

new

DefaultHttpClient();

//

*

创建httpClient实例

HttpProtocolParams.setUserAgent(httpClient.getParams(),

*

"Mozilla/5.0

(Windows;

U;

Windows

NT

5.1;

zh-CN;

rv:)

Gecko/20100315

Firefox/3.5.9"

*

);

String

charset

=

"UTF-8";

if

(null

!=

params

&&

params.length

>=

1)

{

*

charset

=

params[0];

}

HttpGet

httpGet

=

new

HttpGet();

//

创建get方法实例

*

String

content

=

"";

httpGet.setURI(new

.URI(url));

try

{

*

HttpResponse

response

=

httpClient.execute(httpGet);

//

执行请求,得到response对象

*

int

resStatu

=

response.getStatusLine().getStatusCode();

//

得到返回的状态码

if

*

(resStatu

==

HttpStatus.SC_OK)

{

//

200正常

HttpEntity

entity

=

*

response.getEntity();

//

获得相应的实体

if

(entity

!=

null)

{

//

*

使用EntityUtils的toString方法,传递默认编码,在EntityUtils中的默认编码是ISO-8859-1

content

=

*

EntityUtils.toString(entity,

charset);

}

}

}

catch

(Exception

e)

{

*

System.out.println("访问【"

+

url

+

"】出现异常!");

e.printStackTrace();

}

*

finally

{

//

关闭资源

httpGet.abort();

*

httpClient.getConnectionManager().shutdown();

}

return

content;

}

}

*//**

*

google

logo

下载程序

*/

/*

*

public

class

GoogleLogoCrawler

extends

Crawler

{

*

*

private

static

final

String

URL

=

*

"/google/year.php?key=%y&page=%p";

*

*

private

static

final

String

LOGO_URL

=

*

"/google/";

*

*

private

static

final

String[]

YEARS

=

new

String[]

{

//"1998",

"1999",

*

"2000",

//"2001",

"2002",

"2003",

"2004",

"2005",

"2006",

"2007",

"2008",

*

"2009",

"2010",

"2011",

"2012"

};

*

*

private

static

final

String

INDEX

=

*

"/google/year.php?key=%y";

*

*

private

static

final

String

DIR_PATH

=

"D:\\googlelogos\\";

*

*

public

void

doStart()

{

JSONArray

array

=

new

JSONArray();

for

(String

*

year

:

YEARS)

{

String

ind

=

INDEX.replaceAll("%y",

year);

int

pageCount

*

=

getPageCount(ind);

for

(int

i

=

1;

i

<

pageCount+1;

i++)

{

String

url

=

*

URL.replaceAll("%y",

year).replaceAll("%p",

i

+

"");

String

path

=

year

+

*

"_"

+

i;

start(url,

array,

DIR_PATH

+

path

+

"\\",

path);

}

}

try

{

*

FileUtils.writeStringToFile(new

File(DIR_PATH

+

"json"),

*

array.toString(),

"UTF-8");

}

catch

(IOException

e)

{

*

e.printStackTrace();

}

System.out.println(array);

}

*

*

public

int

getPageCount(String

url)

{

int

pageCount

=

1;

try

{

*

org.jsoup.nodes.Document

doc

=

Jsoup.connect(url).get();

*

*

String

els

=

doc.html().toString();

int

start

=

els.indexOf("总页数")

+

4;

*

String

temp

=

els.substring(start);

int

end

=

temp.indexOf(",");

*

pageCount

=

Integer.parseInt(els.substring(start,start+end));

*

System.out.println(pageCount);

}

catch

(IOException

e)

{

*

e.printStackTrace();

}

return

pageCount;

}

*

*

public

void

start(String

url,

JSONArray

array,

String

dir,

String

path)

{

*

try

{

String

content

=

super.doGet(url);

Document

doc

=

*

Jsoup.parse(content);

Elements

dds

=

doc.select(".img

img");

*

List<Map<String,

Object>>

list

=

new

ArrayList<Map<String,

Object>>(0);

*

for

(int

i

=

0;

i

<

dds.size();

i++)

{

Element

img

=

dds.get(i);

String

*

src

=

img.select("img").first().attr("src");

String

title

=

*

img.select("img").first().attr("title");

Map<String,

Object>

map

=

*

super.getMap();

*

*

map.put("url",

LOGO_URL

+

src);

map.put("title",

title);

*

*

list.add(map);

}

JSONArray

tempJsonArray

=

new

JSONArray();

for

*

(Map<String,

Object>

map

:

list)

{

JSONObject

jsonObject

=

new

*

JSONObject();

String

proxy

=

*

StringUtils.substringAfterLast(map.get("url")

.toString(),

".");

long

*

date

=

new

Date().getTime();

String

name

=

date

+

"."

+

proxy;

*

jsonObject.put("url",

map.get("url").toString());

jsonObject.put("dir",

*

name);

jsonObject.put("title",

map.get("title").toString());

*

*

//

翻译

//

String

dateZh

=

super.translateEnToCinese(map.get("date")

//

*

.toString());

//

String

titleZh

=

*

super.translateEnToCinese(map.get("title")

//

.toString());

//

*

json.put("title_zh_cn",

dateZh

+

"

-

"

+

titleZh);

*

*

//

下载图片

super.downloadFile(map.get("url").toString(),

dir

+

name);

*

tempJsonArray.put(jsonObject);

}

array.put(new

JSONObject().put(path,

*

tempJsonArray));

}

catch

(Exception

e)

{

e.printStackTrace();

}

}

*

*

public

static

void

main(String[]

args)

throws

Exception

{

new

*

GoogleLogoCrawler().doStart();

}

*

*

}

*/

}package

com.example.androidtest;

import

android.content.Context;

import

.ConnectivityManager;

import

.NetworkInfo;

/***

*

工具类,检查当前网络状态

*

*

@author

shuimu

*

*/

public

class

NetUtil

{

public

static

boolean

checkNet(Context

context)

{

//

获取手机所以连接管理对象(包括wi-fi,net等连接的管理)

ConnectivityManager

conn

=

(ConnectivityManager)

context.getSystemService(Context.CONNECTIVITY_SERVICE);

if

(conn

!=

null)

{

//

网络管理连接对象

NetworkInfo

info

=

conn.getActiveNetworkInfo();

if

(info

!=

null

&&

info.isConnected())

{

//

判断当前网络是否连接

if

(info.getState()

==

NetworkInfo.State.CONNECTED)

{

return

true;

}

}

}

return

false;

}

}package

com.example.androidtest;

import

java.sql.Timestamp;

import

java.text.SimpleDateFor

温馨提示

  • 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
  • 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
  • 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
  • 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
  • 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
  • 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
  • 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。

评论

0/150

提交评论