0

我一直在研究一种将值发送到目标 c 中的网络服务器的方法。

我认为我在两端都可以正常工作,除了我的价值没有出现在发送的电子邮件中。

这是我的代码,如果有人能指出我做错了什么,那就太好了。

(dval 是什么没有出现)

NSMutableString *mmessage = [[NSMutableString alloc]initWithString:@""];
    [mmessage appendString:[NSString stringWithFormat:@"<p><b>Drink Name:</b> %@</p>", dval]];


    NSMutableURLRequest *urlRequest = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:@"http://test.com/api/stuff/send"]];
    NSMutableDictionary *postInfo = [NSDictionary dictionaryWithObject:[NSDictionary dictionaryWithObjectsAndKeys:mmessage, @"message", nil] forKey:@"form"];

    NSError *e = nil;
    NSData *postData = [NSJSONSerialization dataWithJSONObject:postInfo options:0 error:&e ];

    __block int resp_code = 0;

    [urlRequest setValue:@"application/json" forHTTPHeaderField:@"Content-Type"];
    [urlRequest setHTTPMethod:@"POST"];
    [urlRequest setHTTPBody:postData];

    [NSURLConnection sendAsynchronousRequest:urlRequest
                 queue:[NSOperationQueue mainQueue]
                 completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                     NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse*)response;
                     resp_code = httpResponse.statusCode;
                     if(resp_code == 200){

                         NSDictionary *jsonObjects = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

                         if ([[jsonObjects objectForKey:@"status"] isEqualToString:@"success"]) {
                             UIAlertView *emailsuccess = [[UIAlertView alloc] initWithTitle:@"Email Successfully sent." message:nil delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                             [emailsuccess show];
                         }

                         else if ([[jsonObjects objectForKey:@"status"] isEqualToString:@"failed"]) {
                             UIAlertView *emailfailed = [[UIAlertView alloc] initWithTitle:@"Email Failed." message:[jsonObjects objectForKey:@"messages"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                             [emailfailed show];
                         }

                        else {
                             UIAlertView *emailfailed = [[UIAlertView alloc] initWithTitle:@"Email Response." message:[jsonObjects objectForKey:@"messages"] delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil];
                             [emailfailed show];

                     }

                         }
                     }];
4

0 回答 0