【问题标题】:how to call webservice in xcode by GET Method?如何通过GET方法在xcode中调用webservice?
【发布时间】:2013-06-06 23:34:46
【问题描述】:

我有这个链接:

函数new_message($chat_id,$user_id,$message,$recipient_ids)

http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/2%2C7

返回 chat_log_id

谁能解释一下如何通过这个get方法调用webserive或者给我

解决方案。

我对我的代码所做的如下:

-(void)newMessage{

if ([self connectedToWiFi]){                
    NSString *urlString = [NSString stringWithFormat:@"www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/1,1,2"];

    NSLog(@"urlString is %@", urlString);

    NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

    NSURL *requestURL = [NSURL URLWithString:urlString];

    [request setURL:requestURL];
    [request setHTTPMethod:@"POST"];


    [NSURLConnection sendAsynchronousRequest:request                                  queue:[NSOperationQueue mainQueue]

                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {

                           NSLog(@"ERROR = %@",error.localizedDescription);

                           if(error.localizedDescription == NULL)
                           {

                               NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               NSLog(@"response >>>>>>>>> succ %@",returnString);
                               [delegate ConnectionDidFinishLoading:returnString : @"newMessage"];
                           }
                           else
                           {
                               NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                               NSLog(@"response >>>>>>>>> fail %@",returnString);
                               [delegate ConnectiondidFailWithError:returnString : @"newMessage"];
                           }                               
                       }];        
        }   
}

我该如何处理?

提前致谢。

【问题讨论】:

    标签: iphone objective-c web-services getmethod


    【解决方案1】:

    请像这样更改您的代码

    -(void)newMessage{

        NSString *urlString = [NSString stringWithFormat:@"http://www.demii.com/demo/dooponz/admin/index.php/chat/new_message/4/1/you/27" ];
    
        NSLog(@"urlString is %@", urlString);
    
        NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];
    
        NSURL *requestURL = [NSURL URLWithString:urlString];
    
        [request setURL:requestURL];
        [request setHTTPMethod:@"POST"];
    
    
        [NSURLConnection sendAsynchronousRequest:request                                  queue:[NSOperationQueue mainQueue]
    
                               completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
    
                                   NSLog(@"ERROR = %@",error.localizedDescription);
    
                                   if(error.localizedDescription == NULL)
                                   {
    
                                       NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                       NSLog(@"response >>>>>>>>> succ %@",returnString);
                                       [self parseStringtoJSON:data];
                                       //[delegate ConnectionDidFinishLoading:returnString : @"newMessage"];
                                   }
                                   else
                                   {
                                       NSString *returnString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
                                       NSLog(@"response >>>>>>>>> fail %@",returnString);
                                      // [delegate ConnectiondidFailWithError:returnString : @"newMessage"];
                                   }                               
                               }];        
    }
    

    -(void)parseStringtoJSON:(NSData *)data{

    NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:nil];
    NSLog(@"chat id %@",[dict objectForKey:@"chat_log_id"]);
    

    }

    如果您点击该网址,您将获得 JSON 响应字符串作为结果。如果你熟悉json解析,你可以根据key获取value。

    查看此链接:How do I deserialize a JSON string into an NSDictionary? (For iOS 5+)

    【讨论】:

      【解决方案2】:

      从您的帖子中我不确定您是要“发布”还是“获取”。但是,根据您将方法设置为发布以及您正在服务器上创建新内容这一事实来衡量,我假设您想要发布。

      如果你想发帖,你可以使用我的包装方法来发帖请求。

      + (NSData *) myPostRequest: (NSString *) requestString withURL: (NSURL *) url{
      
      NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
      
      [request setHTTPMethod:@"POST"];
      [request setTimeoutInterval:15.0];
      
      NSData *requestBody = [requestString dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
      
      [request setHTTPBody:requestBody];
      
      NSURLResponse *response = NULL;
      NSError *requestError = NULL;
      NSData *responseData = [NSURLConnection sendSynchronousRequest:request returningResponse:&response error:&requestError];
      
      return responseData;
      
      }
      

      请求字符串的格式如下:

       NSString * requestString = [[NSString alloc] initWithFormat:@"username=%@&password=%@", userInfo[@"username"], userInfo[@"password"]];
      

      这也将返回响应数据,您可以将其转换为这样的字符串。

      responseString = [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
      

      如果你试图从服务器获取 json 格式的数据...

      + (NSArray *) myGetRequest: (NSURL *) url{
      
      NSArray *json = [[NSArray alloc] init];
      
      NSData* data = [NSData dataWithContentsOfURL:
                      url];
      NSError *error;
      
      if (data)
          json = [[NSArray alloc] initWithArray:[NSJSONSerialization
                                                 JSONObjectWithData:data
                                                 options:kNilOptions
                                                 error:&error]];
      
      //NSLog(@"get results: \n %@", json);
      
      return json;
      
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-10-27
        • 2014-10-14
        • 2018-02-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多