2013-10-22 23 views
0

我的JSON在服务器上,假设我的网址是如何使用身份验证从服务器获取JSON数据?

https://abc.companyname.com/posts/list/10 

当我粘贴URL在浏览,它要求我用户名密码。当我输入这些证书时,它会返回一些JSON格式的数据,如下所示。

[{"id":"5","picture":"myimage.jpg","name":"Usman Zafar","type":"textonly","message":"Woo this is demo and it looks good thanks adeco","image":null,"video_id":null,"datecreated":"2013-10-17 10:14:02","totalLikes":0,"totalComments":0,"commentsData":null}, 


{"id":"6","picture":"default.jpg","name":"Usman Zafar","type":"textonly","message":"Hello this is the demo of another post but no image","image":null,"video_id":null,"datecreated":"2013-10-17 10:31:04","totalLikes":0,"totalComments":0,"commentsData":null}, 


{"id":"7","picture":"default.jpg","name":"Usman Zafar","type":"textonly","message":"Hello this is the demo of another post but no image","image":null,"video_id":null,"datecreated":"2013-10-17 10:31:24","totalLikes":0,"totalComments":0,"commentsData":null}, 


{"id":"11","picture":"myimage_9.jpg","message":"Regukar Text comments no.772"}]}] 

问题

如何在iPhone编程认证(用户名+密码)创建NSURLConnection的?

如果需要关于问题的任何其他数据,请通过评论让我知道。

谢谢

+0

可能的复制http://stackoverflow.com/questions/1973325/nsurlconnection-and-basic-http-authentication – Priyatham51

+0

我看到的是,它使用的NSMutableString,其中因为我没有这样的对象。 – Duaan

+0

NSMutableString是一个可变字符串,也就是可以编辑其内容的字符串。你可以从你的字符串中创建NSMutableString。 Ex NSMutableString * newString = [[NSMutableString alloc] initWithString:@“yourString”]; – Priyatham51

回答

0

这是更多的应用程序设计。首先,通过向用户显示一些UI屏幕来捕获用户名和密码,然后您需要将数据发送到服务器。你可以将普通的键值对转换成NSData。

看看下面的一段代码开始。

=============================================================================================== 

self.URLRequest = [NSMutableURLRequest requestWithURL:self.URL cachePolicy:NSURLRequestReloadIgnoringCacheData timeoutInterval:999]; 
[self.URLRequest setHTTPMethod:[self methodStringForMethod:self.requestMethod]]; 
    for (NSString *aHeaderKey in self.requestHeader) { 
     [self.URLRequest setValue:[self.requestHeader valueForKey:aHeaderKey] forHTTPHeaderField:aHeaderKey]; 
    } 

    if (self.requestHeader) { 
     [self.URLRequest setValue:@"gzip" forHTTPHeaderField:@"Accept-Encoding"]; 
    } 

    if (self.body) { 
     [self.URLRequest setHTTPBody:[self keyValuePostDataFromDictionary:self.body]]; 
    } 

    if (self.connection) { 
     [self.connection cancel]; 
     self.connection = nil; 
    } 
self.connection = [[NSURLConnection alloc] initWithRequest:self.URLRequest delegate:self]; 
=============================================================================================== 

- (NSData *)keyValuePostDataFromDictionary:(NSDictionary *)iDictionary { 
    return [[NSString runnerHttpPostBodyStringWithQueryParameters:iDictionary] dataUsingEncoding:NSUTF8StringEncoding]; 
} 


- (NSString *)stringByAppendingRunnerQueryParameters:(NSDictionary *)iParameters appendQuestionMark:(BOOL)iAppendQuestionMark { 
    BOOL aAppendAmpersand = YES; 
    NSMutableString *aWorking = [NSMutableString stringWithString:self]; 

    if (iAppendQuestionMark) { 
     NSRange aQueryBeginning = [self rangeOfString:@"?"]; 
     if (aQueryBeginning.location == NSNotFound) { 
      [aWorking appendString:@"?"]; 
      aAppendAmpersand = NO; 
     } 
    } else { 
     aAppendAmpersand = NO; 
    } 

    for (id aKey in iParameters) { 
     id aValue = [iParameters valueForKey:aKey]; 
     NSString *aKeyStr = [self convertRunnerObjectToURLEncodedValue:aKey]; 

     if (aAppendAmpersand) { 
      [aWorking appendString:@"&"]; 
     } else { 
      aAppendAmpersand = YES; 
     } 

     if ([aValue isKindOfClass:[NSArray class]]) { 
      NSArray *aSubParamaters = (NSArray *)aValue; 
      BOOL aFirstTime = YES; 
      for (id aSubValue in aSubParamaters) { 
       NSString *aValueString = [self convertRunnerObjectToURLEncodedValue:aSubValue]; 

       if (!aFirstTime) { 
        [aWorking appendString:@"&"];     
       } 
       [aWorking appendString:aKeyStr]; 
       [aWorking appendString:@"="]; 
       [aWorking appendString:aValueString]; 

       aFirstTime = NO; 
      } 

     } else { 
      NSString *aValueString = [self convertRunnerObjectToURLEncodedValue:aValue]; 
      [aWorking appendString:aKeyStr]; 
      [aWorking appendString:@"="]; 
      [aWorking appendString:aValueString];   
     } 

    } 

    return [NSString stringWithString:aWorking];   
} 
0

使用下面的代码:

NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:fileURL]; 
[request setCachePolicy:NSURLRequestReloadIgnoringLocalCacheData]; 

NSURLResponse *response = nil; 
NSData *data = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:error]; 
相关问题