【问题标题】:Webview and httpClient issues in titaniumTitan 中的 Webview 和 httpClient 问题
【发布时间】:2012-11-22 12:47:37
【问题描述】:

我正在尝试使用 Titanium Studio 构建简单的应用程序,该应用程序将加载 google.com,但我没有得到正确的结果。

1) 我使用 WebView 加载 url 但除了白屏没有显示。

2) 我使用了 httpClient,但总是遇到我在 onerror 函数中定义的错误。

两种方法都粘贴在下面。我是否必须在 tiApp.xml 中进行更改,即 interent 权限?

请帮帮我,我怎样才能做得更好?

注意:我正在使用代理互联网。模拟器和设备都出现问题。

我正在从 app.js 加载以下 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
});

---------------方法二------

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();

【问题讨论】:

    标签: webview titanium-mobile


    【解决方案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'
    });
    

    【讨论】:

      【解决方案2】:

      您的第一个代码运行良好,我不禁认为您的问题出在您的 win 变量上。

      • 您的窗口可能没有打开吗?即在代码末尾尝试 win.open() 。

      • 能否测试一下 win 是否正确填充了窗口对象,Ti.UI.currentWindow 只能在从 url 实例化窗口时的某些情况下使用。

      如果这些仍然不起作用,您能否提供 app.js 文件中的代码?

      【讨论】:

        【解决方案3】:

        你的问题出在网址上……我曾经为此发疯了,哈哈

        http://www.google.com.pk

        应该是

        http://www.google.com.pk

        基本上,如果您的视图中有一个名为 detailwebview 的网络视图,那么您可以这样做

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

        干杯!

        【讨论】:

          猜你喜欢
          • 2014-08-10
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2015-01-06
          • 1970-01-01
          • 1970-01-01
          • 2012-11-06
          • 2016-10-31
          相关资源
          最近更新 更多