Titanium - HTTPClient:打开新的控制器 onload
Titanium - HTTPClient: open new controller onload
您好,我正在开发一个 android 应用程序测试,它使用 HTTP 协议与远程服务器和数据库进行通信。这是一个简单的登录页面。
现在我有两个控制器:index.js
和 newuser.js
。
这是index.xml
<Alloy>
<Window class="container">
<TableView>
<TableViewRow>
<TextField id="user" borderStyle="Ti.UI.INPUT_BORDERSTYLE_ROUNDED" color="#336699" top="10" left="10" width="250" height="60" />
</TableViewRow>
<TableViewRow>
<TextField id="password" borderStyle="Ti.UI.INPUT_BORDERSTYLE_ROUNDED" color="#336699" top="10" left="10" width="250" height="60" />
</TableViewRow>
<TableViewRow>
<Button id="loginBtn" onClick="doLogin" title="Login" />
</TableViewRow>
</TableView>
<Menu id="menu" platform="android">
<MenuItem id="addBook"
title="Nuovo Utente"
onClick="addNewuser"
showAsAction="Ti.Android.SHOW_AS_ACTION_IF_ROOM" />
</Menu>
<View id="view_indicator">
<ActivityIndicator id="ind" message="Loading..."/>
</View>
</Window>
</Alloy>
这是index.js
loginReq = Titanium.Network.createHTTPClient();
loginReq.onload = function() {
var json = this.responseText;
var response = JSON.parse(json);
if (response.logged == true) {
alert("it's all ok");
} else {
alert(response.message);
}
$.view_indicator.hide();
}
function doLogin(e) {
if ($.user.value != '' && $.password.value != '') {
loginReq.open("POST", "http://solimeo.sviluppofacile.it/login.php");
var params = {
username: $.user.value,
password: Ti.Utils.md5HexDigest($.password.value)
};
$.view_indicator.show();
loginReq.send(params);
} else {
alert("Username/Pasword are required");
};
$.user.blur();
$.password.blur();
}
function addNewuser () {
var addnewuser = Alloy.createController("newuser").getView();
addnewuser.open();
}
$.index.open();
使用此代码,一切正常。我的问题是当我尝试在 loginReq.onload
、index.js
.
中打开一个新控制器时
我创建了一个新控制器,loggedin.js
来显示有关用户的信息,我尝试打开它。但是控制台中有很多错误。我认为问题可能出在控制器上,但如果我调用 addNewuser()
函数,我会遇到同样的错误,该函数会调用一个经过测试的好控制器
错误是
[ERROR] : TiHttpClient: (TiHttpClient-1) [13616,13616] HTTP Error (java.net.SocketException): recvfrom failed: ECONNRESET (Connection reset by peer)
[ERROR] : TiHttpClient: java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
[ERROR] : TiHttpClient: at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
[ERROR] : TiHttpClient: at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
[ERROR] : TiHttpClient: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
[ERROR] : TiHttpClient: at java.net.PlainSocketImpl.access[=12=]0(PlainSocketImpl.java:46)
[ERROR] : TiHttpClient: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
[ERROR] : TiHttpClient: at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
[ERROR] : TiHttpClient: at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
[ERROR] : TiHttpClient: at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
[ERROR] : TiHttpClient: at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
[ERROR] : TiHttpClient: at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
[ERROR] : TiHttpClient: at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
[ERROR] : TiHttpClient: at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
[ERROR] : TiHttpClient: at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
[ERROR] : TiHttpClient: at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1296)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:677)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:637)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1328)
[ERROR] : TiHttpClient: at java.lang.Thread.run(Thread.java:841)
[ERROR] : TiHttpClient: Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
[ERROR] : TiHttpClient: at libcore.io.Posix.recvfromBytes(Native Method)
[ERROR] : TiHttpClient: at libcore.io.Posix.recvfrom(Posix.java:141)
[ERROR] : TiHttpClient: at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
[ERROR] : TiHttpClient: at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
[ERROR] : TiHttpClient: ... 19 more
我已经解决了问题。
我已将 $.view_indicator.hide();
移至 if...else
区块。
所以 loginReq.onload
的正确代码是
loginReq.onload = function() {
var json = this.responseText;
var response = JSON.parse(json);
if (response.logged == true) {
var args = {
name: response.name,
email: response.email
};
var loggedUser = Alloy.createController("loggedin", args).getView();
loggedUser.open();
$.view_indicator.hide();
} else {
alert(response.message);
$.view_indicator.hide();
}
}
您好,我正在开发一个 android 应用程序测试,它使用 HTTP 协议与远程服务器和数据库进行通信。这是一个简单的登录页面。
现在我有两个控制器:index.js
和 newuser.js
。
这是index.xml
<Alloy>
<Window class="container">
<TableView>
<TableViewRow>
<TextField id="user" borderStyle="Ti.UI.INPUT_BORDERSTYLE_ROUNDED" color="#336699" top="10" left="10" width="250" height="60" />
</TableViewRow>
<TableViewRow>
<TextField id="password" borderStyle="Ti.UI.INPUT_BORDERSTYLE_ROUNDED" color="#336699" top="10" left="10" width="250" height="60" />
</TableViewRow>
<TableViewRow>
<Button id="loginBtn" onClick="doLogin" title="Login" />
</TableViewRow>
</TableView>
<Menu id="menu" platform="android">
<MenuItem id="addBook"
title="Nuovo Utente"
onClick="addNewuser"
showAsAction="Ti.Android.SHOW_AS_ACTION_IF_ROOM" />
</Menu>
<View id="view_indicator">
<ActivityIndicator id="ind" message="Loading..."/>
</View>
</Window>
</Alloy>
这是index.js
loginReq = Titanium.Network.createHTTPClient();
loginReq.onload = function() {
var json = this.responseText;
var response = JSON.parse(json);
if (response.logged == true) {
alert("it's all ok");
} else {
alert(response.message);
}
$.view_indicator.hide();
}
function doLogin(e) {
if ($.user.value != '' && $.password.value != '') {
loginReq.open("POST", "http://solimeo.sviluppofacile.it/login.php");
var params = {
username: $.user.value,
password: Ti.Utils.md5HexDigest($.password.value)
};
$.view_indicator.show();
loginReq.send(params);
} else {
alert("Username/Pasword are required");
};
$.user.blur();
$.password.blur();
}
function addNewuser () {
var addnewuser = Alloy.createController("newuser").getView();
addnewuser.open();
}
$.index.open();
使用此代码,一切正常。我的问题是当我尝试在 loginReq.onload
、index.js
.
我创建了一个新控制器,loggedin.js
来显示有关用户的信息,我尝试打开它。但是控制台中有很多错误。我认为问题可能出在控制器上,但如果我调用 addNewuser()
函数,我会遇到同样的错误,该函数会调用一个经过测试的好控制器
错误是
[ERROR] : TiHttpClient: (TiHttpClient-1) [13616,13616] HTTP Error (java.net.SocketException): recvfrom failed: ECONNRESET (Connection reset by peer)
[ERROR] : TiHttpClient: java.net.SocketException: recvfrom failed: ECONNRESET (Connection reset by peer)
[ERROR] : TiHttpClient: at libcore.io.IoBridge.maybeThrowAfterRecvfrom(IoBridge.java:545)
[ERROR] : TiHttpClient: at libcore.io.IoBridge.recvfrom(IoBridge.java:509)
[ERROR] : TiHttpClient: at java.net.PlainSocketImpl.read(PlainSocketImpl.java:488)
[ERROR] : TiHttpClient: at java.net.PlainSocketImpl.access[=12=]0(PlainSocketImpl.java:46)
[ERROR] : TiHttpClient: at java.net.PlainSocketImpl$PlainSocketInputStream.read(PlainSocketImpl.java:240)
[ERROR] : TiHttpClient: at org.apache.http.impl.io.AbstractSessionInputBuffer.fillBuffer(AbstractSessionInputBuffer.java:103)
[ERROR] : TiHttpClient: at org.apache.http.impl.io.AbstractSessionInputBuffer.readLine(AbstractSessionInputBuffer.java:191)
[ERROR] : TiHttpClient: at org.apache.http.impl.conn.DefaultResponseParser.parseHead(DefaultResponseParser.java:82)
[ERROR] : TiHttpClient: at org.apache.http.impl.io.AbstractMessageParser.parse(AbstractMessageParser.java:174)
[ERROR] : TiHttpClient: at org.apache.http.impl.AbstractHttpClientConnection.receiveResponseHeader(AbstractHttpClientConnection.java:180)
[ERROR] : TiHttpClient: at org.apache.http.impl.conn.DefaultClientConnection.receiveResponseHeader(DefaultClientConnection.java:235)
[ERROR] : TiHttpClient: at org.apache.http.impl.conn.AbstractClientConnAdapter.receiveResponseHeader(AbstractClientConnAdapter.java:259)
[ERROR] : TiHttpClient: at org.apache.http.protocol.HttpRequestExecutor.doReceiveResponse(HttpRequestExecutor.java:279)
[ERROR] : TiHttpClient: at org.apache.http.protocol.HttpRequestExecutor.execute(HttpRequestExecutor.java:121)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.DefaultRequestDirector.executeOriginal(DefaultRequestDirector.java:1296)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:677)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:653)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:637)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$ClientRunnable.run(TiHTTPClient.java:1328)
[ERROR] : TiHttpClient: at java.lang.Thread.run(Thread.java:841)
[ERROR] : TiHttpClient: Caused by: libcore.io.ErrnoException: recvfrom failed: ECONNRESET (Connection reset by peer)
[ERROR] : TiHttpClient: at libcore.io.Posix.recvfromBytes(Native Method)
[ERROR] : TiHttpClient: at libcore.io.Posix.recvfrom(Posix.java:141)
[ERROR] : TiHttpClient: at libcore.io.BlockGuardOs.recvfrom(BlockGuardOs.java:164)
[ERROR] : TiHttpClient: at libcore.io.IoBridge.recvfrom(IoBridge.java:506)
[ERROR] : TiHttpClient: ... 19 more
我已经解决了问题。
我已将 $.view_indicator.hide();
移至 if...else
区块。
所以 loginReq.onload
的正确代码是
loginReq.onload = function() {
var json = this.responseText;
var response = JSON.parse(json);
if (response.logged == true) {
var args = {
name: response.name,
email: response.email
};
var loggedUser = Alloy.createController("loggedin", args).getView();
loggedUser.open();
$.view_indicator.hide();
} else {
alert(response.message);
$.view_indicator.hide();
}
}