1

我正在使用tawk.to在我的应用程序中插入在线聊天,只需简单WebView地加载聊天,但是当我输入它的 url 时tawk.to不会显示任何内容。对于任何其他 url,它都可以正常工作并加载页面,但不能加载聊天。我还尝试了页面“tawk.to”并加载。

public class ChatOnlineActivity extends AppCompatActivity {

WebView chatOnlineWebVIew;
ProgressBar loadingChat;
/*MORE CODE HERE*/

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_chat_online);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    String url="https://tawk.to/chat/597771645dfc8255d623ede4/default?fbclid=IwAR3QB_DOoEu9ePTSPJbayOcTIaWtXjuJMUn46qLFTRVNnpPZBTZhsOy6lrs";
    //String url="https://tawk.to/"; //JUST TEST URLS

    loadingChat=(ProgressBar)findViewById(R.id.loadingChatOnline);
    chatOnlineWebVIew = (WebView) findViewById(R.id.chatWebView);

    WebSettings chatSetting=chatOnlineWebVIew.getSettings();
    chatSetting.setJavaScriptEnabled(true);
    //chatSetting.setMixedContentMode(WebSettings.MIXED_CONTENT_ALWAYS_ALLOW);

    chatOnlineWebVIew.setWebViewClient(new chatWebClient());
    //chatOnlineWebVIew.getSettings().setMixedContentMode(WebSettings.MIXED_CONTENT_COMPATIBILITY_MODE);
    chatOnlineWebVIew.loadUrl(url);
    /*MORE CODE HERE*/


private class chatWebClient extends WebViewClient{
    @Override
    public boolean shouldOverrideKeyEvent(WebView view, KeyEvent event) {
        loadingChat.setVisibility(View.VISIBLE);
        return super.shouldOverrideKeyEvent(view, event);
    }

    @Override
    public void onPageStarted(WebView view, String url, Bitmap favicon) {
        super.onPageStarted(view, url, favicon);
    }

    @Override
    public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
        return super.shouldOverrideUrlLoading(view, request);
    }

    @Override
    public void onPageFinished(WebView view, String url) {
        loadingChat.setVisibility(View.GONE);
        chatOnlineWebVIew.setVisibility(View.VISIBLE);
        Toast.makeText(getApplicationContext(),url,Toast.LENGTH_LONG).show(); //just massage to test if the urls is correct
        super.onPageFinished(view, url);
    }
}
}
4

1 回答 1

0

我也面临同样的问题,我的 WebView 正在加载 url 但没有显示,但现在我通过添加以下代码行解决了我的问题

 WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);
    webSettings.setSupportZoom(true);
    webSettings.setDefaultTextEncodingName("utf-8");

我的最终活动如下所示

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;

import org.jetbrains.annotations.NotNull;

public class TawkToChatWebView extends AppCompatActivity {

private WebView webView;

Activity activity ;
private ProgressDialog progDailog;

@SuppressLint({"NewApi", "SetJavaScriptEnabled"})
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tawktochat);

    if (getSupportActionBar()!=null)
    {
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }

    webView=findViewById(R.id.wv_chat);
    activity = this;

    progDailog = ProgressDialog.show(activity, "Loading","Please wait...", true);
    progDailog.setCancelable(false);


    WebSettings webSettings = webView.getSettings();
    webSettings.setJavaScriptEnabled(true);
    webSettings.setDomStorageEnabled(true);
    webSettings.setLoadWithOverviewMode(true);
    webSettings.setUseWideViewPort(true);
    webSettings.setBuiltInZoomControls(true);
    webSettings.setDisplayZoomControls(false);
    webSettings.setSupportZoom(true);
    webSettings.setDefaultTextEncodingName("utf-8");

    webView.setWebViewClient(new WebViewClient(){

        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            progDailog.show();
            view.loadUrl(url);
            return true;
        }
        @Override
        public void onPageFinished(WebView view, final String url) {
            progDailog.dismiss();
            Log.e("loading","finished");
        }
    });

    webView.loadUrl("ADD_YOUR_DIRECT_CHAT_LINK_HERE");
}

@Override
public boolean onOptionsItemSelected(@NonNull @NotNull MenuItem item) {
    if (item.getItemId()==android.R.id.home)
    {
        finish();
    }
    return super.onOptionsItemSelected(item);
}
}
于 2021-10-23T10:53:27.590 回答