2013-11-26 94 views
0

調用Web服務,我不能夠調用Web服務或正在顯示任何警報:無法從網頁視圖

我的活動代碼:

mWebView = (WebView)findViewById(R.id.webViewRootAciviy); 
mWebView.setWebViewClient(new WebViewClient()); 
mWebView.setWebChromeClient(new WebChromeClient()); 
mWebView.loadUrl("file:///android_asset/splashscreen.html"); 

的html代碼:

<html> 
    <body> 
     this is a demo html file. 
     <script type="text/javascript" src="http://code.jquery.com/jquery-1.7.1.min.js"> 
      $.ajax({ 
       type: 'GET', 
       url: 'http://192.168.11.50/ar/service.svc/ProductCategories?$format=json', 
       dataType: "json", 
       contentType: "application/json; charset=utf-8", 
       success: function (data) 
       { 
        alert("success"); 
       }, 
       error: function (error) 
       { 
        alert("error"); 
       } 
      }); 
     </script> 
    </body> 
</html> 

也給了互聯網許可。請幫我看看我錯過了什麼。

編輯:

我們有自己的公司和業務服務器託管就可以了。我使用無線網絡訪問互聯網,服務器也通過以太網電纜連接到同一個無線路由器。

更新:我不知道爲什麼會發生這種情況。由於創建的服務類型,問題出現了。該Web服務是使用WCF Data service創建的,但是在使用普通的WCF Service再次創建Web服務後,該URL現在工作正常。

回答

0

將此應用於您的網絡瀏覽。

webView.setWebViewClient(new WebViewClient() { 
     public boolean shouldOverrideUrlLoading(WebView view, String url) { 

       view.loadUrl(url); 

       return true; 
      } else { 
       return false; 
      } 
     } 

     @Override 
     public void onPageFinished(WebView view, String url) { 
      // TODO Auto-generated method stub 
      super.onPageFinished(view, url); 

     } 
    }); 

    webView.getSettings().setJavaScriptEnabled(true); 

    webView.loadUrl(give your url); 
+0

對不起。不工作。 –

+0

我在想什麼? –

+0

評論此行可能是工作原理 mWebView.setWebChromeClient(new WebChromeClient()); – anddevmanu

0
// I used this class and my code is working fime at my side please try this may be it will help you 


    public class WebViewActivity extends Activity { 
     private WebView webview; 
     private static final String TAG = "Main"; 
     private ProgressDialog progressBar; 
     private TextView header_maintext; 
     private TextView headeroptiontext; 
     private RelativeLayout back; 
     private String url_string="http://www.google.com"; 
     private String header_maintext_string="Your text"; 

     /** Called when the activity is first created. */ 
     @SuppressLint("SetJavaScriptEnabled") @Override 
     public void onCreate(Bundle savedInstanceState) { 
      super.onCreate(savedInstanceState); 
      requestWindowFeature(Window.FEATURE_NO_TITLE); 
      setContentView(R.layout.webview_layout); 


      webview = (WebView)findViewById(R.id.webview01); 
      header_maintext= (TextView)findViewById(R.id.header_maintext); 
      header_maintext.setText(header_maintext_string); 

      headeroptiontext = (TextView)findViewById(R.id.headeroptiontext); 
      headeroptiontext.setVisibility(View.GONE); 

      WebSettings settings = webview.getSettings(); 
      settings.setJavaScriptEnabled(true); 
      webview.setScrollBarStyle(WebView.SCROLLBARS_OUTSIDE_OVERLAY); 
      webview.getSettings().setLoadWithOverviewMode(true); 
      webview.getSettings().setUseWideViewPort(true); 

       back = (RelativeLayout) findViewById(R.id.back_layout); 
       back.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) 
       { 
        // TODO Auto-generated method stub 
        if(webview.canGoBack() == true) 
         { 
          webview.goBack(); 
         } 
        else 
         { 
          finish(); 
         } 
       } 
      }); 

      final AlertDialog alertDialog = new AlertDialog.Builder(this).create(); 

      progressBar = ProgressDialog.show(WebViewActivity.this, "My application", "Loading..."); 

      webview.setWebViewClient(new WebViewClient() { 
       public boolean shouldOverrideUrlLoading(WebView view, String url) { 

        view.loadUrl(url); 
        return true; 
       } 

       public void onPageFinished(WebView view, String url) { 
        Log.i(TAG, "Finished loading URL: " +url); 
        if (progressBar.isShowing()) { 
         progressBar.dismiss(); 
        } 
       } 

       public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 

        Toast.makeText(WebViewActivity.this, "Oh no! " + description, Toast.LENGTH_SHORT).show(); 
        alertDialog.setTitle("Error"); 
        alertDialog.setMessage(description); 
        alertDialog.setButton("OK", new DialogInterface.OnClickListener() { 
         public void onClick(DialogInterface dialog, int which) { 
          return; 
         } 
        }); 
        alertDialog.show(); 
       } 
      }); 
      webview.loadUrl(url_string); 


     } 
     @Override 

     public boolean onKeyDown(int keyCode, KeyEvent event) { 
      if(event.getAction() == KeyEvent.ACTION_DOWN){ 
       switch(keyCode) 
       { 
       case KeyEvent.KEYCODE_BACK: 
        if(webview.canGoBack() == true){ 
         webview.goBack(); 
        }else{ 
         finish(); 
        } 
        return true; 
       } 

      } 
      return super.onKeyDown(keyCode, event); 
     } 
    }