2014-04-25 59 views
0

通常情況下,下載一個JSON,我用AFNetworking創建一個單獨與此代碼如何將一些參數添加到JSON請求?

- (void)getJSON { 
    NSURLRequest * request = 
    [NSURLRequest requestWithURL: 
    [NSURL URLWithString:@"http://URL"]]; 

    AFJSONRequestOperation * operation = 
    [AFJSONRequestOperation JSONRequestOperationWithRequest:request 
    success:^(NSURLRequest *request, NSHTTPURLResponse *response, id JSON) { 

     NSArray * js = JSON; 
     [_delegate dati:js]; 

     NSArray * paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
     NSString * filePath = [[paths lastObject] stringByAppendingPathComponent:@"downloaded.json"]; 

     NSData * data = [NSJSONSerialization dataWithJSONObject:JSON 
                 options:NSJSONWritingPrettyPrinted 
                  error:NULL]; 
     [data writeToFile:filePath 
       atomically:YES]; 

    }]; 

    [operation start]; 
} 

,並查看調用此代碼控制器

[[DataManager sharedClass] getJSON]; 

和它的作品,但現在我需要發送(發佈)一些參數(作爲授權代碼,GPS座標,用戶郵件或類似的東西)到服務器的請求中以接收特定的JSON。服務器已經配置好,並且工作正常,但是我找不到修改我的代碼的指南。有人知道如何繼續?

回答

0

這是發佈使用JSON數據到服務器,例如JSON是下面的例子:

NSDictionary *json = @{@"authorization_code": yourAuthorizationCode,@"gps": @{@"lat": latitude,@"lng": longitude},@"email":email}; 

JSON在服務器上發佈

發帖也取決於你的服務器的要求,因此,如果需要一個JSON發佈,這是這樣的:

//Create an AFHTTPRequestOperationManager 
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

//Depending if you need a HTTP header, you create one 
[manager.requestSerializer setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
NSString *yourAuthorizationCode; 
NSString *latitude; 
NSString *longitude; 
NSString *email; 


//My data to be posted via JSON 
NSDictionary *json = @{@"authorization_code": yourAuthorizationCode,@"gps": @{@"lat": latitude,@"lng": longitude},@"email":email}; 


//Sending Post Method, with parameter JSON 
[manager POST:@"http://myphp.com/api/v1/profile" parameters:json success:^(AFHTTPRequestOperation *operation, id responseObject) 
{ 
    NSLog(@"JSON: %@", responseObject); 
} 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{ 
    NSLog(@"Error at creating post: %@", error); 
}]; 

參數通過URL發佈

在這種方法參數的URL都過去了,你將它們發佈到服務器的URL形式

//Create an AFHTTPRequestOperationManager 
AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 


NSString *yourAuthorizationCode; 
NSString *latitude; 
NSString *longitude; 
NSString *email; 

//Sending Post Method, with parameter JSON (You can change your method of sending data to server just by replacing POST with GET) 
[manager POST:[NSString stringWithFormat:@"http://myphp.com/api/v1/profile?authorization_code=%@&gps_lat=%@&gps_lng=%@&email=%@",yourAuthorizationCode,latitude,longitude,email] parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) 
{ 
    NSLog(@"JSON: %@", responseObject); 
} 
     failure:^(AFHTTPRequestOperation *operation, NSError *error) 
{ 
    NSLog(@"Error at creating post: %@", error); 
}]; 

希望它能幫助,給我的反饋!

相關問題