设置: 一个Android应用程序,它应该向您展示您的购物行为并总结在线总费用 我想分析网站上显示的数据,我不会在我的Android应用程序中使用webview来拥有这些数据。
问题: 因此,即应用程序应该能够检测用户在他的购物车中的内容,并且当他按下按钮以便购买"产品。
我读过JavaScriptInterfaces,但是我真的不了解javascript以及如何构建整个分析过程。如果你能给我发几个例子,或者解释一下这个过程,我会非常高兴!
答案 0 :(得分:1)
1)在按钮上执行点击检测执行以下操作:
让我举一个完整的例子。对于您的网站http://store.nike.com/ch/de_de/pd/mercurial-superfly-v-tech-craft-2-herren-fussballschuh-fur-normalen-rasen/pid-11229711/pgid-11626158
{
...
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(this), "ButtonRecognizer");
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
loadEvent(clickListener());
}
private void loadEvent(String javascript){
webview.loadUrl("javascript:"+javascript);
}
private String clickListener(){
return getButtons()+ "for(var i = 0; i < buttons.length; i++){\n" +
"\tbuttons[i].onclick = function(){ console.log('click worked.'); ButtonRecognizer.boundMethod('button clicked'); };\n" +
"}";
}
private String getButtons(){
return "var buttons = document.getElementsByClassName('add-to-cart'); console.log(buttons.length + ' buttons');\n";
}
});
webview.loadUrl("http://store.nike.com/ch/de_de/pd/mercurial-superfly-v-tech-cra\u200C\u200Bft-2-herren-fussballschuh-fur-normalen-rasen/pid-11229711/pgid-11626158");
...
}
class MyJavaScriptInterface {
private Context ctx;
MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}
@JavascriptInterface
public void boundMethod(String html) {
new AlertDialog.Builder(ctx).setTitle("HTML").setMessage("It worked")
.setPositiveButton(android.R.string.ok, null).setCancelable(false).create().show();
}
}
将按钮的onClick更改为您需要的内容。
按类获取元素(getElementsByClassName())按预期工作,但不:getElementById()。此外,可能需要用getElementsByName()替换getElementsByClassName(),例如在此网站上:https://www.digitec.ch/de/s1/product/lexon-flip-wecker-3522142。你应该把getElementsByName放在哪里(&#39; AddProductToCart&#39;)
信息:由于IDE,clickListener方法中的\ n和奇怪的字符串组合都是这样设置的。 (&#34;;&#34;在字符串中引起问题)。
2)实施网站数据分析,例如检测客户放入购物车的内容:
(代码采用由标签/名称等定义的网站特定部分,并在Android监视器内输出)。您必须为每个网站定义标签和名称。要获得它们,只需查看网站的HTML代码即可。
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.webkit.JavascriptInterface;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class ShoppingCartAnalyzerActivity extends AppCompatActivity {
String adidas_tag = "product";
String nike_tag = "ch4_cartItem";
String zalando_tag = "z-coast-fjord_article";
String digitec_tag = "item-description";
String adidas_shoppingcart_url = "https://www.adidas.ch/on/demandware.store/Sites-adidas-CH-Site/de_CH/Cart-Show";
String nike_shoppingcart_url = "https://secure-store.nike.com/ch/checkout/html/cart.jsp?";
String zalando_shoppingcart_url = "https://www.zalando_tag.ch/cart/";
String digitec_shoppingcart_url = "https://www.digitec.ch/";
WebView webview;
private String[] shoppingCartItems;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
webview = new WebView(this);
setContentView(webview);
webview.getSettings().setJavaScriptEnabled(true);
webview.getSettings().setDomStorageEnabled(true);
webview.addJavascriptInterface(new MyJavaScriptInterface(this), "ShoppingCartAnalyser");
webview.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
loadEvent(clickListener());
}
private void loadEvent(String javascript) {
webview.loadUrl("javascript:" + javascript);
}
private String clickListener() {
return getProducts() + "for(var i = 0; i < products.length; i++){\n" +
"\tconsole.log(products[i].innerText); ShoppingCartAnalyser.boundMethod(products[i].innerText,i,products.length); \n" +
"}";
}
private String getProducts() {
return "var products = document.getElementsByClassName('" + zalando_tag + "'); console.log(products.length + ' products');\n";
}
});
webview.loadUrl(zalando_shoppingcart_url);
}
private void displayItems() {
for (String shoppingCartItem : shoppingCartItems) {
Log.d("ShoppingCart", cleanString(shoppingCartItem));
}
}
private String cleanString(String shoppingCartItem) {
shoppingCartItem = shoppingCartItem.replace("Ändern", "");
shoppingCartItem = shoppingCartItem.replace("Entfernen", "");
shoppingCartItem = shoppingCartItem.replace("Bearbeiten", "");
shoppingCartItem = shoppingCartItem.replace("Löschen", "");
shoppingCartItem = shoppingCartItem.replace("icon-cart-minus", "");
shoppingCartItem = shoppingCartItem.replace("icon-cart-plus", "");
shoppingCartItem = shoppingCartItem.replace("Service + Zubehör", "");
shoppingCartItem = shoppingCartItem.replaceAll("(?m)^[ \t]*\r?\n", "");
return shoppingCartItem;
}
class MyJavaScriptInterface {
private Context ctx;
MyJavaScriptInterface(Context ctx) {
this.ctx = ctx;
}
@JavascriptInterface
public void boundMethod(String decodedShoppingCart, int i, int size) {
if (i == 0) {
shoppingCartItems = new String[size];
}
shoppingCartItems[i] = decodedShoppingCart;
if (i == size - 1) {
displayItems();
}
}
}
}
答案 1 :(得分:0)
您可以参考this了解如何获取网页浏览的HTML内容。 至于解析HTML内容,XmlPullParser要做到这一点。