2015-02-12 65 views
1

如何使用RESTFUL API POST JSON值?iOS中的JSON值POST?

在這裏,我添加了一個樣本API:

http://our.api.com/Search?term=pumas&filters={"productType":["Clothing","Bags"],"color":["Black","Red"]} 

我要發佈類似上述網址,並得到響應值。有沒有什麼可能的方法來獲取價值?它是否有示例API?

回答

2

嘗試下面的代碼...

對於那個使用AFNetworking庫。鏈接:https://github.com/AFNetworking/AFNetworking

#import "AFNetworking.h" 

string = [NSString stringWithFormat:@"%@offer/nearBy", WSURL]; 
NSDictionary *parameters; 

//Pass Perameters 
parameters = @{@"dCurrentLat" : ApplicationDelegate.strLattitudeG, 
       @"dCurrentLong":ApplicationDelegate.strLongitudeG, 
       @"start":[NSString stringWithFormat:@"%d",start], 
       @"limit":[NSString stringWithFormat:@"%d",20], 
       @"iCountryID" : strCountyID}; 

AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager]; 

//Set header here 
[manager.requestSerializer setValue:XAPIKEY forHTTPHeaderField:@"X-API-KEY"]; 

[manager POST:string parameters:parameters success:^(AFHTTPRequestOperation *operation, id responseObject) 
{ 
    //Here you can Get Response from Server 
    NSDictionary *aDictResponse = (NSDictionary *)responseObject; 
    BOOL success = [[aDictResponse objectForKey:@"SUCCESS"] boolValue]; 

    if(success) 
    { 
    } 
} 
failure:^(AFHTTPRequestOperation *operation, NSError *error) { 
    NSLog(@"Error: %@", error); 
    //Here error log 
}]; 

另一種方式......

//使用下面的代碼來請求POST方法。這是簡單

坐落在.h文件中

// JSON 
NSMutableData *responseData; 
NSURLConnection * Connection_Audio_Upload; 

坐落在.m文件

NSMutableURLRequest *request = [NSMutableURLRequest 
           requestWithURL:[NSURL URLWithString:@"http://180.179.227.99/oMomento/webservice.asmx/UploadFileJSon"] cachePolicy:NSURLRequestReloadIgnoringLocalAndRemoteCacheData 
           timeoutInterval:10]; 

NSString * params=[NSString stringWithFormat:@"f=%@&step=0&filename=%@.3gp&path=%@",trimmed,audio_File_Name,@"BabyJournal/Audio/"]; 
[request setHTTPMethod:@"POST"]; 

[request setHTTPBody:[params dataUsingEncoding:NSUTF8StringEncoding]]; 
Connection_Audio_Upload=[[NSURLConnection alloc] initWithRequest:request delegate:self]; 

// -----使用--- ----- JSON方法------------ //

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
    if (connection == Connection_Audio_Upload) 
     [responseData setLength:0]; 
} 

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    if (connection == Connection_Audio_Upload) 
     [responseData appendData:data]; 
} 

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    if (connection == Connection_Audio_Upload) 
     NSLog(@"%@", error); 
} 

- (void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 
    if (connection == Connection_Audio_Upload) 
    { 
     // NSLog(@"Finished Loading Pic Upload............"); 

     //You can get the response HERE// 
     NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]; 
     // NSLog(@"respoce string = %@",responseString); 
     NSLog(@"Succesfully Upload Pic Audio_Upload"); 

    } 
} 
+0

除AFNetworking外,還有其他方法嗎? – Imran 2015-02-12 06:26:17

+0

是的..我已編輯我的答案.. – Mehul 2015-02-12 06:29:39

+0

告訴其他方式? – Imran 2015-02-12 06:35:27