2017-03-14 86 views
0

我必須發佈一些數據到api.I需要發送40個參數到server.In此40個參數我應該發送4個圖像作爲NSData.For我想使用AFNetWorking.In AFNetworking我應該使用哪一個?發表請求AFNetworking與40參數

+0

您需要提供更多信息,以便我們可以幫助您 –

回答

0

創建的NSMutableDictionary並添加所有的40個參數是這樣,然後做一個POST請求

- (void)submitLoginRequest:(NSString *)email password:(NSString *)password { 

    AFHTTPSessionManager *manager = [[AFHTTPSessionManager alloc] initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
    manager.requestSerializer = [AFJSONRequestSerializer serializer]; 



    NSMutableDictionary *dict = [[NSMutableDictionary alloc] init]; 
    [dict setValue:email forKey:@"Email"]; 
    [dict setValue:password forKey:@"Password"]; 



    [manager POST:@"http://www.google.com/api/" parameters:dict success:^(NSURLSessionTask *task, id responseObject) { 

     if (responseObject == [NSNull null]) { 

     }else { 

      NSLog(@"response type : %@", NSStringFromClass([responseObject class])); 
      NSLog(@"response type : %@", responseObject); 

     } 

    } failure:^(NSURLSessionTask *task, NSError *error) { 

     NSLog(@"AFHTTPSession Failure : %@", [error localizedDescription]); 
    }]; 

} 
0

我建議你寫的代碼像這些

NSDictionary *[email protected]{@"customer_firstname":credentials[@"firstname"], 
              @"customer_lastname":credentials[@"lastname"], 
              @"email":credentials[@"email"], 
              @"passwd":credentials[@"password"], 
              @"mobile_number":credentials[@"mobile"], 
              @"device_type" : @"2",          
              @「language_id」:[NSNumber numberWithInteger:[RTGlobalValues sharedGlobalValues].selectedLanguageId] 
              }; 
     NSDictionary *methodParamsDictionary [email protected]{ 
               @「action」:@「register", 
               @「controller」:kControllerLogin 
               }; 
AFHTTPSessionManager *session = [AFHTTPSessionManager manager]; 
    //AFHTTPSessionManager *session = [[AFHTTPSessionManager alloc]initWithSessionConfiguration:[NSURLSessionConfiguration defaultSessionConfiguration]]; 
    session.requestSerializer = [AFJSONRequestSerializer serializer]; 
    [session.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
NSString *URL_String = [NSString stringWithFormat:@"http://www.requesturl/%@」,methodName]; 

     NSLog(@"URL string %@",URL_String); 

     [session POST:URL_String parameters:inputParameters constructingBodyWithBlock:^(id<AFMultipartFormData> _Nonnull formData) { 
      NSLog(@"%@",imageName); 
      [formData appendPartWithFileData:uploadImageData name:@"image" fileName:imageName mimeType:@"image/png"]; 

     } progress:^(NSProgress * _Nonnull uploadProgress) { 

     } success:^(NSURLSessionDataTask * _Nonnull task, id _Nullable responseObject) { 

      NSDictionary *dataObj = (NSDictionary *)responseObject; 
      NSLog(@"%@", dataObj); 
      completeBlock(dataObj); 

     } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) { 

      NSLog(@"%s :%@",__PRETTY_FUNCTION__,error.description); 

      failBlock() 
     }]; 

我希望這會幫助你。 ..