将 NSMutableArray 或 NSDictionary 发送到 php 到 mysql [英] Sending NSMutableArray OR NSDictionary to php to mysql

查看:52
本文介绍了将 NSMutableArray 或 NSDictionary 发送到 php 到 mysql的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我做了一个活动来从用户那里获取输入数字,输入数据的文本字段数量每次都不同,它是一个 tableView,每个单元格都有一个文本字段,我设法将所有数字存储在一个NSMutableArray,我一直在研究如何将它传递给我的 php,我似乎找不到可以帮助我的东西,并且它不能使用 " 作为 NSString 发送StringWithFormat".

In my application, i made an activity to get input numbers from the user, the number of textfields for input data is different each time, it's a tableView and each cell has a textfield, i managed to store all the numbers in a NSMutableArray, and I've been looking into how to pass it to my php, I can't seem to find something to help me, and it can't be send as NSString using "StringWithFormat".

是否应该将 NSMutableArray 改成 NSDictionary ?我如何将它发送到我的 php 代码?

Should NSMutableArray Be changed into NSDictionary ? How Do i Send it to my php code ?

我正在使用 AFNetworking 来管理我的所有连接,这很棒,只是在发送数组时被卡住了.

I am using AFNetworking to manage all my connections and it's great, just got stuck on sending an array.

我的 NSMutableArray 中的每个对象都包含

each object in my NSMutableArray consists of

StudentName, StudentMark, StudentID 

任何我可以学习的建议或好的教程?

any suggestions or good tutorials i can learn from ?

推荐答案

AFNetworking,默认情况下,使用 AFHTTPRequestSerializer(创建 application/x-www-form-urlencoded> 请求).这不太适合发送值数组(尤其是字典数组).

AFNetworking, by default, uses the AFHTTPRequestSerializer (which creates application/x-www-form-urlencoded requests). This is not well suited for sending an array of values (esp an array of dictionaries).

假设您有一组 students,您有几个选择:

You have a couple of options, assuming you have an array of students:

  1. 手动使用 NSJSONSerialization 将字典数组编码为字符串,然后将其作为参数传递给字典:

  1. Manually use NSJSONSerialization to encode the array of dictionaries into a string, and then pass that as a parameter in a dictionary:

NSError *error;
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:students options:0 error:&error];
NSAssert(jsonData, @"Problem encoding JSON: %@", error);
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

[manager POST:urlString parameters:@{@"students": jsonString} success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error);
}];

如果你这样做,你的服务器代码会抓取$_POST['students'],然后json_decode那个.

If you do that, your server code would grab $_POST['students'], and then json_decode that.

更改网络服务以接受 JSON 请求(即 application/json 请求而不是 application/x-www-form-urlencoded 请求).

Change the web service to accept JSON requests (i.e. an application/json request rather than an application/x-www-form-urlencoded requests).

manager.requestSerializer = [AFJSONRequestSerializer serializer];

[manager POST:urlString parameters:students success:^(AFHTTPRequestOperation *operation, id responseObject) {
    NSLog(@"%@", responseObject);
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    NSLog(@"%@", error);
}];

但是,如果您这样做,则需要对 Web 服务进行更根本的更改以接受 JSON 请求(例如,您需要手动获取输入并进行 JSON 解码,而不是使用 $_POST它:

If you do that, though, it requires a more fundamental change in your web service to accept JSON requests (e.g., rather than using $_POST, you'd manually grab the input and JSON decode it:

<?php

    // read the raw post data

    $handle = fopen("php://input", "rb");
    $raw_post_data = '';
    while (!feof($handle)) {
        $raw_post_data .= fread($handle, 8192);
    }
    fclose($handle);

    // decode the JSON into an associative array

    $request = json_decode($raw_post_data, true);

    // you can now access the associative array, $request,
    // e.g. save it to your database

    // let's assume you built a `$response` dictionary summarizing 
    // the success or failure of the above code, now let's output it:  

    $raw_response = json_encode($response);

    // specify headers

    header("Content-Type: application/json");
    header("Content-Length: " . strlen($raw_response));

    // output response

    echo $raw_response;
?>

第一个选项可能最简单,但缺乏一定的优雅(在 x-www-form-urlencoded 请求中嵌入 JSON).后一个选项(更改 Web 服务以接受 JSON 请求)需要做更多的工作(例如,一旦您有一个使用 JSON 的 Web 服务请求,您可能希望整个 Web 服务一致地使用 JSON).

The first option is probably easiest, but lacks a certain elegance (embedding JSON within a x-www-form-urlencoded request). The latter option (changing web service to accept JSON requests) would require a little more work, though (e.g. once you have one web service request using JSON, you might want the whole web service to use JSON consistently).

这篇关于将 NSMutableArray 或 NSDictionary 发送到 php 到 mysql的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