我是Objective-c的新手,我开始在最近的请求/响应中投入大量精力.我有一个可以调用url(通过http GET)并解析返回的json的工作示例.
这个工作的例子如下
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
[responseData setLength:0];
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog([NSString stringWithFormat:@"Connection failed: %@", [error description]]);
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
[connection release];
//do something with the json that comes back ... (the fun part)
}
- (void)viewDidLoad
{
[self searchForStuff:@"iPhone"];
}
-(void)searchForStuff:(NSString *)text
{
responseData = [[NSMutableData data] retain];
NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://www.whatever.com/json"]];
[[NSURLConnection alloc] initWithRequest:request delegate:self];
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是 - …
我有一个需要登录的php网页(用户ID和密码).我让用户在应用程序中输入信息就好......但我需要一个关于如何对网站发出POST请求的示例.支持网站上的苹果示例相当复杂,显示图片上传..我应该更简单..我只想发布2行文字..任何人都有任何好的例子?
亚历克斯
我必须动态生成一个json字符串,并需要发送到服务器.是否有人知道如何使用它NSJSONSerialization.下面是我的字符串
{
"surveyid": "Survey1",
"responsetime": "dd/mm/yyyy hh:mm:ss",
"location": null,
"surveyresponses": [
{
"questionid": "111",
"responses": [
{
"response": "Good",
"optionid": 1,
"language": "en"
}
]
},
{
"questionid": "112",
"responses": [
{
"response": "bad",
"optionid": 2,
"language": "en"
}
]
}
]
}
Run Code Online (Sandbox Code Playgroud)
如何创建一个string.json,如果有人知道请帮助.提前致谢.
objective-c ×3
cocoa-touch ×2
ios ×2
iphone ×2
json ×2
http ×1
ipad ×1
nsurlrequest ×1
post ×1