这是我显示当前网页网址的代码。代码工作正常,但我想从url中获取特定值。
我关注this post但没有帮助我。代码在运行时爆炸。浏览playstore
时,我只想 Id public class WebViewClientDemoActivity extends Activity {
/** Called when the activity is first created. */
WebView web;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
web = (WebView) findViewById(R.id.webview01);
web.setWebViewClient(new myWebClient());
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("https://play.google.com/store/apps");
}
public class myWebClient extends WebViewClient
{
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
// TODO Auto-generated method stub
super.onPageStarted(view, url, favicon);
}
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
// TODO Auto-generated method stub
String[] splited = url.split("&");
String[] id = splited[0].split("?id=");
Toast.makeText (getApplicationContext(), id.toString(),
Toast.LENGTH_SHORT).show ();
view.loadUrl(url);
return true;
// this is example url i just want to get only id from which url
//https://play.google.com/store/apps/details?id=com.eamobile.monopoly_row_wf&feature
//=more_from_developer#?t=W251bGwsMSwyLDEwMiwiY29tLmVhbW9iaWxlLm1vbm9wb2x5X3Jvd193ZiJd
}
}
// To handle "Back" key press event for WebView to go back to previous screen.
@Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
if ((keyCode == KeyEvent.KEYCODE_BACK) && web.canGoBack()) {
web.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
}
答案 0 :(得分:0)
正如您在评论中提到的,您希望看到com.eamobile.monopoly_row_wf
因此,代码是
String standard = "https://play.google.com/store/apps/details?id=";
String id= url.substring(standard.length(), url.indexOf("&"));
Toast.makeText (getApplicationContext(), id.toString(), Toast.LENGTH_SHORT).show ();