菜鸟教程小白 发表于 2022-12-11 19:20:14

ios - 无法读取数据,因为它的格式不正确 - HTTP 网络


                                            <p><p>代码</p>

<pre><code>    let session = URLSession.shared

    // prepare json data
    let json: = [&#34;email&#34;: &#34;[email protected]&#34;]

    let jsonData = try? JSONSerialization.data(withJSONObject: json)

    let proceedURL = NSURL(string:&#34;https://mysitename.herokuapp.com/api/users/isUser&#34;)
    //let proceedURL = NSURL(string:&#34;https://google.com&#34;)
    let request = NSMutableURLRequest(url: proceedURL! as URL)
    //HTTP Headers
    request.addValue(&#34;application/json&#34;, forHTTPHeaderField: &#34;Content-Type&#34;)
    request.addValue(&#34;application/www.inception.v1&#34;, forHTTPHeaderField: &#34;Accept&#34;)
    request.addValue(&#34;Authorization&#34;, forHTTPHeaderField: &#34;Basic aW5jZXB0aW9uQGZ1cmRvOmljM=&#34;)
    request.httpMethod = &#34;POST&#34;
    //request.httpBody = jsonData


    // insert json data to the request
    request.httpBody = jsonData

    //create dataTask using the session object to send data to the server
    let task = session.dataTask(with: request as URLRequest, completionHandler: { data, response, error in

      guard error == nil else {
            return
      }

      guard let data = data else {
            return
      }

      // Print out response string
      let responseString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
      print(&#34;responseString = \(responseString!)&#34;)

      do {
            //create json object from data
            if let json = try JSONSerialization.jsonObject(with: data, options: .mutableContainers) as? {
                print(json)
                // handle json...
            }

      } catch let error {
            print(&#34;error : &#34; + error.localizedDescription)
      }

    })

    task.resume()
</code></pre>

<p>错误:
无法读取数据,因为它的格式不正确。</p>

<p>我是 iphone 应用程序开发的初学者,请帮助我并为建立网络连接提供更好的建议(比如在 android 中我正在使用 Volley 库)</p>

<p>实际 react 是:</p>

<pre><code>{
&#34;status&#34;: 1,
&#34;http_status_code&#34;: 200,
&#34;data&#34;: {
    &#34;email&#34;: &#34;[email protected]&#34;,
    &#34;phone&#34;: &#34;8090909000&#34;
}
}
</code></pre>

<p>我在 Android 上使用相同并在 postman 中进行测试。</p>

<pre><code>// Print out response string
      let responseString = NSString(data: data, encoding: String.Encoding.utf8.rawValue)
</code></pre>

<p>上面的代码响应什么都没有</p></p>
                                    <br><hr><h1><strong>Best Answer-推荐答案</ strong></h1><br>
                                            <p><p>使用 Alamofire。 </p>

<pre><code>let json: = [&#34;email&#34;: &#34;[email protected]&#34;]

Alamofire.request(.POST, &#34;https://mysitename.herokuapp.com/api/users/isUser&#34; , parameters: json, encoding:.JSON).responseJSON {
            Response in
            switch Response.result {
            case .Success(let _data):
                let JsonData = JSON(_data)
                print(&#34;JsonData : \(JsonData)&#34;)


         //handle json


case .Failure(let _error):
                print(_error)
                let AlertBox = UIAlertController(title: &#34;Connection Failed&#34;, message: &#34;No Connection&#34;, preferredStyle: .Alert)
                let ActionBox = UIAlertAction(title: &#34;Ok&#34; , style: .Default, handler: { _ in})
                AlertBox.addAction(ActionBox)
                self.presentViewController(AlertBox, animated: true, completion: nil)
            }
</code></pre></p>
                                   
                                                <p style="font-size: 20px;">关于ios - 无法读取数据,因为它的格式不正确 - HTTP 网络,我们在Stack Overflow上找到一个类似的问题:
                                                        <a href="https://stackoverflow.com/questions/42997965/" rel="noreferrer noopener nofollow" style="color: red;">
                                                                https://stackoverflow.com/questions/42997965/
                                                        </a>
                                                </p>
                                       
页: [1]
查看完整版本: ios - 无法读取数据,因为它的格式不正确 - HTTP 网络