2014-09-05 23 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即將空 - 請求沒有響應NSURLConnection的可能NSURLConnection的解析問題

請建議如何解決它。 謝謝

+0

將'else'子句添加到'if(error == nil)'和log'[errors localizedDescription]'並且您的知識將會擴展。 – trojanfoe 2014-09-05 10:32:59

回答

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)也適當。但是,當我嘗試同步到數據時,所有的鍵值都變爲空。 – 2014-09-05 11:37:00

+0

放置斷點並檢查是否在字符串中獲取了空值。 – 2014-09-05 11:45:31

+0

當我嘗試打印它以XML格式打印的字符串時。 「服務器拒絕了此請求,因爲請求實體的格式不是所請求方法的請求資源支持的格式。」 – 2014-09-05 11:53:38