Swift-使用Alamofire发布请求

时间:2018-06-27 09:59:10

标签: ios json swift

我在Alamofire上遇到了一些麻烦。我有可以在Android设备上成功运行的服务器功能。一切正常,所有数据正常工作。但是在iOS上,每次发送带有2个以上参数的发布请求时,服务器都会收到错误消息,因此无法发布数据。我试图以自定义编码方式将post作为字符串放置,因为当我将参数放入Alamofire.request方法中时,我也遇到了一个错误,但是服务器响应是“ unexeption handler中的错误”。因此,由于使用了String扩展,我将所有JSON都更改为字符串,然后将每个“ [”和“]”都更改为“ {”,“}”,但是仍然无法将数据保存到服务器。

编码字典并在Swift中更改为JSON

  

电话=%7B%220%22%3A%7B%22call%5Fdate%22%3A%222018%2D06%2D27%2B11%3A30%3A46%22%2C%22number%22%3A%22462462432%22% 2C%22sms%22%3A%220%22%2C%22duration%22%3A%220%22%2C%22type%22%3A211%7D%7D

Swift词典

<cfscript>
  rsData = QueryNew('');
  dcm = $.getBean('dataCollectionManager');
  formContentId = '856499BD-01E2-48C9-CD1A0430D859E81B';
  wddxImageFieldName = 'AVATAR_ATTACHMENT';
</cfscript>

<cfif !Len($.event('responseid'))>
  <!--- All Form Submission Results --->
  <cfscript>
      formBean = $.getBean('content').loadBy(contentID=formContentId);
      if ( !formBean.getIsNew() ) {
          currentFieldList = dcm.getCurrentFieldList(formBean.getContentID());
          data = {
              sortby = 'entered'
              ,sortdirection = 'desc'
              ,keywords = ''
              ,siteid = $.event('siteid')
              ,contentid = formBean.getContentID()
          };
          rsData = dcm.getData(data);
      }
  </cfscript>
  <cfif !rsData.recordcount>
    <h3>Sorry, either the form does not exist, or no records have been submitted yet.</h3>
  <cfelse>
    <table cellspacing="5" cellpadding="5" border="1">
      <!--- FieldNames --->
      <thead>
          <tr>
            <th>&nbsp;</th>
            <th>Date/Time Entered</th>
            <cfloop list="#currentFieldList#" index="fieldName">
              <th>#esapiEncode('html', fieldName)#</th>
            </cfloop>
          </tr>
      </thead>
      <!--- Actual Output --->
      <tbody>
        <cfloop query="rsData">
            <tr>
              <!--- Edit --->
              <td>
                  <a href="./?responseid=#responseid#">Edit</a>
              </td>
              <!--- The Date/Time Stamp --->
              <td>
                  #entered#
              </td>
              <!--- The Data --->
              <!--- Forms are stored as WDDX files ... so we need to unpack them --->
              <cfwddx action="wddx2cfml" input="#data#" output="record" />
              <cfloop list="#currentFieldList#" index="fieldName">
                <td>
                  <cfif StructKeyExists(record, fieldName)>
                    #record[fieldName]#
                    <cfif CompareNoCase(fieldName,wddxImageFieldName) EQ 0>
                      <img src="#$.getURLForImage(record[fieldName])#">
                    </cfif>
                  <cfelse>
                    &nbsp;
                  </cfif>
                </td>
              </cfloop>
            </tr>
        </cfloop>
      </tbody>
    </table>
  </cfif>
</cfif>

另一方面,Android上的同一应用程序可以完美地处理相同的数据。

Java中的JSON编码如下:

  

calls =%7B%220%22%3A%7B%22number%22%3A%22852486258965%22%2C%22duration%22%3A%220%22%2C%22type%22%3A211%2C%22call_date% 22%3A%222018-06-27 + 11%3A53%3A53%22%2C%22sms%22%3A%220%22%7D%7D

Java中的JSON

["calls": ["0": ["call_date": "2018-06-27+11:49:18", "number": "56262621", "sms": "0", "duration": "0", "type": 211]]]

为什么在Android App中一切正常,但在iOS上却根本不工作?有人对此有想法吗?

Swift中的功能

{"calls":{"0":{"number":"852486258965","duration":"0","type":211,"call_date":"2018-06-27 11:53:53","sms":"0"}}}

1 个答案:

答案 0 :(得分:2)

尝试使用带有/不带有编码参数以及添加/删除标头[“ Content-Type”:“ application / json”]的此方法

func request(_ method: HTTPMethod
    , _ URLString: String
    , parameters: [String : AnyObject]? = [:]
    , headers: [String : String]? = [:]
    , onView: UIView?, vc: UIViewController, completion:@escaping (Any?) -> Void
    , failure: @escaping (Error?) -> Void) {


    Alamofire.request(URLString, method: method, parameters: parameters, encoding: JSONEncoding.default, headers: headers)
        .responseJSON { response in

            switch response.result {
            case .success:
                completion(response.result.value!)
            case .failure(let error):
                failure(error)
                guard error.localizedDescription == JSON_COULDNOT_SERIALISED else {
                    AppUtil().showMessage((error.localizedDescription), messageTitle: EMPTY_STRING, buttonTitle: OK, vc: vc)
                    return
                }
                //                    AppUtil.showMessage(SOMETHING_WNET_WRONG, messageTitle: EMPTY_STRING, buttonTitle: OK)
            }
    }
}