2017-09-15 41 views
0

メイク、モデル、カラーオプションなどを取得するために、特定のVINでこのAPIに投稿しようとしています...コードを実行すると、次のデータがXCodeのコンソール:入力メッセージと投稿データを含むJSON投稿

responseString = Optional("  {  \"decoder_messages\" : {  \"service_provider\" : \"DataOne Software, Inc.\",  \"decoder_version\" : \"7.0.1\",  \"decoder_errors\" : [  {   \"error_code\" : \"IQ\",   \"error_message\" : \"Invalid query. Query must be properly formatted JSON.\"   }    ]  },   \"query_responses\" : {   }  }  ") 

私は私のgetJSONInput()関数であるかもしれないエラーを推測しているが、私は同じ結果にさまざまな方法を試してみました。何か助けてくれてありがとう、私はこれをぶち抜いて、あらゆる種類のドキュメントを読んで、サンプルコードで試してみました。私もAlamofireでこれを試しましたが、パラメータとしてpostStringを渡す際の問題がありましたので、私はこのアプローチを試みると思っていました。

@IBAction func postButtonPressed(_ sender: AnyObject) { 
     getJSONInput() 
     postJSON() 
} 


func postJSON(){ 


    var request = URLRequest(url: URL(string: url)!) 
    request.httpMethod = "POST" 
    let postString = "client_id=xxx&authorization_code=xxx&decoder_query=\(JSONInput)" 
    request.httpBody = postString.data(using: .utf8) 
    let task = URLSession.shared.dataTask(with: request) { data, response, error in 
     guard let data = data, error == nil else { 
      print("error=(error)") 
      return 
     } 

     if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 { 
      print("statusCode should be 200, but is \(httpStatus.statusCode)") 
      print("response = \(response)") 

     } 

     let responseString = String(data: data, encoding: .utf8) 
     print("responseString = \(responseString)") 
    } 
    task.resume() 
} 



func getJSONInput() { 
    JSONInput = "{" + 
     "decoder_settings : {" + 
     "display : full," + 
     "version : 7.0.1," + 
     "styles : on," + 
     "style_data_packs : {" + 
     "basic_data : off," + 
     "pricing : off," + 
     "engines : on," + 
     "transmissions : on," + 
     "specifications : on," + 
     "installed_equipment : on," + 
     "optional_equipment : on," + 
     "colors : on," + 
     "safety_equipment : off," + 
     "warranties : off," + 
     "fuel_efficiency : off," + 
     "green_scores : off," + 
     "crash_test : off," + 
     "awards : on" + 
     "}," + 
     "common_data : on," + 
     "common_data_packs : {" + 
     "basic_data : on," + 
     "pricing : on," + 
     "engines : on," + 
     "transmissions : on," + 
     "specifications : on," + 
     "installed_equipment : on," + 
     "optional_equipment : on," + 
     "colors : on," + 
     "safety_equipment : on," + 
     "warranties : on," + 
     "fuel_efficiency : on," + 
     "green_scores : on," + 
     "crash_test : on," + 
     "awards : on" + 
     "}" + 
     "}," + 
     "query_requests : {" + 
     "Request-Sample : {" + 
     "vin : \(myVIN)," + 
     "year : ," + 
     "make : ," + 
     "model : ," + 
     "trim : ," + 
     "model_number : ," + 
     "package_code : ," + 
     "drive_type : ," + 
     "vehicle_type : ," + 
     "body_type : ," + 
     "body_subtype : ," + 
     "doors : ," + 
     "bedlength : ," + 
     "wheelbase : ," + 
     "msrp : ," + 
     "invoice_price : ," + 
     "engine : {" + 
     "description : ," + 
     "block_type : ," + 
     "cylinders : ," + 
     "displacement : ," + 
     "fuel_type :" + 
     "}," + 
     "transmission : {" + 
     "description : ," + 
     "trans_type : ," + 
     "trans_speeds :" + 
     "}," + 
     "optional_equipment_codes : ," + 
     "installed_equipment_descriptions : ," + 
     "interior_color : {" + 
     "description : ," + 
     "color_code :" + 
     "}," + 
     "exterior_color : {" + 
     "description : ," + 
     "color_code :" + 
     "}" + 
     "}" + 
     "}" + 
    "}" 


} 
+0

