2014-09-05 2 views
0
NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:SPACEREQUESTFINAL_URL] cachePolicy:NSURLRequestUseProtocolCachePolicy 
                timeoutInterval:60]; 
NSString *authStr = [NSString stringWithFormat:@"%@:%@", @"mymac", @"qerty!98"]; 
NSData *authData = [authStr dataUsingEncoding:NSUTF8StringEncoding]; 
NSString *authValue = [NSString stringWithFormat:@"Basic %@", [authData base64EncodedString]]; 
[request setValue:authValue forHTTPHeaderField:@"Authorization"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
[request setValue:[NSString stringWithFormat:@"%d",[data length]] forHTTPHeaderField:@"Content-Length"]; 
[request setHTTPBody:data]; 
[request setHTTPMethod:@"POST"]; 
[request setTimeoutInterval:120]; 
[request setAllHTTPHeaderFields:jsonDictionary]; 
NSHTTPURLResponse* urlResponse=nil; 
NSError *error=nil; 
NSData *serverResponse = [NSURLConnection sendSynchronousRequest:request returningResponse:&urlResponse error:&error]; 
NSLog(@"ServerResponse %@",serverResponse); 
    if (error==nil) 
    { 
[NSURLConnection connectionWithRequest:request delegate:self]; 
NSString *str1=[[NSString alloc]initWithData:serverResponse encoding:NSUTF8StringEncoding]; 
NSLog(@"SSTR %@",str1); 
NSMutableDictionary *JsonDict=[NSJSONSerialization JSONObjectWithData:serverResponse options:NSJSONReadingAllowFragments error:&error]; 
} 

데이터 ServerResponse가 null오고 파싱되지 않은 - 요청이있는 NSURLConnection 구문 분석 문제

그것을 해결하는 방법을 제안 해주세요 수있는 NSURLConnection에 응답하지 않습니다. 는

+0

'(오류 == 전무)'와 로그'[오류 localizedDescription]'당신의 지식을 확장 할 경우에'else' 절을 추가 감사드립니다. – trojanfoe

답변

0
NSMutableDictionary *datadict=[NSMutableDictionary dictionary]; 
[datadict setObject:[NSString stringWithFormat:@"%@", mymac] forKey:@"yourkeytoidentify"]; 
[datadict setObject:[NSString stringWithFormat:@"%@",qerty!98] forKey:@"yourkeytoidentify"]; 
NSData* jsonData = [NSJSONSerialization dataWithJSONObject:datadict options:kNilOptions error:nil]; 
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init]; 
NSURL *someURLSetBefore =[NSURL URLWithString:@" url "]; 
[request setURL:someURLSetBefore]; 
[request setHTTPMethod:@"POST"]; 
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"]; 
// [request setValue:@"application/json" forHTTPHeaderField:@"Content-Type"]; 
// [request setValue:@"application/json" forHTTPHeaderField:@"Accept"]; 
[request setHTTPBody:jsonData]; 
NSError *error; 
NSURLResponse *response; 
NSData *responseData=[NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&error]; 
NSString * string1=[[NSString alloc]initWithData:responseData encoding:NSUTF8StringEncoding]; 
NSLog(@"%@",string1); 

} 
} 
+0

그런데 사용하지 않으려 고했지만 .... 데이터가 적절하고 요청 (NSMutableURLRequest)도 적절합니다. 그러나 데이터에 동기화하려고하면 모든 키 값이 null이됩니다. –

+0

중단 점을 넣고 문자열에 null 값이 있는지 확인하십시오. –

+0

문자열을 인쇄하려고하면 XML로 인쇄됩니다. "요청 엔터티가 요청 된 메서드에 대해 요청 된 리소스에서 지원되지 않는 형식이므로 서버가이 요청을 거부했습니다." –