gpt4 book ai didi

ios - NSURLConnection JSON 提交到服务器

转载 作者:技术小花猫 更新时间:2023-10-29 11:08:15 27 4
gpt4 key购买 nike

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

我可以毫无问题地提取数据,但我正在尝试将数据发布到服务器并且似乎可以让它甚至触发。

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

- (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 值,我是从服务器端获取的。 ProjectsController 处理#create as JSON
参数:{"{\n\"desc_long\":\"a\",\n\"名称\":\"a\",\n\"desc_small\":\"a\"\n}"=>无

更新 2

从这里读一点:http://elusiveapps.com/blog/2011/04/ios-json-post-to-ruby-on-rails/

我能够查看是否错过了以下行。[request setValue:@"application/json"for HTTPHeaderField:@"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];

}

最佳答案

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

替换

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

通过

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

如果要向服务器发送 json,请使用 nil 选项,如果要显示 json,请使用 NSJSONWritingPrettyPrinted

希望对您有所帮助。

关于ios - NSURLConnection JSON 提交到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12762027/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com