1

こんにちは、私は iOS 開発の初心者です。上記のコードでは、応答が json 形式の Web サービスを呼び出しています。そのWebサービスの応答でjson解析を行いたいです。

ここで、json と registartion の値がコンソールで null を表示している理由 plz を助けてください。

ここに私の WebService.h ファイルがあります

@interface WebService : NSObject<NSURLConnectionDelegate ,NSURLConnectionDataDelegate> {
NSMutableData *data;
NSArray  * registrationDetail;
}
-(void)callWebService;
@end

ここに私のWeb WebService.mファイル

@implementation WebService

-(void)callWebService {
NSString *JsonString =[ NSString stringWithFormat:@"http://bia.igreentechservices.com/ws/ws_registration.php?emailid=%@&password=%@&fname=%@&lname=%@&gender=%@&birthdate=%@&mobile=%@&address=%@&emergency_contact_no=%@&other_contact_no_1=%@&other_contact_no_2=%@",emailAdd,pwd,fName,lName,gen,bDate,mobileNum,add,emergencyContactNum,anotherContactNum1,anothercontactNum2]


   UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
   NSURL *url = [NSURL URLWithString:JsonString];
   NSURLRequest *request =[NSURLRequest  requestWithURL:url];
   NSURLConnection *myConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];
   [myConnection start];
}

-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {


 data= [[NSMutableData alloc] init];
}


-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)theData {
[data appendData:theData];
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {
[UIApplication sharedApplication].networkActivityIndicatorVisible = NO;
NSString *responseString = [[NSString alloc]
    initWithData:data encoding:NSUTF8StringEncoding];
NSDictionary *json = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
registrationDetail = [json objectForKey:@"registration"];
NSLog(@"%@",json);
NSLog(@"%@",registrationDetail);
NSLog(@"%@",responseString);
NSLog(@"%@",data);

}

4

1 に答える 1

2

空白に %20 を追加する必要がある空白を含む URL 文字列に含まれている可能性があります。次のコード行を追加するだけです:-

JsonString = [JsonString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

あなたはまた、NSURLConnection以下のように使用してメソッドを呼び出しました:-

-(void)callWebService {
NSString *JsonString =[ NSString stringWithFormat:@"http://bia.igreentechservices.com/ws/ws_registration.php?emailid=%@&password=%@&fname=%@&lname=%@&gender=%@&birthdate=%@&mobile=%@&address=%@&emergency_contact_no=%@&other_contact_no_1=%@&other_contact_no_2=%@",emailAdd,pwd,fName,lName,gen,bDate,mobileNum,add,emergencyContactNum,anotherContactNum1,anothercontactNum2]


   UIApplication sharedApplication].networkActivityIndicatorVisible = YES;
   NSURL *url = [NSURL URLWithString:JsonString];
   NSURLRequest *request =[NSURLRequest  requestWithURL:url];
   NSURLConnection *myConnection =[[NSURLConnection alloc] initWithRequest:request delegate:self];

    if(myConnection)
        {
            data=[[NSMutableData alloc]init]; 
        }
        else
        {
            NSLog(@"connection not complet")
        }

}


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {

    [data setLength: 0];
}
-(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [data appendData:data];
}
-(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {

    NSLog(@"Error is %@",error);
}
-(void)connectionDidFinishLoading:(NSURLConnection *)connection {

    NSString *responseString = [[NSString alloc] initWithData:self.webData encoding:NSUTF8StringEncoding];

    data = nil;
    [connection release];
    [data release];

}
于 2013-03-08T05:38:20.770 に答える