Ric*_*lsh 10 ruby json nsurlconnection ios
我在这里不知所措,我想我会为我的应用程序尝试新的Web服务.
我可以提取数据没有问题,但我试图发布到服务器,似乎可以得到这甚至火.
我打算发生什么是在提交按钮按下动作被解雇:
- (IBAction)didPressSubmit:(id)sender {
//JSON SERIALIZATION OF DATA
NSMutableDictionary *projectDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
[projectDictionary setObject:[projectName text] forKey:@"name"];
[projectDictionary setObject:[projectDescShort text] forKey:@"desc_short"];
[projectDictionary setObject:[projectDescLong text] forKey:@"desc_long"];
NSError *jsonSerializationError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError];
if(!jsonSerializationError) {
NSString *serJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Serialized JSON: %@", serJSON);
} else {
NSLog(@"JSON Encoding Failed: %@", [jsonSerializationError localizedDescription]);
}
// JSON POST TO SERVER
NSURL *projectsUrl = [NSURL URLWithString:@"http://70.75.66.136:3000/projects.json"];
NSMutableURLRequest *dataSubmit = [NSMutableURLRequest requestWithURL:projectsUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[dataSubmit setHTTPMethod:@"POST"]; // 1
[dataSubmit setValue:@"application/json" forHTTPHeaderField:@"Accept"]; // 2
[dataSubmit setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; // 3
[dataSubmit setHTTPBody: jsonData];
[[NSURLConnection alloc] initWithRequest:dataSubmit delegate:self];
}
Run Code Online (Sandbox Code Playgroud)
之后它贯穿:
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
NSLog(@"DidReceiveResponse");
}
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData
{
NSLog(@"DidReceiveData");
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
UIAlertView *errorView = [[UIAlertView alloc] initWithTitle:@"Error" message:@"BLAH CHECK YOUR NETWORK" delegate:nil cancelButtonTitle:@"Dismiss" otherButtonTitles:nil];
[errorView show];
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
}
Run Code Online (Sandbox Code Playgroud)
我显然缺少一些东西,但我甚至不知道在哪里看.我所需要的只是朝着正确方向发展的一点,任何帮助都会很棒.
UPDATE
我能够通过以下方式获得解雇请求.
好的,我能够使用以下内容获取触发请求:
- (IBAction)didPressSubmit:(id)sender {
NSMutableDictionary *projectDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
[projectDictionary setObject:[projectName text] forKey:@"name"];
[projectDictionary setObject:[projectDescShort text] forKey:@"desc_small"];
[projectDictionary setObject:[projectDescLong text] forKey:@"desc_long"];
NSError *jsonSerializationError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError];
if(!jsonSerializationError) {
NSString *serJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Serialized JSON: %@", serJSON);
} else {
NSLog(@"JSON Encoding Failed: %@", [jsonSerializationError localizedDescription]);
}
NSURL *projectsUrl = [NSURL URLWithString:@"http://70.75.66.136:3000/projects.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:projectsUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"]; // 1
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; // 2
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; // 3
[request setHTTPBody: jsonData]; // 4
(void) [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
Run Code Online (Sandbox Code Playgroud)
但由于某种原因,post方法只收到一堆零值,我从服务器端得到这个. Processing by ProjectsController#create as JSON
Parameters: {"{\n \"desc_long\" : \"a\",\n \"name\" : \"a\",\n \"desc_small\" : \"a\"\n}"=>nil}
更新2
稍微阅读一下:http://elusiveapps.com/blog/2011/04/ios-json-post-to-ruby-on-rails/
我能够看到是否错过了以下一行.
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
所以最后的didPressSubmit代码如下;
- (IBAction)didPressSubmit:(id)sender {
NSMutableDictionary *projectDictionary = [NSMutableDictionary dictionaryWithCapacity:1];
[projectDictionary setObject:[projectName text] forKey:@"name"];
[projectDictionary setObject:[projectDescShort text] forKey:@"desc_small"];
[projectDictionary setObject:[projectDescLong text] forKey:@"desc_long"];
NSError *jsonSerializationError = nil;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError];
if(!jsonSerializationError) {
NSString *serJSON = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(@"Serialized JSON: %@", serJSON);
} else {
NSLog(@"JSON Encoding Failed: %@", [jsonSerializationError localizedDescription]);
}
NSURL *projectsUrl = [NSURL URLWithString:@"http://70.75.66.136:3000/projects.json"];
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:projectsUrl cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:30.0];
[request setHTTPMethod:@"POST"]; // 1
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; // 2
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
[request setValue:[NSString stringWithFormat:@"%d", [jsonData length]] forHTTPHeaderField:@"Content-Length"]; // 3
[request setHTTPBody: jsonData]; // 4
(void) [[NSURLConnection alloc] initWithRequest:request delegate:self];
}
Run Code Online (Sandbox Code Playgroud)
我遇到了同样的问题,但我通过将选项设置为零来解决它。
代替
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError];
Run Code Online (Sandbox Code Playgroud)
经过
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:nil error:&jsonSerializationError];
Run Code Online (Sandbox Code Playgroud)
nil如果要将 json 发送到服务器,请使用选项 to ,如果要显示 json,请使用NSJSONWritingPrettyPrinted。
希望对你有帮助。
| 归档时间: |
|
| 查看次数: |
6541 次 |
| 最近记录: |