Android 显示 WebView ,加载URL 时,向webview的 header 里面传递参数

1、主要布局 

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:fitsSystemWindows="true"
    tools:context=".MainActivity">

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

</RelativeLayout>

2、代码实现

 1 package com.webview.demo;
 2 
 3 import android.os.Bundle;
 4 import android.support.v7.app.AppCompatActivity;
 5 import android.webkit.WebView;
 6 
 7 import java.util.HashMap;
 8 import java.util.Map;
 9 
10 public class MainActivity extends AppCompatActivity {
11 
12     private WebView webView ;
13 
14     private String webViewHeaderKey = "tokenId" ;
15     private String webViewHeaderValue = "562142" ;
16 
17     private String url = "" ;
18 
19     @Override
20     protected void onCreate(Bundle savedInstanceState) {
21         super.onCreate(savedInstanceState);
22         setContentView(R.layout.activity_main);
23 
24         webView = (WebView) findViewById( R.id.webview );
25 
26 
27         if ( webViewHeaderValue != "" ){
28             Map<String, String > map = new HashMap<String, String>() ;
29             map.put( webViewHeaderKey , webViewHeaderValue ) ;
30 
31             webView.loadUrl( url  , map ) ;
32         }else {
33             webView.loadUrl( url ) ;
34         }
35 
36     }
37 }
原文地址:https://www.cnblogs.com/zhaoyanjun/p/4977929.html