저는 여기에 손해를보고 있습니다. 제 앱의 웹 서비스에 새로운 것을 시도 할 것이라고 생각했습니다.NSURLConnection JSON 서버에 제출
아무런 문제없이 데이터를 가져올 수는 있지만 서버에 게시하려고 시도하고 있으며이를 실행하는 것처럼 보일 수도 있습니다. 그것을 통해 실행되는 후
- (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;
}
내가 분명히 뭔가가 있어요,하지만 난 돈 ' 봐야 할지도 모릅니다. 내가 필요한 모든 것이 올바른 방향으로 향한 것입니다. 어떤 도움도 좋을 것입니다.
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];
}
그러나 포스트 방법은 전무 값의 무리를받은 어떤 이유로
, 나는 서버 측에서이 무엇입니까.Processing by ProjectsController#create as JSON Parameters: {"{\n \"desc_long\" : \"a\",\n \"name\" : \"a\",\n \"desc_small\" : \"a\"\n}"=>nil}
UPDATE 여기에서 약간의 읽기와 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];
}
여기에 무슨 일이 일어나고 :'[[NSURLConnection alloc] initWithRequest : dataSubmit delegate : self];'? 새로 초기화 된 연결을 변수에 할당하지 않습니다. – FluffulousChimp
NSURLConnection의 sendAsynchronousRequest : queue : completionHandler : 메소드를 사용해보십시오. – 8vius