2013-09-17 1 views
0

Я новичок в разработке iOS.NSURLRequest + JSON: Плохой URL

Я использую следующий код, чтобы отправить запрос на сервер и получить синхронный ответ.

Мой запрос содержит объект JSON. Когда я запускаю симулятор, я получил ошибку «Плохой URL».

Пожалуйста, помогите мне решить эту проблему.

NSString *regUrlWithJson = @"https://dso.mpl.com/reg?json={\"operation\":\"login\",\"contactEmail\":\"[email protected]\",\"password\":\"e3dba1bbfab020a2eb105c69405a7766\",\"SSO\":\"true\",\"service\":\"lp\",\"sig\":\"true\",\"url\":\"mpl\"}"; 

NSLog(regUrlWithJson); 
NSURLRequest * urlRequest = [NSURLRequest requestWithURL:[NSURL URLWithString:regUrlWithJson]]; 

NSURLResponse * response = nil; 
NSData * data = [NSURLConnection sendSynchronousRequest:urlRequest 
             returningResponse:&response 
                error:&error]; 

if (error != nil) 
{ 
    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:[error localizedDescription] delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert show]; 
} 
else{ 
    NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 

    UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Login" message:string delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
    [alert show]; 
} 

Спасибо,

+0

Получил ответ. Необходимо кодировать URL-адрес. NSURL * theUrl = [NSURL URLWithString: [regUrlWithJson stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]]; NSURLRequest * urlRequest = [Запрос NSURLRequestWithURL: theUrl]; – user2787834

ответ

0

regUrlWithJson не является действительным URL. Вот почему