2012-11-22 35 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”

干杯!