添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
纯真的柑橘  ·  iOS WKWebView详解及JS ...·  3 天前    · 
路过的小熊猫  ·  [Python] ...·  5 月前    · 
愉快的柚子  ·  Tuning - Spark 3.5.1 ...·  7 月前    · 
I am going to discuss about how to handle results if url called via
webview, and another MOST IMPORTANT task is to handle connection timeout.

Here we GO…

web_view.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <WebView android:id="@+id/my_webkit" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:gravity="center_vertical" android:orientation="vertical" > <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="URL" android:textAppearance="?android:attr/textAppearanceMedium" /> <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="Enter URL for test timeout or result" android:inputType="textUri" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Load" /> </LinearLayout>

MainActivity.java

import android.app.Activity; import android.app.ProgressDialog; import android.content.Context; import android.content.Intent; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.text.Editable; import android.text.TextUtils; import android.text.TextWatcher; import android.util.Log; import android.view.View; import android.view.View.OnClickListener; import android.webkit.URLUtil; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private static final int REQUEST_CODE = 1000; private Button mButtonUrl = null; private EditText mEditTextUrl = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); initComponents(); addListener(); private void initComponents() { mButtonUrl = (Button) findViewById(R.id.button1); mEditTextUrl = (EditText) findViewById(R.id.editText1); private void addListener() { mButtonUrl.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { //Validate URL if (TextUtils.isEmpty(mEditTextUrl.getText())) { mEditTextUrl.setError("Server URL can not be empty..."); } else { if (URLUtil.isValidUrl(mEditTextUrl.getText().toString())) { WebviewActivity.callWebview(MainActivity.this, mEditTextUrl.getText().toString().trim(), REQUEST_CODE); } else { mEditTextUrl.setError("Invalid URL..."); mEditTextUrl.addTextChangedListener(new TextWatcher() { @Override public void onTextChanged(CharSequence s, int start, int before, int count) { if(s != null && s.length() > 0 && mEditTextUrl.getError() != null) { mEditTextUrl.setError(null); @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) {} @Override public void afterTextChanged(Editable s) {} @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if(requestCode == REQUEST_CODE) { if(resultCode == Activity.RESULT_OK) { //Got something from server. Read that. new WebviewResultReader(MainActivity.this).execute(data); public class WebviewResultReader extends AsyncTask<Intent, Void, Void> { private Context mContext; private ProgressDialog mProgressDialog; public WebviewResultReader(Context mContext) { this.mContext = mContext; this.mProgressDialog = new ProgressDialog(this.mContext); this.mProgressDialog.setTitle("Wait"); this.mProgressDialog.setMessage("Reading result..."); @Override protected void onPreExecute() { this.mProgressDialog.show(); super.onPreExecute(); @Override protected void onPostExecute(Void result) { if (this.mProgressDialog != null) this.mProgressDialog.dismiss(); @Override protected Void doInBackground(Intent... intents) { Uri uri = intents[0].getData(); //TODO write your logic here to read data came from webview. Log.i("MY_TAG", "Got response from server : \n" + uri.toString()); return null;

WebviewActivity.java

import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; import android.graphics.Bitmap; import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.text.format.Time; import android.util.Log; import android.webkit.WebChromeClient; import android.webkit.WebSettings; import android.webkit.WebView; import android.webkit.WebViewClient; @SuppressLint("SetJavaScriptEnabled") public class WebviewActivity extends Activity { private WebView browser = null; private static final String TAG = "WebviewActivity"; private String requestedURL = null; private ConnectionTimeoutHandler timeoutHandler = null; private static int PAGE_LOAD_PROGRESS = 0; public static final String KEY_REQUESTED_URL = "requested_url"; * A string which will be added in url if server want to send something to client after everything has been done. * When you load any url to webview and after task done it doen't go back to caller activity. User must have to press back key. * So what we do, we will check url each time for a string if loading url contains the string, we assume that we got result. * In my example my server returns success string into url. * for example: * I called http://192.../test, after doing some task on server, server moves to http://192.../test/success, so * I read this string and say my code that I got result, move me back. public static final String CALLBACK_URL = "success"; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.web_view); initComponents(); addListener(); requestedURL = getIntent().getStringExtra(KEY_REQUESTED_URL); if (requestedURL != null) { browser.loadUrl(requestedURL); } else { Log.e(TAG, "Can not process ... URL missing."); public void initComponents() { browser = (WebView) findViewById(R.id.webkit); WebSettings webSettings = browser.getSettings(); webSettings.setJavaScriptEnabled(true); public void addListener() { browser.setWebViewClient(new MyWebViewClient()); browser.setWebChromeClient(new MyWebChromeClient()); //Custom web view client public class MyWebViewClient extends WebViewClient { @Override public boolean shouldOverrideUrlLoading(WebView view, String url) { Log.i(TAG, "Loading url : " + url); if (url.contains(CALLBACK_URL)) { Log.i(TAG, "Callback url matched... Handling the result"); Intent intent = new Intent(); Uri uri = Uri.parse(url); intent.setData(uri); setResult(RESULT_OK, intent); finish(); return true; return false; @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { timeoutHandler = new ConnectionTimeoutHandler(WebviewActivity.this, view); timeoutHandler.execute(); super.onPageStarted(view, url, favicon); @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); if (timeoutHandler != null) timeoutHandler.cancel(true); Log.i(TAG, "Loading url : " + url); // Do all your result processing here if (url.contains(CALLBACK_URL)) { Log.i(TAG, "Callback url matched... Handle result"); Intent mIntent = new Intent(); Uri uri = Uri.parse(url); mIntent.setData(uri); setResult(RESULT_OK, mIntent); finish(); @Override public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { Log.i(TAG, "GOT Page error : code : " + errorCode + " Desc : " + description); showError(WebviewActivity.this, errorCode); //TODO We can show customized HTML page when page not found/ or server not found error. super.onReceivedError(view, errorCode, description, failingUrl); //Custom web chrome client public class MyWebChromeClient extends WebChromeClient { @Override public void onProgressChanged(WebView view, int newProgress) { PAGE_LOAD_PROGRESS = newProgress; //Log.i(TAG, "Page progress [" + PAGE_LOAD_PROGRESS + "%]"); super.onProgressChanged(view, newProgress); private void showError(Context mContext, int errorCode) { //Prepare message String message = null; String title = null; if (errorCode == WebViewClient.ERROR_AUTHENTICATION) { message = "User authentication failed on server"; title = "Auth Error"; } else if (errorCode == WebViewClient.ERROR_TIMEOUT) { message = "The server is taking too much time to communicate. Try again later."; title = "Connection Timeout"; } else if (errorCode == WebViewClient.ERROR_TOO_MANY_REQUESTS) { message = "Too many requests during this load"; title = "Too Many Requests"; } else if (errorCode == WebViewClient.ERROR_UNKNOWN) { message = "Generic error"; title = "Unknown Error"; } else if (errorCode == WebViewClient.ERROR_BAD_URL) { message = "Check entered URL.."; title = "Malformed URL"; } else if (errorCode == WebViewClient.ERROR_CONNECT) { message = "Failed to connect to the server"; title = "Connection"; } else if (errorCode == WebViewClient.ERROR_FAILED_SSL_HANDSHAKE) { message = "Failed to perform SSL handshake"; title = "SSL Handshake Failed"; } else if (errorCode == WebViewClient.ERROR_HOST_LOOKUP) { message = "Server or proxy hostname lookup failed"; title = "Host Lookup Error"; } else if (errorCode == WebViewClient.ERROR_PROXY_AUTHENTICATION) { message = "User authentication failed on proxy"; title = "Proxy Auth Error"; } else if (errorCode == WebViewClient.ERROR_REDIRECT_LOOP) { message = "Too many redirects"; title = "Redirect Loop Error"; } else if (errorCode == WebViewClient.ERROR_UNSUPPORTED_AUTH_SCHEME) { message = "Unsupported authentication scheme (not basic or digest)"; title = "Auth Scheme Error"; } else if (errorCode == WebViewClient.ERROR_UNSUPPORTED_SCHEME) { message = "Unsupported URI scheme"; title = "URI Scheme Error"; } else if (errorCode == WebViewClient.ERROR_FILE) { message = "Generic file error"; title = "File"; } else if (errorCode == WebViewClient.ERROR_FILE_NOT_FOUND) { message = "File not found"; title = "File"; } else if (errorCode == WebViewClient.ERROR_IO) { message = "The server failed to communicate. Try again later."; title = "IO Error"; if (message != null) { new AlertDialog.Builder(mContext) .setMessage(message) .setTitle(title) .setIcon(android.R.drawable.ic_dialog_alert) .setCancelable(false) .setPositiveButton("OK", new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int id) { setResult(RESULT_CANCELED); finish(); }).show(); public class ConnectionTimeoutHandler extends AsyncTask<Void, Void, String> { private static final String PAGE_LOADED = "PAGE_LOADED"; private static final String CONNECTION_TIMEOUT = "CONNECTION_TIMEOUT"; private static final long CONNECTION_TIMEOUT_UNIT = 60000L; //1 minute private Context mContext = null; private WebView webView; private Time startTime = new Time(); private Time currentTime = new Time(); private Boolean loaded = false; public ConnectionTimeoutHandler(Context mContext, WebView webView) { this.mContext = mContext; this.webView = webView; @Override protected void onPreExecute() { super.onPreExecute(); this.startTime.setToNow(); WebviewActivity.PAGE_LOAD_PROGRESS = 0; @Override protected void onPostExecute(String result) { if (CONNECTION_TIMEOUT.equalsIgnoreCase(result)) { showError(this.mContext, WebViewClient.ERROR_TIMEOUT); this.webView.stopLoading(); } else if (PAGE_LOADED.equalsIgnoreCase(result)) { //Toast.makeText(this.mContext, "Page load success", Toast.LENGTH_LONG).show(); } else { //Handle unknown events here @Override protected String doInBackground(Void... params) { while (! loaded) { currentTime.setToNow(); if (WebviewActivity.PAGE_LOAD_PROGRESS != 100 && (currentTime.toMillis(true) - startTime.toMillis(true)) > CONNECTION_TIMEOUT_UNIT) { return CONNECTION_TIMEOUT; } else if (WebviewActivity.PAGE_LOAD_PROGRESS == 100) { loaded = true; return PAGE_LOADED; public static void callWebview(Activity activity, String requestedURL, int requestCode) { Intent intent = new Intent(activity, WebviewActivity.class); intent.putExtra(WebviewActivity.KEY_REQUESTED_URL, requestedURL); activity.startActivityForResult(intent, requestCode);

We did it…….

Happy coading ...
Privacy & Cookies: This site uses cookies. By continuing to use this website, you agree to their use.
To find out more, including how to control cookies, see here: Cookie Policy