Sending Request and Get Response

前端 未结 3 606
广开言路
广开言路 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

    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.

提交回复
热议问题