jestem pobierania danych JSON z adresu URL, który jest sformatowany tak:dane POST JSON do istniejącego obiektu
{"zoneresponse":
{"tasks":
[{"datafield1":"datafor1",
"datafield2":"datafor2",
"datafield3":"datafor3",...
}]
}}
mam żadnej kontroli nad strukturą, jak to jest z prywatnej API.
Jak wstawić dane w wybranym polu danych istniejącego obiektu?
Próbowałem to:
self.responseData = [NSMutableData data];
//testingURL is the api address to the specific object in tasks
NSURL *url = [NSURL URLWithString:testingURL];
NSMutableDictionary *params = [[NSMutableDictionary alloc] init];
[[[params objectForKey:@"zoneresponse"] objectForKey:@"tasks"] setValue:@"HelloWorld" forKey:@"datafield1"];
//HAVE TRIED setObject: @"" objectForKey: @"" as well
//*****PARAMS IS EMPTY WHEN PRINTED IN NSLog WHICH IS PART OF THE ISSUE - SETTING VALUE DOES NOT WORK
NSError * error = nil;
NSLog(@"Params is %@", params);
NSData *requestdata = [NSJSONSerialization dataWithJSONObject:params options:0 error:&error];
NSMutableURLRequest *request;
request = [NSMutableURLRequest requestWithURL:url];
[request setHTTPMethod:@"POST"];
[request setValue:[NSString stringWithFormat:@"%d", [requestdata length]] forHTTPHeaderField:@"Content-Length"];
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:requestdata];
NSURLConnection *conn = [[NSURLConnection alloc] initWithRequest:request delegate:self];
if(conn) {
NSLog(@"Connection Successful, connection is: %@", conn);
} else {
NSLog(@"Connection could not be made");
}
Połączenie jest wykonane ale słownika params jest pusty po wydrukowaniu (the setValue nie wyświetla) i nie wkracza żadnych danych w polu wybiorę.
Sprawdziłem te linki, ale nic nie wyjaśnia, czy zostanie wstawiony do odpowiedniego pola i oznacza, że utworzy nowy obiekt, a nie zaktualizuje istniejący.
How to update data on server db through json api?
How to send json data in the Http request using NSURLRequest
metody Delegowanie
//any time a piece of data is received we will append it to the responseData object
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
[self.responseData appendData:data];
NSError *jsonError;
id responseDict =
[NSJSONSerialization JSONObjectWithData:self.responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
NSLog(@"Did Receive data %@", responseDict);
}
//if there is some sort of error, you can print the error or put in some other handling here, possibly even try again but you will risk an infinite loop then unless you impose some sort of limit
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
{
// Clear the activeDownload property to allow later attempts
self.responseData = nil;
NSLog(@"Did NOT receive data ");
}
//connection has finished, the requestData object should contain the entirety of the response at this point
- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
NSError *jsonError;
id responseDict =
[NSJSONSerialization JSONObjectWithData:self.responseData
options:NSJSONReadingAllowFragments
error:&jsonError];
if(responseDict)
{
NSLog(@"%@", responseDict);
}
else
{
NSLog(@"%@", [jsonError description]);
}
//clear out our response buffer for future requests
self.responseData = nil;
}
Pierwsza metoda tu stwierdza, że dane zostały odebrane z "dotarła danych (null)", nie ma błędu w związku jednak ostatnia metoda wypisuje komunikat o błędzie "Tekst JSON nie zaczął się od tablicy lub obiektu i opcji, aby nie zezwalać na fragmenty.", co jest zrozumiałe, ponieważ nie ma danych ani obiektów, które są wysyłane.
Jak wstawić dane do wybranego pola istniejącego obiektu?
gdzie i jak można rozpocząć Conn ** **? Mam na myśli [conn start] –
@VivekMolkar Mam to wewnątrz metody (IBAction) i zakładam, że łączy się autonomicznie w oparciu o moje metody zwrotu '- (void) połączenie: (NSURLConnection *) połączenie didReceiveData: (NSData *) dane' , '- (void) connection: (NSURLConnection *) connection didFailWithError: (NSError *) error' oraz' - (void) connectionDidFinishLoading: (NSURLConnection *) connection' –
Nie, musisz wpisać linię kodu '[conn start ] ', aby zainicjować połączenie. ** conn ** sam się nie inicjuje. –