通过iOS将数据发送到POST JSON Web服务

我想使用下面的代码将数据传递给POST JSON Web服务.
Nsstring *urlString = @"http://3.10.204.99:9090/service/outage/data/getShortFormData";
NSDictionary *inputData = [[NSDictionary alloc] initWithObjectsAndKeys:
                           @"1020",@"outageId",@"Alex",@"customerName",nil];

NSError *error = nil;
NSData *jsonInputData = [NSJSONSerialization dataWithJSONObject:inputData options:NSJSONWritingPrettyPrinted error:&error];
Nsstring *jsonInputString = [[Nsstring alloc] initWithData:jsonInputData encoding:NSUTF8StringEncoding];
[self checkWithServer:urlString jsonString:jsonInputString];
-(void)checkWithServer:(Nsstring *)urlname jsonString:(Nsstring *)jsonString{

    NSURL *url1 = [NSURL URLWithString:urlname];
    NSMutableuRLRequest *request = [NSMutableuRLRequest requestWithURL:url1];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [request setValue:@"charset=utf-8" forHTTPHeaderField:@"Content-Type"];
    [request setHTTPBody:[jsonString dataUsingEncoding:NSUTF8StringEncoding]];
    NSURLResponse *response;
    NSError *err;
    NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
    id jsonResponseData = [NSJSONSerialization JSONObjectWithData:responseData options:kNilOptions error:nil];

    NSDictionary *jsonResponseDict;
    if ([jsonResponseData isKindOfClass:[NSDictionary class]]) {
        jsonResponseDict = jsonResponseData;
    } else {
        // Error-handling code
    }
    jsonResponseData = [jsonResponseDict objectForKey:@"d"];
    if (jsonResponseData == nil) {
        // Server may have returned a response containing an error
        // The "ExceptionType" value is returned from my .NET server used in sample
        id jsonExceptioTypeData = [jsonResponseDict objectForKey:@"ExceptionType"];
        if (jsonExceptioTypeData != nil) {
            NSLog(@"%s ERROR : Server returned an exception",__func__);
            NSLog(@"%s ERROR : Server error details = %@",__func__,jsonResponseDict);
        }
    }
}

执行此代码时,它不会给出错误,但数据不会更新.

对此的任何帮助都会感到遗憾.

谢谢你们.

解决方法

请尝试以下代码
+(NSData *)postDataToUrl:(Nsstring*)urlString:(Nsstring*)jsonString
{
    NSData* responseData = nil;
    NSURL *url=[NSURL URLWithString:[urlString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
    responseData = [NSMutableData data] ;
    NSMutableuRLRequest *request=[NSMutableuRLRequest requestWithURL:url];
    Nsstring *bodydata=[Nsstring stringWithFormat:@"data=%@",jsonString];

    [request setHTTPMethod:@"POST"];
    NSData *req=[NSData dataWithBytes:[bodydata UTF8String] length:[bodydata length]];
    [request setHTTPBody:req];
    NSURLResponse* response;
    NSError* error = nil;
    responseData = [NSURLConnection sendSynchronousRequest:request     returningResponse:&response error:&error];
    Nsstring *responseString = [[Nsstring alloc] initWithData:responseData encoding:NSUTF8StringEncoding];

    NSLog(@"the final output is:%@",responseString);

    return responseData;
}

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...