错误域= NSCocoaErrorDomain代码= 3840“该操作不能完成。(可可错误3840)”

问题描述:

以下(无效的周围字符0的值)是代码错误域= NSCocoaErrorDomain代码= 3840“该操作不能完成。(可可错误3840)”

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://[email protected]"]]]; 
    [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

- (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

您可以登录在'connectionDidFinishLoading:':'的NSString *海峡= [[NSString的页头] initWithData:responseData编码:NSUTF8StringEncoding] '? – Larme

+0

它显示HTML内容 –

+0

而内容似乎有效(我的意思是,返回的数据是正确的,而不是格式化的方式,但他们有意义)?那么它不是JSON。如果您使用POSTMan(或类似的)进行测试,您的服务是否响应JSON? – Larme

如果我发布的数据同步方式,然后我得到了响应,并连我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); 
     }