如何通过点击按钮将选定的 JSON 数据值从 uitableview 发送到服务器

Posted

技术标签:

【中文标题】如何通过点击按钮将选定的 JSON 数据值从 uitableview 发送到服务器【英文标题】:How to send selected values of JSON Data to server from an uitableview by tapping a button 【发布时间】:2016-10-31 07:12:35 【问题描述】:

我实现了一种将JSON数据加载到UITableView的方法,并传递了多个选定的数组并将其加载到另一个表视图中,该表视图位于同一UITableViewplease refer this to understand my project中。

我想要的是在点击确认按钮时将选定的JSON 数据从 Tableview2 发送到服务器。

我收到以下url格式作为确认的url(由于隐私,只编辑了DNS名称。看看url格式)

http://satests.com/srm/php/AJAXHandler.php?command=   "object": "BusinessEntity",   "method": "SetSQLView",   "params":      "name": "InsertWPLog",     "records": [            "worksitepersonnelid": "35a0c9e1XX-161d-11e6-804c-005056bc570d",       "actiondate": "2016-10-06 13:24:35",       "actiontype": "SIGNIN"        ]   ,   "security":      "userID": 2    

我团队的一位 Java 开发人员向我发送了一个在 java 中发送此 url 的方法,如下所示:

// button on click event
    String url = "http://satests.com/srm/php/AJAXHandler.php?command=\"object\": \"BusinessEntity\",\"method\":\"SetSQLView\",\"params\":\"name\":\"InsertWPLog\",\"records\":[";
               String date = ""; //get system date
               for(String userID : userIds) 
                   url = url + "\"worksitepersonnelid\":\"" + userID + "\",\"actiondate\": \""+date+"\",\"actiontype\": \"SIGNIN\"";
               
               url = url + "],\"security\":\"userID\": 2";

               // send request url

我对此一无所知,但我想将上述 url 格式发送到服务器。

任何人都可以解释上述方法或发布OBjective-C中确认按钮单击事件的示例代码。

【问题讨论】:

请求是 POST 还是 GET? 【参考方案1】:

请看JAVA代码,我在上面写了一些cmets。

    // Build the request URL
    String url = "http://satests.com/srm/php/AJAXHandler.php?command=\"object\": \"BusinessEntity\",\"method\":\"SetSQLView\",\"params\":\"name\":\"InsertWPLog\",\"records\":[";

    // Create the JSON Array for Records
    String date = ""; //get system date
               for(String userID : userIds) 
                   // Create JSON data for each Record and append it to the URL
                   url = url + "\"worksitepersonnelid\":\"" + userID + "\",\"actiondate\": \""+date+"\",\"actiontype\": \"SIGNIN\"";
               
               url = url + "],\"security\":\"userID\": 2";

如果你了解 Objective-C,你可以使用上面的逻辑创建 URL:

// Build the request URL
NSString *url = @"http://satests.com/srm/php/AJAXHandler.php?command=\"object\": \"BusinessEntity\",\"method\":\"SetSQLView\",\"params\":\"name\":\"InsertWPLog\",\"records\":[";

// Create the JSON Array for Records
NSString *date = @""; //get system date
NSArray *userIds = @[]; // get data

for(NSString *userID in userIds) 
    // Create JSON data for each Record and append it to the URL
    url = [NSString stringWithFormat:@"%@ \"worksitepersonnelid\":\"%@\",\"actiondate\": \"%@\",\"actiontype\": \"SIGNIN\"", url, userID, date];

url = [NSString stringWithFormat:@"%@],\"security\":\"userID\": 2",url];
NSLog(@"%@", url);

【讨论】:

如果您可以在objective-c中重新编写代码并在此处发布。在这种情况下它会帮助我。 请等我一下。 非常感谢。等待它。【参考方案2】:

这样的事情应该可以打勾。 您需要根据需要格式化 date 字符串,并将请求更改为 GET(如果不是 POST 请求)。

NSString *urlString = @"http://satests.com/srm/php/AJAXHandler.php?command=\"object\": \"BusinessEntity\",\"method\":\"SetSQLView\",\"params\":\"name\":\"InsertWPLog\",\"records\":[";
NSString *date; //I'm not sure how you want this formatted.

for (NSString *userID in userIds) 
    urlString = [urlString stringByAppendingString:[NSString stringWithFormat:@"\"worksitepersonnelid\":\"%@\",\"actiondate\": \"%@\",\"actiontype\": \"SIGNIN\"", userID, date]];

urlString = [urlString stringByAppendingString:@"],\"security\":\"userID\": 2"];


NSURL *url = [NSURL URLWithString:urlString];

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]initWithURL:url];

[request setHTTPMethod:@"POST"]; //Change to 'GET' if the request is 'GET'.

NSURLSessionDownloadTask *task = [[NSURLSession sharedSession] downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) 
    if (!error) 
        //Sucessful
    else 
        //Error
        NSLog(@"URL Session Error: %@", error);
    
];
[task resume];

【讨论】:

非常感谢我会尝试 “userIds”的定义是什么? 这将与 userIds 在您的 Java 示例中的任何内容相同。

以上是关于如何通过点击按钮将选定的 JSON 数据值从 uitableview 发送到服务器的主要内容,如果未能解决你的问题,请参考以下文章