JSON在iOS中重视POST?

时间:2015-02-12 04:24:57

标签: ios json

如何使用RESTFUL API发布JSON值?

这里我添加了一个示例API:

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

我想发布上述网址等值并获得回复。有没有可能获得价值的方法?它是否有示例API?

1 个答案:

答案 0 :(得分: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方法------------ // < / p>

- (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");

    }
}