10

我在这里不知所措,我想我会为我的应用程序尝试一些新的网络服务。

我可以将数据拉下来没问题,但我正在尝试发布到服务器并且似乎可以让它甚至触发。

我打算发生的是在提交按钮上按下动作被触发:

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

}
4

1 回答 1

1

我遇到了同样的问题,但我通过将选项设置为 nil 来解决它。

代替

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

经过

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

如果您正在向服务器发送 json,请使用选项 to nil,如果您正在显示 json use NSJSONWritingPrettyPrinted

希望这会帮助你。

于 2012-10-22T05:23:32.163 回答