Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在目标c中使用POST_Ios_Objective C_Json_Post_Webserver - Fatal编程技术网

Ios 在目标c中使用POST

Ios 在目标c中使用POST,ios,objective-c,json,post,webserver,Ios,Objective C,Json,Post,Webserver,我在实际使用POST保存/更改数据库中的信息时遇到问题。谁能帮帮我吗 NSDictionary *newDatasetInfo = [NSDictionary dictionaryWithObjectsAndKeys:un, @"username", n, @"name", pn, @"phonenumber",em, @"email", nil]; NSError *error; NSData* jsonData = [NSJSONSerialization dataWithJSONObj

我在实际使用POST保存/更改数据库中的信息时遇到问题。谁能帮帮我吗

NSDictionary *newDatasetInfo = [NSDictionary dictionaryWithObjectsAndKeys:un, @"username", n, @"name", pn, @"phonenumber",em, @"email", nil];

NSError *error;

NSData* jsonData = [NSJSONSerialization dataWithJSONObject:newDatasetInfo options:kNilOptions error:&error];


NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
[request setURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setHTTPBody:jsonData];

// print json:
NSLog(@"JSON summary: %@", [[NSString alloc] initWithData:jsonData
                                                 encoding:NSUTF8StringEncoding]);
NSURLConnection *connection = [[NSURLConnection alloc] initWithRequest:request delegate:self];
[connection start];
上面的代码来自应用程序我的服务器端代码如下所示: 另外,请注意,此代码不在php文件中

if(!isset($_GET["username"]))
{
    // adding new record
    $q = sprintf("insert into users (username, name, phoneNumber, email) values ('%s','%s', '%s', '%s')",
        mysql_real_escape_string($_GET["username"]),
        mysql_real_escape_string($_GET["name"]),
        mysql_real_escape_string($_GET["phone"]),
        mysql_real_escape_string($_GET["email"]));
}
else
{
    // update the  record
    $q = sprintf("update users set name = '%s', phoneNumber = '%s', email = '%s' where username = '%s'",
        mysql_real_escape_string($_GET["name"]),
        mysql_real_escape_string($_GET["phoneNumber"]),
        mysql_real_escape_string($_GET["email"]),
        mysql_real_escape_string($_GET["username"]));
}

您在PHP代码中解码过JSON对象吗

尝试如下设置

[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
我不确定你是否需要跟随

[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];

希望这有帮助。

也许不是最理想的方式,但我可以提出一个可行的方式

在iOS方面

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:user 
                                                       options:kNilOptions
                                                         error:&error];
    if (!jsonData) {
        NSLog(@"Error:%@",error);
    }
    else {
        jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
    }

    NSString * params = [[NSString alloc] initWithFormat:@"user=%@",jsonString];

    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:kLinkUpdateFacebook]];
    [request setHTTPMethod:@"POST"];
    [request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; // and send the request
在php方面

<?
    $json_user = json_decode($_POST["user"],true);

    echo $json_user["name"];
?>


您能解释一下发生了什么吗?谢谢,但我的php代码似乎在数据库中添加了一个空白条目,甚至在我从浏览器调用url时也会这样做。我从应用程序传递的信息是否错误?或者我的php文件可能有问题吗?我相信您需要在php中正确访问它。因为iPhone代码似乎是正确的。如果您需要进一步的问题,请告诉我,这种方法是将其作为post参数发送并在php中收集,如下所示