2012-10-06 31 views
10

私はここで迷っています。私は自分のアプリのための新しいWebサービスを試したいと思っていました。NSURLConnection JSONサーバーに送信

私はデータを問題なく引き出すことができますが、私はサーバーに投稿しようとしています。 「私は明らかに何かが足りないのですが、私はドン

- (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 { 

    //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]; 
} 

それが通ることの後:私が起こることを意図しています何

は、送信ボタンを押してアクションを発射することですどこを見ても知りません。私が必要とするのは、正しい方向のポイントです。どんな助けも素晴らしいでしょう。

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]; 

} 

をしかし、postメソッドのみがゼロ値の束を受け取った何らかの理由で、私は、サーバー側からこれを取得しています。ここから少しの読み取りで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]; 

} 
+0

ここで何が起こっていますか?[[NSURLConnection alloc] initWithRequest:dataSubmit delegate:self]; '?新しく初期化された接続を変数に代入していません。 – FluffulousChimp

+0

NSURLConnectionのsendAsynchronousRequest:queue:completionHandler:メソッドを使用してみてください。 – 8vius

答えて

1

同じ問題が発生しましたが、オプションをnilに設定して解決しました。あなたはJSONの使用NSJSONWritingPrettyPrintedを表示している場合は、サーバーにJSONを送信する場合

nil

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:nil error:&jsonSerializationError]; 

使用オプションにより

NSData *jsonData = [NSJSONSerialization dataWithJSONObject:projectDictionary options:NSJSONWritingPrettyPrinted error:&jsonSerializationError]; 

を交換してください。

希望すると、これが役立ちます。

関連する問題