2012-10-08 72 views
0

目前我正在iPhone應用程序中工作,使用JSON發送請求並獲取響應來讀取它。iPhone中的JSON Webservice問題?

請求(例如URL):

http://www.Genifer.com/index.php?q=api/username-available&username=stephen 

我是指從瀏覽器(Firefox)的響應:

{"status":true,"result":true} 

我試圖在Xcode:

NSString *urlString = [NSString stringWithFormat:@" http://www.Genifer.com/index.php?"]; 
     NSString *parameter = [NSString stringWithFormat:@"q=api/username-available&username=stephen"]; 

     NSData *parameterData = [parameter dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES]; 
     NSURL *url = [NSURL URLWithString:urlString]; 

     NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 

     [theRequest addValue: @"application/x-www-form-urlencoded; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
     [theRequest setHTTPMethod:@"POST"]; 
     [theRequest setHTTPBody:parameterData]; 

     NSURLConnection *connection = [[NSURLConnection alloc] 
             initWithRequest:theRequest delegate:self]; 

     if(connection) 
     { 
      mutableData = [[NSMutableData alloc] init]; 
     } 
     else 
     { 

     } 


-(void) connection:(NSURLConnection *) connection didReceiveResponse:(NSURLResponse *)response 
{ 
    [mutableData setLength:0]; 
    NSLog(@"mutableData:%@",mutableData); 
} 

-(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
{ 
    [mutableData appendData:data]; 
    NSLog(@"mutableData:%@",mutableData); 

} 

-(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
{ 
    [mutableData release]; 
    [connection release]; 
    return; 
} 


-(void)connectionDidFinishLoading:(NSURLConnection *)connection 
{ 


     NSString *jsonStr = [[NSString alloc] initWithData:mutableData encoding:NSUTF8StringEncoding]; 
     NSLog(@"JSonSTr : %@", jsonStr); 

} 

響應來自:

JSonSTr : <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en" dir="ltr"> 
    <head> 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> 

我在Firefox瀏覽器中引用請求,得到類似{「status」:true,「result」:true}的回覆。 但我試圖在xcode中集成相同的請求,但響應不同,如何解決這個問題? 請幫我

由於提前

回答

2

你能與此

NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; 
NSDictionary *jsonDict = [jsonString JSONValue]; 

檢查什麼樣的價值觀做這樣的事情

NSDictionary *question = [jsonDict objectForKey:@"status"]; 
+0

感謝您的答覆彈出在你的字典查詢 – SampathKumar

+0

如果這有助於你可以標記答案是正確的? –

+0

我已經得到了jsonString的響應 NSLOG - JSonSTr:<!DOCTYPE html PUBLIC「 - // W3C // DTD XHTML 1.0 Strict // EN」「http://www.w3.org/TR/xhtml1/DTD /xhtml1-strict.dtd「> < meta http-equiv =「Content-Type」content =「text/html; charset = utf-8」/> – SampathKumar