2012-11-22 34 views
4

我正在嘗試使用鈦工作室構建簡單的應用程序,它將加載google.com,但我沒有得到正確的結果。鈦的Webview和httpClient問題

1)我用WebView加載url,但沒有任何顯示,除了白色的屏幕。

2)我使用httpClient,但我總是得到錯誤,我在onerror函數中定義。

這兩種方法都粘貼在下面。我是否必須在tiApp.xml中進行更改,即interent權限?

請幫助我,我該如何做得更好?

注意:我正在使用代理Internet。問題出現在仿真器和設備上。

我加載下面從app.js

var win = Ti.UI.currentWindow;  
Ti.App.fireEvent('show_indicator'); //display loading spinner until webview gets loaded 
var fbUrl = "http://www.facebook.com"; 
var extwebview = Titanium.UI.createWebView({ 
    url:fbUrl, 
    backgroundColor:'#ccc' 
}); 
win.add(extwebview); //adding webview in current window 

extwebview.addEventListener('load', function() { 
    Ti.App.fireEvent('hide_indicator'); //Hide the Loading indicatr 
}); 

---------------方法2 js文件------------ -----

var url ="http://www.google.com.pk"; 
var xhr = Ti.Network.createHTTPClient({ 
onload: function() { 
    // I don't know what to write here. 
}, 
onerror: function(e) { 
    Ti.API.debug("STATUS: " + this.status); 
    Ti.API.debug("TEXT: " + this.responseText); 
    Ti.API.debug("ERROR: " + e.error); 
    alert('There was an error retrieving the remote data. Try again.'); 
    }, 
    timeout:50000 
}); 
xhr.open("GET", url); 
xhr.send(); 

回答

1

您的代碼方法1看起來不錯。你的載入事件是否會觸發?我建議你爲WebView上的「錯誤」事件添加一個處理程序,看看它是否被解僱?

extwebview.addEventListener('error', function(e) { 
    Ti.API.debug("Oh no!"); 
}); 

但是,對於404或其他HTTP相關錯誤,「錯誤」事件不會觸發。測試的另一個選項是爲您的webview設置html值,而不是加載外部URL,並確保您的WebView的其他一切正常工作,並且您可以在其中看到html。您的代理可能有問題。

var extwebview = Titanium.UI.createWebView({ 
    html:"<html><head></head><body><h1>YOU MADE IT!</h1></body></html>", 
    backgroundColor:'#ccc' 
}); 
0

你的第一個代碼工作正常,我不禁想到你的問題是你的win變量。

  • 您的窗口可能未打開?即在代碼結束時嘗試win.open()。

  • 您可以測試win是否正確填充窗口對象,Ti.UI.currentWindow只能在窗口從url實例化的某些情況下使用。

如果這些仍不起作用,您可以提供您的app.js文件中的代碼嗎?

0

你的問題是與網址...我瘋了這個曾經笑

http://www.google.com.pk

應該

http://www.google.com.pk

基本上如果你在你的視圖中有一個名爲detailwebview的webview,那麼你可以做

$ .detailwebview.url =「http://www.google.com.pk」

乾杯!