2016-04-12 152 views
2

當我嘗試使用webview加載網站時,頁面會加載。但是,該頁面不會加載所有內容。我得到了PHP部分和Youtubevídeo,但它沒有出現。這是我的代碼:Webview不會加載整個網站

public class ChatFragment extends Fragment { 


public ChatFragment() { 
    // Required empty public constructor 
} 


@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View view = inflater.inflate(R.layout.fragment_chat, container, false); 

    String url = "mywebpath/index.php"; // It has the http part 
    WebView webView = (WebView) view.findViewById(R.id.chat); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.loadUrl(url); 
    webView.setWebViewClient(new WebViewClient()); 
    webView.setWebChromeClient(new WebChromeClient()); 
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
    webView.getSettings().setRenderPriority(WebSettings.RenderPriority.HIGH); 
    webView.getSettings().setAllowFileAccess(true); 
    webView.getSettings().setJavaScriptEnabled(true); 
    webView.getSettings().setJavaScriptCanOpenWindowsAutomatically(true); 
    webView.getSettings().setPluginState(WebSettings.PluginState.ON); 
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 
    if (Build.VERSION.SDK_INT >= 19) { 
     webView.setLayerType(View.LAYER_TYPE_HARDWARE, null); 
    } 
    else { 
     webView.setLayerType(View.LAYER_TYPE_SOFTWARE, null); 
    } 
    webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE); 

    WebSettings webSettings = webView.getSettings(); 
    webView.getSettings().setLoadWithOverviewMode(true); 
    webView.getSettings().setUseWideViewPort(true); 
    webView.getSettings().setBuiltInZoomControls(true); 
    webView.getSettings().setPluginState(WebSettings.PluginState.ON); 

    return view; 
} 

這是網站:http://i.imgur.com/Tk8UIvL.jpg

這是web視圖結果:http://i.imgur.com/reDPlx1.png

感謝這麼多的關注

附:聊天開始於php部分,然後出現一個div。

回答

0

哦!你必須建立自己的網站的移動版本

EG:

vsersion瀏覽器:for Browser

版本手機:for mobile

+0

它的工作。非常感謝你的幫助! – NyanMe

0

試試吧!

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
        Bundle savedInstanceState) { 
View view = inflater.inflate(R.layout.fragment_chat, container, false); 
String url = "mywebpath/index.php"; // It has the http part 
WebView mWebView = (WebView) view.findViewById(R.id.chat); 
mWebView.getSettings().setJavaScriptEnabled(true); 
mWebView.getSettings().getPluginState(); 
if (Build.VERSION.SDK_INT < 8) { 
mWebView.getSettings().setPluginsEnabled(true); 
} else { 
mWebView.getSettings().setPluginState(PluginState.ON); 
} 
mWebView.setWebViewClient(new WebViewClient() { 
     @Override 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 
      view.loadUrl(url); 
      return true; 
     } 
} 
mWebView.loadUrl(url); 
return view; 
} 

在AndroidMainfest

<uses-permission android:name="android.permission.INTERNET"/> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> 
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/> 

,如果它不能正常工作。嘗試這個。 (替換mWebview.loadUrl)

mWebview.loadData(url, "text/html", "utf-8"); 

或者

mWebview.loadDataWithBaseURL("", url, "text/html", "UTF-8", "");