package com.example.com.cs;
package com.example.com.cs;
import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.apache.http.util.ByteArrayBuffer;
import org.apache.http.util.EncodingUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.w3c.dom.Text;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebChromeClient;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Toast;
public class MainActivity extends Activity {
private WebView mWebView;
Document doc;
String mWebstring;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initTitle();
}
private void initTitle() {
mWebView = (WebView) findViewById(R.id.webView1);
String lin = mWebView.getSettings().getUserAgentString();
mWebView.getSettings().setUserAgentString(lin + " appName/fenXiao");
WebSettings settings = mWebView.getSettings();
mWebView.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY);
mWebView.setHorizontalScrollBarEnabled(false);
settings.setSupportZoom(true);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.setInitialScale(70);
mWebView.setHorizontalScrollbarOverlay(false);
mWebView.setWebChromeClient(new WebChromeClient() {
@Override
public void onReceivedTitle(WebView view, String title) {
super.onReceivedTitle(view, title);
}
// 进度条事件
@Override
public void onProgressChanged(WebView view, int newProgress) {
// TODO Auto-generated method stub
super.onProgressChanged(view, newProgress);
}
});
mWebView.setWebViewClient(new WebViewClient() {
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
mWebstring =url;//获取当前的url
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
}
});
this.initJsInterface();
String url = “url地址";
mWebView.loadUrl(url);
}
private void initJsInterface() {
this.mWebView.addJavascriptInterface(new Object() {
/**
* 关闭当前WebView
*/
@JavascriptInterface
public void closeWebView() {
finish();
}
@JavascriptInterface
public void toYL() {
}
@JavascriptInterface
public void toMyCentre() {
}
@JavascriptInterface
public void toMerchant(String im_name,String im_tel) {
String dsadString =im_name;
String dsadStrin1 =im_tel;
load();
}
}, "yunqiapp");
}
protected void load() {
try {
doc = Jsoup.parse(new URL(mWebstring), 5000);
} catch (MalformedURLException e1) {
e1.printStackTrace();
} catch (IOException e1) {
e1.printStackTrace();
}
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
Elements es = doc.getElementsByClass("group1");
for (Element e : es) {
Map<String, String> map = new HashMap<String, String>();
map.put("title", e.getElementsByTag("img").text());
map.put("href",e.getElementsByTag("img").attr("src"));//这里是才html获取的值
list.add(map);
}
}
/**
* @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 "";
}
}
}