2017-05-09 70 views
0
Alamofire.request(NEWS_FEED_URL).responseJSON { response in 
     guard let newsResponse = response.result.value as? [[String:String]] else{ 
      print("Error") 
      return 
     } 
     print("JSON: \(newsResponse)") 

這是我的alamofire代碼來獲得服務器的響應。大多數時候它工作正常,但有時會失敗並打印錯誤。即使我在故障塊中粘貼打印(「JSON:」,newsResponse),它也會顯示響應,但它不會進入成功塊。我也打印一次狀態碼。它給我200.我的互聯網運行良好,相關的網址在郵差中給予回覆。但有時它不工作爲什麼?Swift 3 Alamofire有時會失敗

+1

當失敗那麼你的反應不是'[[字符串:字符串]]'這裏添加的響應,當它失敗 –

+0

這肯定是在說NiravD。它失敗了,因爲演員不工作。在這種情況下打印回覆,看看應該做什麼。 – Larme

+0

響應如下:[{key:value},{key:value},{key:value}] – Sptibo

回答

0

嘗試此方法

func apiRequest(method:String, urlMethod:String, parametersDictionary:NSMutableDictionary, success:@escaping successDictionaryBlock, failure: @escaping failBlockErrorMessage){ 
      let requestUrl = "Request URL" 


      Alamofire.request(requestUrl, method: .post, parameters: (parametersDictionary as NSDictionary) as? Parameters , encoding: JSONEncoding.default, headers: nil).responseJSON { response in 
       print(response) 
       print(requestUrl) 

       if(response.result.error == nil){ 

        if((response.response?.statusCode)! < 500){ 

         if(response.response?.statusCode == 200){ 

          if let JSON = response.result.value { 

           print(JSON) 
           let dict = JSON as! NSDictionary 
           let status :Bool = dict["status"] as! Bool 

           if(status){ 
            success(dict) 
           }else{ 
            failure(dict["message"] as! String) 
           } 
          } 
         }else{ 
          failure("Something went wrong please try again") 
         } 
        }else{ 
         failure("Something went wrong please try again") 
        } 
       } 
      } 
     }