HTTP POST to Google Form from iOS App

怎甘沉沦 提交于 2019-12-13 17:16:20

问题


I am working on a POST request and have been using this answer. There is a lot of documentation NSUrlRequest (and connection) but I am having trouble figuring out why the request won't work.

  1. I have performed a successful POST using an HTTP Dev Client using this code

    entry.0.single=name&entry.1.single=location&entry.4.single=phoneNumber&entry.2.single=order????&pageNumber=0&backupCache=
    
  2. The 4 variables (name, location, phoneNumber, order) are all linked to textFields in the app.

    - (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];
    
    }
    

I think I might be missing something subtle but I have been pouring through stackoverflow and developer documentation. Any thoughts would be much appreciated. Thanks


回答1:


You would need to implement the NSURLConnectionDelegate protocol, put [nsUrlConnection setDelegate:self]; into your code and add the -connectionDidFinishLoading:, -connection:didReceiveData: and -connectionDidFailWithError: methods into your code and capture the response data:

.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]);
}



回答2:


I did this task by using Swift. Check it out in this repo: https://github.com/goktugyil/QorumLogs

Here is the tutorial of how to set it up: https://github.com/goktugyil/QorumLogs/blob/master/Log%20To%20GoogleDocs.md

Heres the code to do it:

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)
}


来源:https://stackoverflow.com/questions/17748132/http-post-to-google-form-from-ios-app

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!