Android加载WebView - 使用post请求加载

本文介绍了如何在Android中使用post请求加载WebView,详细阐述了加载过程,并提及获取WebView的历史记录方法。同时,还讨论了如何通过get请求加载页面以及如何清空WebView的内容。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >


使用post请求加载WebView

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.google.android.gms.appindexing.Action;
import com.google.android.gms.appindexing.AppIndex;
import com.google.android.gms.appindexing.Thing;
import com.google.android.gms.common.api.GoogleApiClient;

import org.apache.http.util.EncodingUtils;

import java.util.Iterator;
import java.util.Set;
import java.util.TreeMap;

public class WebBusinessActivity extends BaseActivity implements View.OnClickListener {

    private String TAG = "WebBusinessActivity";
    private Context mContext;
    private SPUtil spUtil;

    private RequestQueue requestQueue;

    private WebView wv;
    private GoogleApiClient client;
    private String key_sort_url = "";
    private String substring_url = "";

    @Override
    protected void initActivity() {
        setContentView(R.layout.activity_web_business);
        mContext = this;
        spUtil = SPUtil.getInstance(mContext);
        requestQueue = Volley.newRequestQueue(mContext);

        initView();
        initWebView();
        API_Business_Center();
    }

    private void initWebView() {
        WebSettings settings = wv.getSettings();
        settings.setJavaScriptCanOpenWindowsAutomatically(true);//JS互调
        settings.setJavaScriptEnabled(true);
        settings.setDefaultTextEncodingName("utf-8");
        settings.setBuiltInZoomControls(true);
        settings.setSupportZoom(true);
        settings.setUseWideViewPort(true);
        settings.setLoadWithOverviewMode(true);
        settings.setDomStorageEnabled(true); // 开启 DOM storage API 功能
        settings.setDatabaseEnabled(true);
        wv.setWebViewClient(webViewClient);
    }

    private void API_Business_Center() {
        // 请求网络
    }
        
    // 请求到数据后的处理
    VolleyRequest volleyRequest = new VolleyRequest() {
        @Override
        protected void onMyResponse(String response) {
            _Return Re = new _Return(response);
            if (Re.isSuccess(mContext)) {
                _WebViewMortgage webViewMortgage = new _WebViewMortgage(Re.getData());
                initUrl(wv, webViewMortgage.getUrl(), webViewMortgage.getApi());
            }
        }
    };

    private void initUrl(WebView webView, String countUrl, String countApi) {
        TreeMap<String, String> params = geturl(countApi);
        //        UtilsTools.Log_e(TAG, "要传的map里的数据" + params.toString());
        Set<String> keySet = params.keySet();
        Iterator<String> iter = keySet.iterator();
        while (iter.hasNext()) {
            String key = iter.next();
            key_sort_url = key_sort_url + key + "=" + params.get(key) + "&";
            //            UtilsTools.Log_e(TAG, "遍历map拿到所有KEY+vaule拼成字符串" + key_sort_url);
            substring_url = key_sort_url.substring(0, key_sort_url.length() - 1);
            //            UtilsTools.Log_e(TAG, "截取字符串:" + substring_url);
        }
        webView.postUrl(countUrl, EncodingUtils.getBytes(substring_url, "BASE64"));
    }

    private TreeMap<String, String> geturl(String countApi) {
        TreeMap<String, String> map = new TreeMap();
        map.put("xxx", "xxxxxxxxx");
        return map;
    }


    private void initView() {
        TextView tv_title_bar_title = (TextView) findViewById(R.id.tv_title_bar_title);
        tv_title_bar_title.setVisibility(View.VISIBLE);
        tv_title_bar_title.setText("标题");

        ImageView iv_title_bar_left = (ImageView) findViewById(R.id.iv_title_bar_left);
        iv_title_bar_left.setVisibility(View.VISIBLE);
        iv_title_bar_left.setImageResource(R.mipmap.ic_back);
        iv_title_bar_left.setOnClickListener(this);

        wv = (WebView) findViewById(R.id.wv_main_mortgage);
    }

    WebViewClient webViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String request) {
            view.loadUrl(request);
            return true;
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // ATTENTION: This was auto-generated to implement the App Indexing API.
        // See https://g.co/AppIndexing/AndroidStudio for more information.
        client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
    }

    /**
     * ATTENTION: This was auto-generated to implement the App Indexing API.
     * See https://g.co/AppIndexing/AndroidStudio for more information.
     */
    public Action getIndexApiAction() {
        Thing object = new Thing.Builder()
                .setName("WebViewMortgage Page") // TODO: Define a title for the content shown.
                // TODO: Make sure this auto-generated URL is correct.
                .setUrl(Uri.parse("http://[ENTER-YOUR-URL-HERE]"))
                .build();
        return new Action.Builder(Action.TYPE_VIEW)
                .setObject(object)
                .setActionStatus(Action.STATUS_TYPE_COMPLETED)
                .build();
    }

    @Override
    public void onStart() {
        super.onStart();

        client.connect();
        AppIndex.AppIndexApi.start(client, getIndexApiAction());
    }

    @Override
    public void onStop() {
        super.onStop();
        
        AppIndex.AppIndexApi.end(client, getIndexApiAction());
        client.disconnect();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_title_bar_left:
                finish();
                break;
        }
    }
}
  • 布局 :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.yiban.caesar.Activity.Main.WebBusinessActivity">

    <include layout="@layout/layout_title_bar"/>

    <WebView
        android:id="@+id/wv_main_mortgage"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></WebView>
</LinearLayout>

获取webview已经打开的网址记录

WebBackForwardList historyList = mWebviewContent.copyBackForwardList();
for (int i = 0; i < historyList.getSize(); i++) {
    LogUtil.e(mTag, "历史" + (i + 1) + " --- " + historyList.getItemAtIndex(i).getUrl());
}
  • 打印结果为 :
历史1 --- https://xxxxxxxxxxxx
历史2 --- https://xxxxxxxxx
历史3 --- https://xxxxx
历史4 --- https://xxxxxxxxxxxxxxxxxxx
  • 注意 : 当前webview所属的Activity关闭以后 , 也就是onDestroy()之后 , 则下次重新打开此webview打印出来的历史记录就又从零开始了 .

使用get请求加载WebView


把webview页面设置为空白

  • 清除掉本webview里打开的所有网页
mWebviewContent.loadDataWithBaseURL(null, "", "text/html", "utf-8", null);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值