English 中文(简体)
Android - Webview 只对初始请求应用信头
原标题:Android - Webview only applying headers to initial request

使用网络视图请求网络服务器内容, 但使用 mWebView.loadUrl( url1, 页眉) ; 只对初始请求应用信头, 而不是请求中的资源 。

任何想法,如何将信头也应用于资源请求?

问题回答

Not absolutely sure but you can try to override shouldOverrideUrlLoading(WebView view, String url) method and handle all redirects by starting mWebView.loadUrl(url, yourHeaders); Dont forget to return true in that overriden method.

首先,让我说,我不能相信 网络视图如此糟糕。

这是我为传递自定义信头所做的

public class CustomWebview extends WebView {



    public void loadWithHeaders(String url) {

        setWebViewClient(new WebViewClient() {

        @Override
        public WebResourceResponse shouldInterceptRequest(WebView view, String url) {
            //makes a custom http request, which allows you to add your own headers
            return customRequest(url);
        时 时
      时 时);

        loadUrl(url);
    时 时


    /**
    * Custom http request with headers
    * @param url
    * @return
    */
    private WebResourceResponse customRequest(String url) {

    try {

        OkHttpClient httpClient = new OkHttpClient();

        Request request = new Request.Builder()
                .url(url.trim())
                .addHeader("Header-Name",  "Android Sucks")
                .build();

        Response response = httpClient.newCall(request).execute();

        return new WebResourceResponse(
                "text/html", // You can set something other as default content-type
                "utf-8",  // Again, you can set another encoding as default
                response.body().byteStream()
        );
    时 时 catch (IOException e) {
        //return null to tell WebView we failed to fetch it WebView should try again.
        return null;
    时 时
时 时

时 时





相关问题
Android - ListView fling gesture triggers context menu

I m relatively new to Android development. I m developing an app with a ListView. I ve followed the info in #1338475 and have my app recognizing the fling gesture, but after the gesture is complete, ...

AsyncTask and error handling on Android

I m converting my code from using Handler to AsyncTask. The latter is great at what it does - asynchronous updates and handling of results in the main UI thread. What s unclear to me is how to handle ...

Android intent filter for a particular file extension?

I want to be able to download a file with a particular extension from the net, and have it passed to my application to deal with it, but I haven t been able to figure out the intent filter. The ...

Android & Web: What is the equivalent style for the web?

I am quite impressed by the workflow I follow when developing Android applications: Define a layout in an xml file and then write all the code in a code-behind style. Is there an equivalent style for ...

TiledLayer equivalent in Android [duplicate]

To draw landscapes, backgrounds with patterns etc, we used TiledLayer in J2ME. Is there an android counterpart for that. Does android provide an option to set such tiled patterns in the layout XML?

Using Repo with Msysgit

When following the Android Open Source Project instructions on installing repo for use with Git, after running the repo init command, I run into this error: /c/Users/Andrew Rabon/bin/repo: line ...

Android "single top" launch mode and onNewIntent method

I read in the Android documentation that by setting my Activity s launchMode property to singleTop OR by adding the FLAG_ACTIVITY_SINGLE_TOP flag to my Intent, that calling startActivity(intent) would ...

From Web Development to Android Development

I have pretty good skills in PHP , Mysql and Javascript for a junior developer. If I wanted to try my hand as Android Development do you think I might find it tough ? Also what new languages would I ...

热门标签