我的清单中有互联网权限,我试图在我的网页浏览的搜索栏上搜索“facebook”,但问题是,对于50%的搜索,它不会加载搜索请求!这太奇怪了,令人沮丧。我正确地实现了一切,并检查了一切3次,但没有运气。每次我在其他浏览器上搜索相同的内容时,它总是有效。但是当我在我的webview上搜索时,它有时会加载页面,并且大多数时候,进度条显示加载,并且就像页面实际加载一样消失,但它仍然是白色的。为什么会这样?
public class MainActivity extends AppCompatActivity {
private EditText searchEditText;
private ProgressBar progressBar;
private WebView webView;
private String SearchString = "https://www.google.co.in/search?q=";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar = (ProgressBar) findViewById(R.id.progressBar);
progressBar.setMax(100);
webView = (WebView) findViewById(R.id.webView);
webView.setWebViewClient(new WebViewClientDemo());
webView.setWebChromeClient(new WebChromeClientDemo());
webView.getSettings().setJavaScriptEnabled(true);
webView.loadUrl("about:blank");
// use keyboard go button to search
searchEditText = (EditText) findViewById(R.id.searchfield);
searchEditText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
public boolean onEditorAction(TextView arg0, int arg1, KeyEvent arg2) {
CustomSearch();
return false;
}
});
}
private class WebViewClientDemo extends WebViewClient {
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
@Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
progressBar.setVisibility(View.GONE);
progressBar.setProgress(100);
}
@Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
progressBar.setVisibility(View.VISIBLE);
progressBar.setProgress(0);
}
@Override
public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) {
Toast.makeText(getBaseContext(), "page error. please try again!", Toast.LENGTH_SHORT).show();
super.onReceivedError(view, errorCode, description, failingUrl);
}
}
private class WebChromeClientDemo extends WebChromeClient {
public void onProgressChanged(WebView view, int progress) {
progressBar.setProgress(progress);
}
public void CustomSearch() {
searchEditText = (EditText) findViewById(R.id.searchfield);
String url = searchEditText.getText().toString();
url = SearchString + url.replace(" ", "+");// replace_spaces_with_a_"+"_sign(prefix)
webView.loadUrl(url);
}
答案 0 :(得分:0)
您是否尝试对搜索文本进行编码,根据我的理解,您需要对搜索字符串进行编码。