0

我有这个链接:

函数 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"];
                           }                               
                       }];        
        }   
}

我该如何处理?

提前致谢 。

4

2 回答 2

0

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

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

+ (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;

}
于 2013-06-11T08:51:56.163 回答
0

请像这样更改您的代码

-(无效)新消息{

    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。

请参阅此链接:如何将 JSON 字符串反序列化为 NSDictionary?(适用于 iOS 5+)

于 2013-06-11T09:43:29.313 回答