Android:loadDataWithBaseURL& shouldOverrideUrlLoading with Remote Websites

时间:2014-01-30 15:21:26

标签: android android-webview android-assets

经过几个小时的搜索来解决我的问题,我留下了发布我的问题,希望有人可以在这里提供帮助。

我的简单应用程序使用带有

的webView
loadDataWithBaseURL("file:///android_asset", source, "text/html", "utf-8", url) 

从远程网页获取源部分。 在我的应用程序中,我可以在第一页上访问存储在android_asset中的文件 - 正是在应用程序启动时。如果我使用锚点,表单发布请求或AJAX调用来浏览网站,那么对android_asset中存储的文件的访问就会消失。

我尝试了几个没有成功的东西 - 所以这是我的最后一个项目,其中第一页正在处理资产文件夹中的文件,但是其他每个页面都没有。

package com.myPackage.Test;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.concurrent.ExecutionException;

import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.annotation.TargetApi;
import android.app.Activity;
import android.view.Window;
import android.view.WindowManager;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceResponse;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

public class MainActivity extends Activity {
    final Activity activity = this;

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_NO_TITLE);
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
                                WindowManager.LayoutParams.FLAG_FULLSCREEN);

        setContentView(R.layout.activity_main);

        String url = "http://dev.xyz.com";
        String html = null;
        try {
            html = new RetrieveSiteData().execute(url).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }

        WebView view = (WebView) this.findViewById(R.id.webView1);
        WebSettings settings = view.getSettings();
        settings.setJavaScriptEnabled(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setAllowFileAccess(true);
        settings.setAllowFileAccessFromFileURLs(true);
        settings.setAllowUniversalAccessFromFileURLs(true);

        view.setWebChromeClient(new WebChromeClient() {
            public void onProgressChanged(WebView view, int progress)
            {
                activity.setTitle("Loading...");
                activity.setProgress(progress * 100);

                if(progress == 100)
                    activity.setTitle(R.string.app_name);
            }
        });

        view.setWebViewClient(new WebViewClient() {
            @Override
            public void onReceivedError(WebView view, int errorCode, String description, String failingUrl)
            {
                // Handle the error
            }

            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url)
            {
                if ( url.contains("dev.xyz.com") == true ) {
                    return false;
                }
                return true;
            }
        });

        view.loadDataWithBaseURL("file:///android_asset/", html, "text/html", "UTF-8", url);
    }

    public class RetrieveSiteData extends AsyncTask<String, Void, String> {
        @Override
        protected String doInBackground(String... urls) {
            StringBuilder builder = new StringBuilder(100000);

            for (String url : urls) {
                DefaultHttpClient client = new DefaultHttpClient();
                HttpGet httpGet = new HttpGet(url);
                try {
                    HttpResponse execute = client.execute(httpGet);
                    InputStream content = execute.getEntity().getContent();

                    BufferedReader buffer = new BufferedReader(new InputStreamReader(content));
                    String s = "";
                    while ((s = buffer.readLine()) != null) {
                        builder.append(s);
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            return builder.toString();
        }

        @Override
        protected void onPostExecute(String result) {

        }
    }
}

非常感谢提前。

0 个答案:

没有答案