Titanium:主机连接时出现TiHttpClient错误

时间:2014-04-25 14:34:58

标签: android rest titanium androidhttpclient

描述:我想通过POST请求连接到服务器。我的方法,发布请求:

function loginClick(e) {
    var url = "http://...";
    var xhr = Ti.Network.createHTTPClient({
        onload: function (e) { // this function is called when data is returned from the server and available for use
            // this.responseText holds the raw text return of the message (used for text/JSON)
            // this.responseXML holds any returned XML (including SOAP)
            // this.responseData holds any returned binary data
            Ti.API.debug(this.responseText);
            alert(xhr.responseText);
        },
        onerror: function (e) { // this function is called when an error occurs, including a timeout
            Ti.API.debug(e.error);
            alert(e.toString);
        },
        timeout: 5000 /* in milliseconds */
    });
    xhr.autoEncodeUrl = false;
    var params = {
        email: $.email.value,
        password: $.password.value
    };
    xhr.open('POST', url);
    xhr.send(params); // request is actually sent with this statement 
    Ti.API.info(xhr.responseText);
};

但我不能这样做,因为收到奇怪的消息。我把它放在日志中。此外,我做了另一个请求,他们成功地工作。 日志:

[ERROR] : TiHttpClient: (TiHttpClient-1) [3529,3529] HTTP Error (org.apache.http.client.HttpResponseException): Not Found
[ERROR] : TiHttpClient: org.apache.http.client.HttpResponseException: Not Found
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:258)
[ERROR] : TiHttpClient: at ti.modules.titanium.network.TiHTTPClient$LocalResponseHandler.handleResponse(TiHTTPClient.java:217)
[ERROR] : TiHttpClient: at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
[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:1287)
[ERROR] : TiHttpClient: at java.lang.Thread.run(Thread.java:841)

2 个答案:

答案 0 :(得分:1)

我用发送请求的JSON.stringify(params)解决了它:

function loginClick(e)
{
var url = 'http://...';
var xhr = Ti.Network.createHTTPClient({
    onload: function(e) {
        // this function is called when data is returned from the server and available for use
        // this.responseText holds the raw text return of the message (used for text/JSON)
        // this.responseXML holds any returned XML (including SOAP)
        // this.responseData holds any returned binary data
        Ti.API.debug(this.responseText);
        alert(xhr.responseText);
    },
    onerror: function(e) {
        // this function is called when an error occurs, including a timeout
        Ti.API.debug(e.error);
        alert(this.status);
        alert("error" + e.toString);
    },
    timeout:5000  /* in milliseconds */
});
xhr.autoEncodeUrl = false;
var params = {  
    'email': $.email.value,  
    'password' :$.password.value
};

xhr.open('POST', url);
xhr.setRequestHeader("Content-Type", "application/json; charset=utf-8");
xhr.send(JSON.stringify(params)); // request is actually sent with this statement

};

答案 1 :(得分:0)

“未找到”例外是有道理的。我尝试使用CURL进行POST以证明这不是由Titanium引起的。您的网址错误。

> curl --verbose --data "email=test@mail.com&password=dnipro" http://www.assignmentexpert.com/api/v1
* Adding handle: conn: 0x7ffdc4004400
* Adding handle: send: 0
* Adding handle: recv: 0
* Curl_addHandleToPipeline: length: 1
* - Conn 0 (0x7ffdc4004400) send_pipe: 1, recv_pipe: 0
* About to connect() to www.assignmentexpert.com port 80 (#0)
*   Trying 198.72.112.182...
* Connected to www.assignmentexpert.com (198.72.112.182) port 80 (#0)
> POST /api/v1 HTTP/1.1
> User-Agent: curl/7.30.0
> Host: www.assignmentexpert.com
> Accept: */*
> Content-Length: 35
> Content-Type: application/x-www-form-urlencoded
> 
* upload completely sent off: 35 out of 35 bytes
< HTTP/1.1 404 Not Found
* Server nginx/1.4.7 is not blacklisted
< Server: nginx/1.4.7
< Date: Fri, 25 Apr 2014 16:45:58 GMT
< Content-Type: text/html; charset=utf-8
< Transfer-Encoding: chunked
< Connection: keep-alive
< X-Powered-By: PHP/5.4.26
< Set-Cookie: updater=a%3A5%3A%7Bs%3A10%3A%22session_id%22%3Bs%3A32%3A%22d0a5527549f76365252a61361cd78e59%22%3Bs%3A10%3A%22ip_address%22%3Bs%3A12%3A%2298.218.93.45%22%3Bs%3A10%3A%22user_agent%22%3Bs%3A11%3A%22curl%2F7.30.0%22%3Bs%3A13%3A%22last_activity%22%3Bi%3A1398444358%3Bs%3A9%3A%22user_data%22%3Bs%3A0%3A%22%22%3B%7D7b00051302d9b00cd28b355be89a1e98; expires=Sun, 24-Apr-2016 16:45:58 GMT; path=/
< Set-Cookie: logged=deleted; expires=Thu, 01-Jan-1970 00:00:01 GMT
< Location: /api/v1/
< 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
(truncated -- HTML returned is identical to www.assignmentexpert.com)