あなたは 'Dictionary'を使用し、' JSONSerialization'を使用する必要が同じ問題を持っている人のための私の解決策です –

答えて

0

はここ

import UIKit 
import SwiftyJSON 
import Alamofire 

class API { 



static func getData(completion: @escaping (_ datas: JSON?) -> Void){ 


    let url = "https://myapiurl" 

    let query = "{ \"decoder_settings\" : { \"display\" : \"full\", \"version\" : \"7.0.1\", \"styles\" : \"on\", \"style_data_packs\" : { \"basic_data\" : \"on\", \"pricing\" : \"on\", \"engines\" : \"on\", \"transmissions\" : \"on\", \"specifications\" : \"on\", \"installed_equipment\" : \"on\", \"optional_equipment\" : \"off\", \"colors\" : \"on\", \"safety_equipment\" : \"on\", \"warranties\" : \"on\", \"fuel_efficiency\" : \"on\", \"green_scores\" : \"on\", \"crash_test\" : \"on\", \"awards\" : \"on\" }, \"common_data\" : \"on\", \"common_data_packs\" : { \"basic_data\" : \"on\", \"pricing\" : \"on\", \"engines\" : \"on\", \"transmissions\" : \"on\", \"specifications\" : \"on\", \"installed_equipment\" : \"on\", \"optional_equipment\" : \"on\", \"colors\" : \"on\", \"safety_equipment\" : \"on\", \"warranties\" : \"on\", \"fuel_efficiency\" : \"on\", \"green_scores\" : \"on\", \"crash_test\" : \"on\", \"awards\" : \"on\" } }, \"query_requests\" : { \"Request-Sample\" : { \"vin\" : \"myVINwasHERE\", \"year\" : \"\", \"make\" : \"\", \"model\" : \"\", \"trim\" : \"\", \"model_number\" : \"\", \"package_code\" : \"\", \"drive_type\" : \"\", \"vehicle_type\" : \"\", \"body_type\" : \"\", \"body_subtype\" : \"\", \"doors\" : \"\", \"bedlength\" : \"\", \"wheelbase\" : \"\", \"msrp\" : \"\", \"invoice_price\" : \"\", \"engine\" : { \"description\" : \"\", \"block_type\" : \"\", \"cylinders\" : \"\", \"displacement\" : \"\", \"fuel_type\" : \"\" }, \"transmission\" : { \"description\" : \"\", \"trans_type\" : \"\", \"trans_speeds\" : \"\" }, \"optional_equipment_codes\" : \"\", \"installed_equipment_descriptions\" : \"\", \"interior_color\" : { \"description\" : \"\", \"color_code\" : \"\" }, \"exterior_color\" : { \"description\" : \"\", \"color_code\" : \"\" } } } } " 



    let params: [String: Any] = [ 
     "client_id": "xxx", 
     "authorization_code": "xxx123", 
     "decoder_query": query 
    ] 


    Alamofire.request(url, method: .post, parameters: params, encoding: URLEncoding.default, headers: nil).responseString{ 
     response in 

     switch response.result{ 
     case .success(let data): 

      let dic = JSON.parse(data) 
      completion(dic) 

      break; 
     case .failure(let err): 
      print(err) 
      break; 
     } 
    } 
} 

static func convertToDictionary(text: String) -> [String: Any]? { 
    if let data = text.data(using: .utf8) { 
     do { 
      return try JSONSerialization.jsonObject(with: data, options: []) as? [String: Any] 
     } catch { 
      print(error.localizedDescription) 
     } 
    } 
    return nil 
} 
} 
関連する問題