从iOS App向Google表单发送HTTP POST

时间:2013-07-19 14:05:34

标签: ios objective-c post google-form

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

  1. 我使用此代码

    使用HTTP Dev Client成功执行了POST
    entry.0.single=name&entry.1.single=location&entry.4.single=phoneNumber&entry.2.single=order????&pageNumber=0&backupCache=
    
  2. 4个变量(名称,位置,电话号码,订单)都与应用中的textFields相关联。

    - (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];
    
    }
    
  3. 我想我可能会遗漏一些微妙但我一直在倾注stackoverflow和开发人员文档。任何想法将不胜感激。感谢

2 个答案:

答案 0 :(得分: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]);
}

答案 1 :(得分:0)

我使用Swift完成了这项任务。请在此回购中查看: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)
}