使用POST请求发送嵌套JSON iOS Swift 3

时间:2017-07-10 07:04:36

标签: ios json post swift3 xcode8

我想用POST请求将JSON发送到服务器,但我不明白我是如何做到这一点的。我从表格视图中选择朋友,并在我的收藏夹视图中显示这些朋友。所选人员在集合视图中显示,其电子邮件以JSON格式发送到创建组。这是我用Swift编写的代码。

@IBAction func createGroupButton(_ sender: Any) {

    let groupName = groupNameTextField.text

    let adminEmail = UserDefaults.standard.value(forKey: "userEmail")


    if groupName == "" {

    alertMessage(msg: "Enter Group name")

    }

    else if base64String == nil {

        alertMessage(msg: "Select Group Image")

    }

    else if emailArray.isEmpty {

        alertMessage(msg: "Select atleast one person")

    }


    else {


    if isNetAvailable {

        var emailData: String?

        print("email data \(emailArray)")

        for i in emailArray {


         emailData = "\"email\":\"\(i)\""

        }

    let postData = "\"name\":\"\(groupName!)\",\"adminemail\":\"\(adminEmail!)\",\"image\":\"\(base64String!)\",\"emailArray\":\"\(emailData!)\""


    let postDat = "jsondata={\(postData)}"

    print("Post Data \(postDat)")


    let urlString = create_group

    let url = URL(string: urlString)


    var request = URLRequest(url: url!)

    request.httpMethod = "POST"

    request.httpBody = postDat.data(using: String.Encoding.utf8)

    request.addValue("application/json", forHTTPHeaderField: "Content-Type")
    request.addValue("application/json", forHTTPHeaderField: "Accept")

    let session = URLSession.shared

    let task = session.dataTask(with: request) { (data, response, error) in

        if error != nil {

        print("Something wrong with creating Group")

        }

        if data == nil {

            print("Nil Data")

        }

        if response == nil {
            print("Nil Response")

        }

        if response != nil {
            let responseString = NSString(data: data!, encoding: String.Encoding.utf8.rawValue)

            print("Response String is \(responseString)")
        }
    }
    task.resume()

    }

    else {
        alertMessage(msg: "Internet Not Available")
     }

    }        
}

这是我的API

<?php

if($_SERVER["REQUEST_METHOD"] !="POST"){ exit; }
require_once 'dbcon.php';
if (!empty($_POST['jsondata']))
{
$configjson = $_POST['jsondata'];

$config = json_decode($configjson, true);

$groupname = $config['name'];
$adminemail = $config['adminemail'];
$image = $config['image'];

$queryemailcheck = "SELECT * FROM groups_name WHERE admin =   '$adminemail' AND groupname = '$groupname'";
$selectquery = mysqli_query($connect, $queryemailcheck);
$totalemails= mysqli_num_rows($selectquery);
if($totalemails>0)
{
echo "Already exist";
}
else {
$queryinsert= "INSERT INTO groups_name(admin , groupname , pic ) VALUES('$adminemail' ,'$groupname' , '$image')";

if(mysqli_query($connect, $queryinsert)){
    echo "Successfully Saved";
}else{
    echo "Error: " ;
}
$members = $config['emailArray'];
foreach($members as $row ){
$email = $row['email'];

$queryinsert2= "INSERT INTO group_members(groupname , member , status    ) VALUES('$groupname' ,'$email' , '0')";

if(mysqli_query($connect, $queryinsert2)){
    echo "Successfully Saved";
}else{
    echo "Error: " ;
}
}

}

}

else echo "post data is empty";
?>

2 个答案:

答案 0 :(得分:0)

您可以使用Alamofire库来处理您的网络服务。喜欢

