【问题标题】:Connecting through webservice通过网络服务连接
【发布时间】:2012-02-05 07:37:40
【问题描述】:

我正在尝试插入网络服务器上的数据库。我正在使用 MYSQL、Appache 和 PHP

Xcode

*-(IBAction)saveRecord:(id)sender{
    UITextField *description_txtfield = (UITextField*)[self.tableView viewWithTag:11];
    description_string = description_txtfield.text;
    NSLog(description_string);

    NSURL *url = [NSURL URLWithString:@"http://192.168.1.140/~admin/qw/"];
    ASIFormDataRequest *request = [ASIFormDataRequest requestWithURL:url];
    [request setPostValue:description_string forKey:@"product_name"];
    [request setDelegate:self];
    [request startAsynchronous];

    [self.navigationController popToRootViewControllerAnimated:YES];
}
- (void)requestFinished:(ASIHTTPRequest *)request {    

    if (request.responseStatusCode == 400) {
        NSLog(@"Invalid code");        
    } else if (request.responseStatusCode == 403) {
        NSLog(@"Code already used");
    } else if (request.responseStatusCode == 200) {
        NSLog(@"OK");

    } else {
        NSLog(@"Unexpected error");
    }
     }*

- (void)requestFailed:(ASIHTTPRequest *)request
{    
    NSError *error = [request error];
    NSLog(error.localizedDescription);
}   
index.php has the following code inside to handle insert.

Function redeem() {

        // Check for required parameters
        if (isset($_POST["product_name"])) {

            // Put parameters into local variables
            $rw_app_id = $_POST["product_name"];
            // Add tracking of redemption
            $stmt = $this->db->prepare("INSERT INTO inventory (product_name) VALUES (?)");
            $stmt->bind_param($rw_app_id);
            $stmt->execute();
            $stmt->close();

        }
        return false;

    }

谁能告诉我我在这里做错了什么。我不断收到无效代码,即 Bad Request-400。帮助!这让我发疯了!

【问题讨论】:

    标签: php objective-c web-services ipad nsurl


    【解决方案1】:

    如果我正确阅读了您的 php 代码,在我看来,您的 redeemfunction 总是会点击函数底部的 sendResponse(400, 'Invalid request'); 行。

    在任何情况下,您的 redeem 函数返回 true 并且没有返回 400 代码?

    【讨论】:

    • 我把它拿出来了,但它仍然没有通过。我想。这给了我一些问题,但我告诉了它并且我从这个方法中得到了一个意外的错误 - (void)requestFinished:(ASIHTTPRequest *)request。我在我的网址中使用 IP 地址这一事实会导致问题吗?
    • 我怎样才能确保 index.php 正在接收一个值?我可以查询数据库以列出所有产品并将其显示在我的页面上。那是工作发现。当我点击我的应用程序上的保存按钮时,似乎 index.php 页面没有接收到值。有什么方法可以检查出来吗?
    • 不,使用数字 IP 地址不是问题。也许您需要从 php 端发回一个 200 响应,以便您的客户端的 requestFinished 方法将报告一切都成功了?
    • 未收到 200 响应。我认为它没有价值。
    最近更新 更多