/ / Fehler beim Senden des JSONArray-Parameters in der URL (iOS) - ios, json, nsarray, afnetworking-2, jsonkit

Fehler beim Senden des JSONArray-Parameters in URL (iOS) - ios, json, nsarray, afnetworking-2, jsonkit

Ich muss ein Array in JSON konvertieren und es sendenals Parameter in einer URL zusammen mit anderen Parametern. Ich verwende jsonKit für die json-Konvertierung und AFNetworking für die Netzwerkkommunikation. Es gibt jedoch immer eine Ausnahme im json auf dem Server.

Hier ist der Code für die Sohnkonvertierung:

NSString *jsonData = [self.finalArray JSONString];
NSString *data = [NSString stringWithFormat:@"%s",[jsonData UTF8String] ];

Diese Daten werden schließlich als Parameter in der URL gesendet. Code für die Netzwerkkommunikation:

postData=[postData stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
NSURL *requestUrl = [[NSURL alloc] initWithString:[NSString stringWithFormat:@"%@%@",url,postData]];
NSURLRequest *request =[[NSURLRequest alloc] initWithURL:requestUrl];

AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:request];

operation.responseSerializer = [AFJSONResponseSerializer serializer];
operation.responseSerializer.acceptableContentTypes = [NSSet setWithObject:@"text/plain"];

[operation setCompletionBlockWithSuccess:success failure:failure];
[operation start];

Wird auch für Escapezeichen verwendet:

[postData stringByAddingPercentEncodingWithAllowedCharacters: [NSCharacterSet URLQueryAllowedCharacterSet]];

Fehler erhalten:

Fehlerdomäne = NSURLErrorDomain Code = -1002"nicht unterstützte URL" UserInfo = {NSUnderlyingError = 0x7d51e9a0 {Fehlerdomäne = kCFErrorDomainCFNetwork Code = -1002 "nicht unterstützte URL" UserInfo = {NSLocalizedDescription = nicht unterstützte URL}}, NSLocalizedDescription = nicht unterstützte URL}

Antworten:

-1 für die Antwort № 1

Ich denke du solltest es so benutzen

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
NSString *strURL = [NSString stringWithFormat:@"%@/add-product.php", apiPrefix];

[manager POST:strURL parameters:nil constructingBodyWithBlock:^(id<AFMultipartFormData> formData)
{
NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:dictData options:0 error:&error]; // dictData is your dictionary
NSAssert(jsonData, @"Failure building JSON: %@", error);

NSDictionary *jsonHeaders = @{@"Content-Disposition" : @"form-data; name="data""}; // data is your parameter name in which you have to pass the json

[formData appendPartWithHeaders:jsonHeaders body:jsonData];
}

success:^(AFHTTPRequestOperation *operation, id responseObject)
{
NSLog(@"JSON: %@", responseObject);
}
failure:^(AFHTTPRequestOperation *operation, NSError *error)
{
NSLog(@"Error: %@", error);
}];

Hoffe das wird helfen ....