getting result of a html page

Discussion in 'iOS Programming' started by bobisa, Nov 9, 2010.

  1. bobisa macrumors newbie

    Joined:
    Nov 8, 2010
    #1
    i had made a kind of html webservice

    i want to be able to call by providing a parameters for example this page
    http://mysite/pStatus.php?P1=myparam

    this page will generate a page with no html balise, it will juste generate three answer

    it can be

    -1
    0
    1

    and can i retrieve the source of a html request
     
  2. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
  3. bobisa thread starter macrumors newbie

    Joined:
    Nov 8, 2010
    #3
    yes, my question is how i retrieve the source of a webpage into a string ?
     
  4. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #4
    You have at least two options:

    1) If a synchronous (blocking) approach is desired (not recommended, though), you could simply use one of the instance methods of NSString that returns a string by reading data from a given URL.

    2) If an asynchronous (non-blocking) approach is desired, you could look into using NSURLConnection as described in the URL Loading System Programming Guide.
     
  5. bobisa, Nov 9, 2010
    Last edited by a moderator: Nov 9, 2010

    bobisa thread starter macrumors newbie

    Joined:
    Nov 8, 2010
    #5
    if i follow this example, the result will be in the receivedData variable

    if yes, what kind of variable it will be a string ?? and where do i have to declare it ??


    Code:
    NSURLRequest *theRequest=[NSURLRequest requestWithURL:[NSURL URLWithString:@"http://mywebsite.com/"]
                            cachePolicy:NSURLRequestUseProtocolCachePolicy
                        timeoutInterval:60.0];
    // create the connection with the request
    // and start loading the data
    NSURLConnection *theConnection=[[NSURLConnection alloc] initWithRequest:theRequest delegate:self];
    if (theConnection) {
        // Create the NSMutableData to hold the received data.
        // receivedData is an instance variable declared elsewhere.
        receivedData = [[NSMutableData data] retain];
    } else {
        // Inform the user that the connection failed.
     
  6. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #6
    It will be an NSMutableData. You will still need to convert it to an NSString. Since the comments allude to it being an instance variable, you should declare it where you normally declare instance variables.

    P.S. Just be aware that the code you've supplied will not be enough to handle the entire task. There is still the delegate methods needed.
     
  7. bobisa thread starter macrumors newbie

    Joined:
    Nov 8, 2010
    #7
    do you mean
    - (void)connectionDidFinishLoading,....
    and so on ?
     
  8. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #8
    Based on this quote from the URL Loading System Programming Guide:
    what do you think? :)
     
  9. bobisa thread starter macrumors newbie

    Joined:
    Nov 8, 2010
    #9
    i know it sound like i dont know what i do , but its the first time that i am coding something like that on a mac

    habitually i am doing windows coding :)

    that why
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Make sure you are comfortable with the fundamentals of Objective-C / Cocoa Touch coding before tackling the real issues. If not, no harm in stepping back and taking time to get to that comfort level.
     
  11. bobisa, Nov 9, 2010
    Last edited by a moderator: Nov 9, 2010

    bobisa thread starter macrumors newbie

    Joined:
    Nov 8, 2010
    #11
    i did some c++ at school.

    it look familiar but i have to get rid of what i am doing in windows coding :)

    when i click on my button , i got kicked out of the software on the iphone simulator

    how can i find what is doing that

    here is my code


    Code:
    - (IBAction)buttonClicked:(id)sender {
    	// Create the request.
    	webData = [[NSMutableData data] retain];
    	NSLog("Before request");
    	NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:@"http://html_webservice.gcilogistic.com/pParsCheckup.awp?P1=a"]];
    	[[NSURLConnection alloc] initWithRequest:request delegate:self];
    	NSLog("after request");
    }
    
    
    - (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    	NSLog("Void didreceiveresponse");
    	[webData setLength:0];
    }
    
    - (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    	NSLog("Void didreceiverdata");
    	[webData appendData:data];
    }
    - (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    	NSLog("connection failed");
    	//label.text = [NSString stringWithFormat:@"Connection failed: %@", [error description]];
    }
    
    - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    	NSLog("connectionfinishloading");
    	[connection release];
    }
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    Time to apply some basic debugging. Looks like you already have a number of NSLogs in your code. Which ones are you seeing in the console? Are you seeing anything else? Run-time errors, perhaps? And did your code build without any compile-time errors or warnings? If not, address those first. Etc.
     
  13. bobisa, Nov 9, 2010
    Last edited by a moderator: Nov 9, 2010

    bobisa thread starter macrumors newbie

    Joined:
    Nov 8, 2010
    #13
    i am not able to see the NSLog("Before request");

    i got the following in the console

    Program received signal : "EXC_BAD_ACCESS"

    found my error
    missing @ in the nslog function
     

Share This Page