Sending Request and Get Response

前端 未结 3 569
广开言路
广开言路 2021-01-07 14:01

I have a php code running on my server that i call my web service.It processes the data in send integer value.How can i get that?Here is my request url :

  N         


        
相关标签:
3条回答
  • 2021-01-07 14:33

    You can use NSURLConnection. You should use implement the NSURLConnectionDataDelegate protocol and use the NSURLConnection class.

    -(void) requestPage
    {
        NSString *urlString = @"http://the.page.you.want.com";
        NSURL *url = [NSURL URLWithString:urlString];
    
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url cachePolicy:NSURLCacheStorageAllowed timeoutInterval:20.0f];
    
    
        responseData = [[NSMutableData alloc] init];
        connection = [[NSURLConnection connectionWithRequest:request delegate:self] retain];
        delegate = target;
    }
    
    
    -(void) connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response
    {   
        if ([response isKindOfClass:[NSHTTPURLResponse class]])
        {
            NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*) response; 
            //If you need the response, you can use it here
        }
    }
    
    -(void) connection:(NSURLConnection *)connection didReceiveData:(NSData *)data
    {
        [responseData appendData:data];
    }
    
    -(void) connection:(NSURLConnection *)connection didFailWithError:(NSError *)error
    {
        [responseData release];
        [connection release];
    }
    
    -(void) connectionDidFinishLoading:(NSURLConnection *)connection
    {
        if (connection == adCheckConnection)
        {
            NSString *responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
    
            //You've got all the data now
            //Do something with your response string
    
    
            [responseString release];
        }
    
        [responseData release];
        [connection release];
    }
    
    0 讨论(0)
  • 2021-01-07 14:33
    1. visit HTTPRequest and setup it in your project according to given instructins.
    2. TouchJSON - download and put it in your project.
    3. below are the methods you can use for sending request, and getting response.

        -(void) SendAsyncReq:(NSString *)urlString 
         {
      
      
      NSLog(@"URL IS: %@",urlString);
      NSURL *url = [NSURL URLWithString:[urlString     stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];
      
      
      ASIHTTPRequest *request1 = [ASIHTTPRequest requestWithURL:url];
      
      [request1 setDelegate:self];
      
      
          [request1 startAsynchronous];
      }
      
        /// THIS METHOD WILL BE CALLED IF REQUEST IS COMPLETED SUCCESSFULLY
      
       - (void)requestFinished:(ASIHTTPRequest *)response
      {
      NSData *responseData = [response responseData];
      
      CJSONDeserializer *jsonDeserializer = [CJSONDeserializer   deserializer];
      
      NSDictionary *resultsDictionary = [jsonDeserializer  deserializeAsDictionary:responseData error:nil];
      
      NSLog(@"Response is: %@",resultsDictionary);
      
      
          }
      
       /// THIS METHOD WILL BE CALLED IF REQUEST IS FAILED DUE TO SOME REASON.
      
      - (void)requestFailed:(ASIHTTPRequest *)response
      {
      NSError *error = [response error];
      NSLog(@"%d", [error code]);
      if([error code] !=4)
      {
          NSString *errorMessage = [error localizedDescription];
          UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Error"
                                                              message:errorMessage
                                                         delegate:nil
                                                cancelButtonTitle:@"OK"
                                                otherButtonTitles:nil];
          [alertView show];
          [alertView release];
      }
      
      
      
      }
      

    you must import #import "CJSONDeserializer.h" and #import "ASIHTTPRequest.h" in your class. I hope this will help.

    0 讨论(0)
  • 2021-01-07 14:48

    If you're looking for a way to make HTTP requests to a server, there's a number of frameworks that's helping you do that. Up until recently, ASIHTTPRequest was the one i favored, but unfortunately it's been discontinued.

    Google's API Client Library is also a good option, as well as a number of others suggested by the creator of ASIHTTPRequest.

    There should be plenty of docs in there to get you started.

    EDIT: To make your specific request with ASIHTTPRequest, do the following in a class with the protocol ASIHTTPRequestDelegate:

    /**
     * Make a request to the server.
     */
    - (void) makeRequest {
        // compile your URL string and make the request
        NSString *requestURL = [NSString stringWithFormat:@"%@?u=%@&  p=%@&platform=ios", url, txtUserName.text, txtPassword.text];
    
        NSURL *url = [NSURL URLWithString:requestURL];
        ASIHTTPRequest *request = [ASIHTTPRequest requestWithURL:url];
        [request setDelegate:self];
        [request startAsynchronous];
    }
    
    /**
     * Handle the response from the previous request.
     * @see ASIHTTPRequestDelegate
     */
    - (void) requestFinished:(ASIHTTPRequest*)request {
        NSString *responseString = [request responseString];
        // do whatever you need with the response, i.e. 
        // convert it to a JSON object using the json-framework (https://github.com/stig/json-framework/) 
    }
    
    0 讨论(0)
提交回复
热议问题