NSURLConnection post方法不起作用

时间:2015-10-06 14:45:36

标签: ios objective-c

嗨,我是iOS的初学者,在我的项目中,有使用NSURLConnection发布请求的用户“ForGotPassword”请求的设施

为此,我写了一些代码,但这不起作用并显示异常,如

“此类不符合关键值MedicaidId编码。”

我做错了什么,请帮帮我

我的代码: -

- (void)viewDidLoad {

 [super viewDidLoad];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc] initWithURL:[NSURL URLWithString:@"my-url"]];

    [request setHTTPMethod:@"POST"];

    [request setValue:self.MedicalIdTxt.text forKey:@"MedicaidId"];

    [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];

    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if (theConnection) {
        NSLog(@"connected");

        receivedData=[[NSMutableData alloc]init];
    }
    else {

        NSLog(@"not connected");
    }
}

- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
{
    [receivedData setLength:0];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
{
    [receivedData appendData:data];
    NSString* responseString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
    NSLog(@"response: %@",responseString);
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection
{
    NSLog(@"Succeeded! Received %lu data",(unsigned long)[receivedData length]);

    NSString* responseString = [[NSString alloc] initWithData:receivedData encoding:NSUTF8StringEncoding];

    NSLog(@"response: %@",responseString);

    NSError *myError = nil;

    NSDictionary *res = [NSJSONSerialization JSONObjectWithData:receivedData options:NSJSONReadingMutableLeaves error:&myError];

    NSLog(@"final result is %@",res);
}

1 个答案:

答案 0 :(得分:0)

-[NSObject setValue:forKey:]不是您想要的方法。

这就是你想要的:Append data to a POST NSURLRequest