Web服务在模拟器上工作,但不是真正的设备

时间:2011-10-12 14:08:14

标签: iphone web-services

我刚把我的iOS应用程序放在连接到我的无线网络的iPad上,我无法连接到网络服务。这是在模拟器中工作,但是当我把它放在iPad上时,连接超时。放在真实设备上时是否需要不同的东西?

不过这是我的代码:

-(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服务?

1 个答案:

答案 0 :(得分:3)

192.168.51.3看起来像一个网络的私有子网,可能是你的WiFi在另一个子网上?