ここで途方に暮れています。アプリの 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];
}
その後、次のように実行されます。
- (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;
}
明らかに何かが欠けていますが、どこを見ればよいかさえわかりません。私が必要とするのは、正しい方向へのポイントだけです。どんな助けも素晴らしいでしょう。
アップデート
以下で発射リクエストを取得できました。
さて、私は以下を使用して発火する要求を得ることができました:
- (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];
}
しかし、なんらかの理由で、post メソッドは多数の nil 値しか受け取っていません。これはサーバー側から取得しています。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];
}