2011-10-12 126 views
1

我剛剛將我的iOS應用程序放到了我的iPad上,該iPad已連接到我的無線網絡,而且我無法連接到Web服務。這在模擬器中工作,但是當我把它放在iPad上時,連接超時。放在真實的設備上時是否需要不同的東西?在模擬器上工作的Web服務,但不是真實的設備

這裏是我的代碼還是:

-(BOOL)Webservice{ 
recordResults = FALSE; 
user = nameInput.text; 
pass = passwordInput.text; 




NSString *soapMessage = [NSString stringWithFormat: 
         @"<?xml version=\"1.0\" encoding=\"utf-8\"?>\n" 
         "<soap:Envelope xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">\n" 
         "<soap:Body>\n" 
         "<ValidateUser xmlns=\"http://tempuri.org/\">\n" 
         "<User>%@</User>\n" 
         "<Password>%@</Password>\n" 
         "</ValidateUser>\n" 
         "</soap:Body>\n" 
         "</soap:Envelope>\n", nameInput.text, passwordInput.text 
         ]; 



NSLog(soapMessage); 

NSURL *url = [NSURL URLWithString:@"http://192.168.51.3:60010/Webservice/IDLMobile.asmx?WSDL"]; 
NSMutableURLRequest *theRequest = [NSMutableURLRequest requestWithURL:url]; 
NSString *msgLength = [NSString stringWithFormat:@"%d", [soapMessage length]]; 

[theRequest addValue: @"text/xml; charset=utf-8" forHTTPHeaderField:@"Content-Type"]; 
[theRequest addValue: @"http://tempuri.org/ValidateUser" forHTTPHeaderField:@"soapAction"]; 
[theRequest addValue: msgLength forHTTPHeaderField:@"Content-Length"]; 
[theRequest setHTTPMethod:@"POST"]; 
[theRequest setHTTPBody: [soapMessage dataUsingEncoding:NSUTF8StringEncoding]]; 

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

if(theConnection) 
{ 
    webData = [[NSMutableData data] retain]; 
} 
else 
{ 
    NSLog(@"theConnection is NULL"); 
} 

[nameInput resignFirstResponder]; 
[passwordInput resignFirstResponder]; 

/* update function */ 
    } 


-(void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response 
{ 
[webData setLength: 0]; 
    } 
    -(void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data 
    { 
[webData appendData:data]; 
    } 
    -(void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error 
    { 
NSLog(@"ERROR with theConenction"); 
[connection release]; 
[webData release]; 
    } 
    -(void)connectionDidFinishLoading:(NSURLConnection *)connection 
    { 
NSLog(@"DONE. Received Bytes: %d", [webData length]); 
NSString *theXML = [[NSString alloc] initWithBytes: [webData mutableBytes] length:  [webData length] encoding:NSUTF8StringEncoding]; 
NSLog(theXML); 
[theXML release]; 

if(xmlParser) 
{ 
    [xmlParser release]; 
} 

xmlParser = [[NSXMLParser alloc] initWithData: webData]; 
[xmlParser setDelegate: self]; 
[xmlParser setShouldResolveExternalEntities: YES]; 
[xmlParser parse]; 

[connection release]; 
[webData release]; 
} 

    -(void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *) namespaceURI qualifiedName:(NSString *)qName 
     attributes: (NSDictionary *)attributeDict 
{ 

if ([elementName isEqualToString:@"Message"]) 
{ 
    //---displays the country--- 

    NSLog(soapResults); 

    NSString *convertString = soapResults; 
    // check = 0; 
    check = [convertString intValue]; 
    thyCheck = [convertString intValue]; 
    NSLog(@"user is"); 
    // NSLog(user); 
    if(check > 0){ 

     [self someUpdateFunction]; 


     NSString *string = [NSString stringWithFormat:@"%d", check]; 
     NSLog(string); 
     NSLog(@"lolololol"); 
     check = 0; 


    }else{ 
      UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Problem!" message:@"Incorrect Username or Password" 
                  delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:@"Yes", nil]; 
     [alert show]; 
     [alert release]; 
     thyCheck = 0; 
    } 
    // gotoContent = checkConfirm; 
} 



if([elementName isEqualToString:@"ValidateUserResponse"]) 
{ 

    if(!soapResults) 
    { 
     soapResults = [[NSMutableString alloc] init]; 
     NSLog(soapResults); 
     NSLog(@"above is soap validate user"); 


    } 

    recordResults = TRUE; 
} 


    } 



    -(void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string 
    { 
if(recordResults) 
{ 
    [soapResults appendString: string]; 
} 
    } 

我能想到的另一件事是,iPhone必須連接到3G網絡?所以它不能通過連接到路由器來訪問Web服務?

+3

192.168.51.3看起來像一個網絡的私人子網,可能是你的WiFi在另一個子網上? – rckoenes

+0

我認爲這可能是一個問題。我會盡快檢查並儘快回覆您 –

+0

您是對的。愚蠢的我,但我被給了這些細節,我試圖訪問一個私人服務器。如果你把答案放在下面,我會選擇它作爲正確的。 –

回答

3

192.168.51.3看起來像一個網絡的私有子網,可能是你的WiFi在另一個子網上?

+0

雅就是這樣。 –

相關問題