错误域= NSCocoaErrorDomain代码= 3840"操作无法完成。 (可可误差3840。)" (字符0周围的值无效。)

时间:2016-06-08 13:37:48

标签: ios objective-c post nsurlconnection

以下是代码

NSDictionary *post = [NSDictionary dictionaryWithObjectsAndKeys:
    @"signup",@"methodName",_tfName.text,@"name",_tfNickName.text,@"nickname",
        _dateOfBirth.text, @"birth_date",
        @"1",@"gender",
        _tfEmail.text,@"email",
                          _tfPassword.text,@"password",
                          _tfContact.text, @"mobile",
                          _tfCountry.text,@"country",


                          @"Normal",@"social_provider",
                          @"",@"social_accesstoken",@"3",@"sponsor_choice" ,_tfPinCode.text,@"pincode",@"",
                                 @"fromLogin",nil];

这是实际发布数据的过程

NSData *postdata = [NSJSONSerialization dataWithJSONObject:post options:NSJSONWritingPrettyPrinted error:nil];

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    [request setURL:[NSURL URLWithString:[NSString stringWithFormat:@"http://abc@login.php"]]];
    [request setHTTPMethod:@"POST"];
    [request setValue:@"multipart/form-data" forHTTPHeaderField:@"Content-Type"];


    [request setHTTPBody:postdata];

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


    if(conn)
    {
        NSLog(@"Connection Successful");
    }
    else
    {
        NSLog(@"Connection could not be made");
    }


}

URLConnection的DeleGates

  - (void)connection:(NSURLConnection*)connection didReceiveResponse:(NSURLResponse *)response
    {
        NSLog(@"Did Receive Response %@", response);
        responseData = [[NSMutableData alloc]init];
    }
    - (void)connection:(NSURLConnection*)connection didReceiveData:(NSData*)data
    {
        //NSLog(@"Did Receive Data %@", data);
        [responseData appendData:data];


    }

我在这里获得响应数据的价值

  - (void)connection:(NSURLConnection*)connection didFailWithError:(NSError*)error
    {
        NSLog(@"Did Fail");
    }
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection
    {
        NSLog(@"Did Finish");



       NSError *error = nil;
       NSDictionary *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options:NSJSONReadingAllowFragments error:&error];

       NSLog(@"Array: %@", jsonArray);
        // Do something with responseData
    }

实际上我认为jsonarray的价值为零。即使我从服务器获得响应数据的价值。

1 个答案:

答案 0 :(得分:1)

如果我同步发布数据,那么我得到了响应,甚至我得到了JSON

NSData *responseData =[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&err];
        if (!err)
        {
            //Getting response from server
            NSArray *jsonArray = [NSJSONSerialization JSONObjectWithData:responseData options: NSJSONReadingMutableContainers error: &err];

            NSLog(@"%@", jsonArray);
        }