【问题标题】:How to post json object in Xcode 4?如何在 Xcode 4 中发布 json 对象?
【发布时间】:2011-11-05 01:20:53
【问题描述】:

我正在使用异步 nsurl 连接获取我的 json 对象 {"firstname":"A","Contact":{"phone":1234567899,"email":"abc@yahoo.com"}}

我想知道,如果我更改了我的电话号码的值,然后想将其上传回来,我该怎么做。我们是否必须只发送电话或整个对象的键值对 {"firstname":"A","Contact":{"phone":1234567899,"email":"abc@yahoo.com"}} 将更新的电话号码发送到服务器以在数据库中更新。

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; [request setURL:myUrl];
[request setHTTPMethod:@"POST"];
[request setValue:@"text/plain" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:stringInNSData]; 

上面的逻辑我几乎到处都找到了,但是不知道怎么实现,在哪里实现。

【问题讨论】:

    标签: ios ios4 xcode4


    【解决方案1】:

    这实际上取决于您的服务器以及数据库的存储方式。如果您使用的是类似 SQL 的数据库,则需要传递标识要更新的数据行以及更新的字段的主键。接收帖子的服务器代码可以更新您传入的属性,而无需理会其余数据行。

    关于在哪里实现它,你应该在你的数据发生变化时实现它。如果有触发数据更改的 UI 元素,您可以在那里实现它。只需在您的代码下方添加此代码

    NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:request delegate:self];
    

    这会将实现代码的类设置为连接的委托并启动连接。您可以使用这些方法来处理更新的成功或失败。

    Connection Data and Responses
    – connection:didSendBodyData:totalBytesWritten:totalBytesExpectedToWrite:  delegate method Available in iOS 3.0 through iOS 4.3
    – connection:didReceiveData:  delegate method Available in iOS 2.0 through iOS 4.3
    – connection:didReceiveResponse:  delegate method Available in iOS 2.0 through iOS 4.3
    – connection:willCacheResponse:  delegate method Available in iOS 2.0 through iOS 4.3
    – connection:willSendRequest:redirectResponse:  delegate method Available in iOS 2.0 through iOS 4.3
    Connection Completion
    – connection:didFailWithError:  delegate method Available in iOS 2.0 through iOS 4.3
    – connectionDidFinishLoading:  delegate method Available in iOS 2.0 through iOS 4.3
    

    【讨论】:

    • 是的,它是:-) 我正在制作一个类似于在线产品评级的应用程序。现在我再次陷入困境。我正在访问用户发布的所有 cmets。我已经放入了一个表格视图,但是有一个问题,比如文本的大小是否更大,并且表格视图单元格只显示 1 行。我正在寻找如何调整 tableview 的高度。
    • 我在 viewDidLoad -> myTableViewTwo = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, 320, 460) style:UITableViewStylePlain]; myTableViewTwo.dataSource = 自我; myTableViewTwo.delegate = 自我; [self.view addSubview: myTableViewTwo];
    • and and - (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath { jsonDetailData = [responseString JSONValue]; CGSize 大小 = [[jsonDetailData objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:14] constrainedToSize:CGSizeMake(300,500)];返回尺寸.高度 + 10; }
    • 但我在“constrainedToSize:CGSizeMake(300,500)”中得到“SIGABRT”,控制台中出现错误日志 == -[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x4c79540 2011-11-05 10 :25:29.396 InstructorReview [851:bc03] ***由于未捕获的异常'NSInvalidArgumentException'而终止应用程序,原因:'-[__NSCFDictionary objectAtIndex:]:无法识别的选择器发送到实例0x4c79540'
    • 您的 jsonDetailData 对象实际上是一个字典。 [__NSCFDictionary objectAtIndex:] 导致异常。在进一步显示数据之前,您应该使用日志语句并了解如何访问从 json 字符串返回的所有数据。此外,您应该提出新问题,而不是在评论更改中提出新问题。如果和回答第一个问题有关就好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-12-15
    • 2018-11-20
    • 2011-07-14
    • 2019-12-06
    • 1970-01-01
    相关资源
    最近更新 更多