Alamofire.request(logoutUrl, method: .post, parameters: dict, encoding: JSONEncoding.default, headers:["Authorization":"Bearer \(accessToken)", "Accept":"application/json", "Content-Type":"application/json; charset=UTF-8"]).responseJSON { (response) in

            let json:JSON = JSON(response.result.value)

            let status =  json["status"].stringValue
            let statusCode = response.response?.statusCode

答案 1 :(得分:0)

创建网络处理程序类,您可以从所有应用程序中点击Web服务。

func makeHttpPostRequest(uri:String, postBody:Dictionary<String, Any>  ,Completion:@escaping (_ response: AnyObject, _ success: Bool) -> Void) {
    let networkCheck = self.getNetworkStatus()
    if networkCheck == false {
        Completion("NO internet connection." as AnyObject,false)
        return
    }//end network check block
    var urlRequest = self.makeHttpRequestWithUrl(url: uri)
    urlRequest.httpMethod = "POST"
    urlRequest.setValue("application/json; charset=utf-8", forHTTPHeaderField: "Content-Type")
    urlRequest.setValue("application/json", forHTTPHeaderField: "Accept")
    //check and set authorization header
    if (UserDefaults.standard.object(forKey: AppConstant.KAppAuthenticationToken) != nil) {
        let authToken = UserDefaults.standard.object(forKey: AppConstant.KAppAuthenticationToken) as! String
        urlRequest.setValue("Token token="+authToken, forHTTPHeaderField: "Authorization")
    }
    //let postData = [self.addDefultParameters(detailDict: postBody)]
    let jsonData = try? JSONSerialization.data(withJSONObject: postBody)
    urlRequest.httpBody = jsonData

    //create connection
    self.httpConnectionWithRequest(request: urlRequest, Completion: Completion)
}

func makeHttpRequestWithUrl(url: String) -> URLRequest {
    let urlString = AppConstant.baseUrl+url
    let networkUrlRequest = URLRequest.init(url: URL.init(string: urlString)!, cachePolicy: .reloadIgnoringCacheData, timeoutInterval: 50)
    return networkUrlRequest
}

func httpConnectionWithRequest(request: URLRequest, Completion:@escaping (_ response: AnyObject, _ success: Bool) -> Void) {
    let session = URLSession.shared
    // make the request
    let task = session.dataTask(with: request) {
        (data, response, error) in
        // check for any errors
        guard error == nil else {
            print("error is: " + String.init(format: "%@", error?.localizedDescription as! CVarArg))
            Completion(error!.localizedDescription as String as AnyObject, false)
            return
        }
        // make sure we got data
        guard let responseData = data else {
            print("Error: did not receive data")
            Completion("didn't get response data" as String as AnyObject, false)
            return
        }

        let responseString =  String.init(data: responseData, encoding: .utf8)
        // parse the result as JSON, since that's what the API provides

        if let resp = response as? HTTPURLResponse{

            if (UserDefaults.standard.object(forKey: AppConstant.KAppAuthenticationToken) != nil) {
                if resp.statusCode == 401{
                    //auth token expired

                    self.makeRequestWithNewToken(request: request, Completion: Completion)
                }
            }
            //check if response is Array or dictionary
            var jsonArray:[Any]?
            var jsonDict:[String:Any]?


            // response is in array
            do{
                jsonArray = try JSONSerialization.jsonObject(with: responseData, options: [])as? [Any]
            }catch{
                print("response doesn't contain array")
            }
            //response is in dictionary
            do{
                jsonDict  = try JSONSerialization.jsonObject(with: responseData, options: [])as? [String:Any]
            }catch{
                print("response doesn't contain dict")
            }
            //=====
          //  if resp.statusCode == 200{

                if jsonArray != nil{
                    Completion(jsonArray! as AnyObject, true)
                    return
                }

                if jsonDict != nil{
                    Completion(jsonDict! as AnyObject, true)
                    return
                }

                Completion("didn't get response data" as String as AnyObject, false)
                return
        }

    }
    task.resume()
        }

通过完成处理程序,您将获得获取的数据。