1

我正在处理一个 POST 请求并且一直在使用这个答案。有很多文档 NSUrlRequest (和连接),但我无法弄清楚为什么请求不起作用。

  1. 我已经使用此代码使用 HTTP 开发客户端执行了成功的 POST

    entry.0.single=name&entry.1.single=location&entry.4.single=phoneNumber&entry.2.single=order????&pageNumber=0&backupCache=
    
  2. 4 个变量(姓名、位置、电话号码、订单)都链接到应用程序中的文本字段。

    - (IBAction)placeOrder:(id)sender {
        NSURL *nsURL = [[NSURL alloc] initWithString:@"url"];
        NSMutableURLRequest *nsMutableURLRequest = [[NSMutableURLRequest alloc] initWithURL:nsURL];
    
        // Set HTTP method to POST
        [nsMutableURLRequest setHTTPMethod:@"POST"];
    
        // Set up the parameters to send.
        NSString *paramDataString = [NSString stringWithFormat:@"%@=%@&%@=%@&%@=%@&%@=%@&pageNumber=0&backupCache=",@"entry.0.single", _name, @"entry.1.single", _location, @"entry.4.single", _phoneNumber, @"entry.2.single", _order];
    
        // Encode the parameters to default for NSMutableURLRequest.
        NSData *paramData = [paramDataString dataUsingEncoding:NSUTF8StringEncoding];
    
        // Set the NSMutableURLRequest body data.
        [nsMutableURLRequest setHTTPBody: paramData];
    
    
        // Create NSURLConnection and start the request.
        NSURLConnection *nsUrlConnection=[[NSURLConnection alloc]initWithRequest:nsMutableURLRequest delegate:self];
    
        [ nsUrlConnection start];
    
    }
    

我想我可能遗漏了一些微妙的东西,但我一直在翻阅 stackoverflow 和开发人员文档。任何想法将不胜感激。谢谢

4

2 回答 2

1

您需要实现NSURLConnectionDelegate协议,放入[nsUrlConnection setDelegate:self];您的代码并将-connectionDidFinishLoading:,-connection:didReceiveData:-connectionDidFailWithError:方法添加到您的代码中并捕获响应数据:

.h
NSMutableData *responseData;

.m
- (void)connection:(NSURLConnection *)connection didReceiveResponse:(NSURLResponse *)response {
    responseData = [[NSMutableData alloc] init];
}

- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
    [responseData appendData:data];
}

- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
    NSLog(@"RESPONSE: %@", [[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding]);
}

- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
    NSLog(@"CONNECTION ERROR: %@", [error localizedDescription]);
}
于 2013-07-19T15:36:55.750 回答
0

我使用 Swift 完成了这项任务。在这个 repo 中查看:https ://github.com/goktugyil/QorumLogs

这是如何设置它的教程: https ://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md

这是执行此操作的代码:

private static func sendError(#text: String) {
    var url = NSURL(string: formURL)
    var postData = formField1 + "=" + text
    postData += "&" + formField2 + "=" + "anothertext"                
    postData += "&" + formField3 + "=" + "anothertext" 
    postData += "&" + formField4 + "=" + "anothertext" 

    var request = NSMutableURLRequest(URL: url!)
    request.HTTPMethod = "POST"
    request.setValue("application/x-www-form-urlencoded; charset=utf-8", forHTTPHeaderField: "Content-Type")
    request.HTTPBody = postData.dataUsingEncoding(NSUTF8StringEncoding)
    var connection = NSURLConnection(request: request, delegate: nil, startImmediately: true)
}
于 2015-09-17T10:23:43.817 回答