gönder 'ben uygulamam için web hizmetleri ile yeni bir şey denemek düşündüm.NSURLConnection JSON Burada bir kayıp am Sunucusu
hiçbir sorun aşağı veri çekebilir ama sunucuya göndermek için çalışıyorum ve sadece bu bile ateş almaya görünebilir. o geçiyor Bundan sonra
- (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];
}
:
Ne gerçekleşmesi niyetindeyim eylem ateş düğmesine basın göndermek üzerindedir
- (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;
}
Açıkçası bir şey eksik, ama ben don' nereye bakılacağını bile bilmiyorum. Tek ihtiyacım doğru yönde bir noktadır, herhangi bir yardım çok iyi olurdu.
GÜNCELLEME Ben aşağıdaki ile ateş isteğini elde edebildi.
aşağıdaki kullanarak ateş isteği elde edebildi Okay:
- (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];
}
Ama sonrası yöntemi yalnızca nil değerlerin bir demet aldı nedense
, ben Sunucu tarafından bu alıyorum.Processing by ProjectsController#create as JSON Parameters: {"{\n \"desc_long\" : \"a\",\n \"name\" : \"a\",\n \"desc_small\" : \"a\"\n}"=>nil}
GÜNCELLEME Buradan biraz okuma ile 2
: Ben aşağıdaki satırı cevapsız olmadığını görmek başardı http://elusiveapps.com/blog/2011/04/ios-json-post-to-ruby-on-rails/
. [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
Son olarak, didPressSubmit kodu aşağıdaki gibidir;
- (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];
}
Burada neler oluyor: [[NSURLConnection alloc] initWithRequest: dataSubmit delege: self]; '? Yeni başlatılmış bağlantıyı bir değişkene atamadınız. – FluffulousChimp
NSURLConnection'ın sendAsynchronousRequest: queue: completionHandler: yöntemini kullanmayı deneyin. – 8vius