URLSession从HTTP GET请求返回空数组

时间:2016-08-16 20:18:09

标签: ios json swift nsurlsession

我正在发出一个HTTP GET请求并随机返回一个空数组,每个请求返回一个200 OK的状态代码,无论我是否得到正确的响应。

我已经检查了Chrome中的网址并且它们是正确的,我无法在iOS之外重现此错误。我目前正在做的是递归调用此方法,直到它给我回复正确的响应。

有时它会给我一个填充数组的正确响应,有时它并没有给我一个空数组,即使是同一个数组。

library(shiny)
library(shinydashboard)
#library(openxlsx)
rm(ui)
rm(server)


ui <- shinyUI(dashboardPage(
  dashboardHeader(title = "Sentiment Analysis"),
  dashboardSidebar(
    sidebarMenu(
      menuItem("Dashboard", tabName = "dashboard", icon = icon("dashboard")),
      menuItem("Tweets", icon = icon("twitter"),
               menuSubItem("Positive Tweets", tabName = "pTweets", icon = icon("thumbs-up")),
               menuSubItem("Neutral Tweets", tabName = "neuTweets", icon = icon("hand-spock-o")),
               menuSubItem("Negative Tweets", tabName = "negTweets", icon = icon("thumbs-down"))
      ),
      menuItemOutput("out1") # added
    )
  ),
  ## Body content
  dashboardBody(

    tabItems(
      # First tab content
      tabItem(tabName = "dashboard",
              div(class = "my-class", h2("Sentiment Analysis of Twitter Tweets using RapidMinor and Shiny Dashboard.")),
              fluidRow(
                #valueBox(count, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
                valueBox(15, "Total Number of Tweets Analyzed in the competition", icon = icon("twitter"), width = 6),
                #valueBox(countDays, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
                valueBox(10, "Number of Days ", icon = icon("calendar-check-o"), width = 6, color = "yellow")
              ),
              fluidRow(
                #infoBox("Positive", paste(positivePercent, "%"), icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
                infoBox("Positive", "80%", icon = icon("thumbs-up"), width = 4, fill = TRUE, color = "green"),
                infoBox("Neutral", "15%", icon = icon("hand-spock-o"), width = 4, fill = TRUE, color = "light-blue"),
                infoBox("Negative", "5%", icon = icon("thumbs-down"), width = 4, fill = TRUE, color = "red")
              )
      ),

      # Positive Tweets tab content
      tabItem(tabName = "pTweets",
              h2("Positive Tweets #Brexit"),
              #h4(positiveTweets)
              h4("Great")
      ),
      # Neutral Tweets tab content
      tabItem(tabName = "neuTweets",
              h2("Neutral Tweets #Brexit"),
              #h4(neutralTweets)
              h4("ok")
      ),
      # Negative Tweets tab content
      tabItem(tabName = "negTweets",
              h2("Negative Tweets #Brexit"),
              #h4(negativeTweets)
              h4("shit :D")
      )
    )
  )
))

server <- function(input, output) {

  #my_files will be updated each time you run the app
  #my_files <- list.files() 

  # for testing purposes generate 5 tabs with names given by random letters
  my_files <- letters[sample(1:26, 5)] 

  # There could also be the case when there is no files in a folder
  # You can handle it with `req` or `validate(need(...))` functions
  #my_files <- ""

  output$out1 <- renderUI({
    # Just in case if you would put new files to the folder
    # while the app is working and wanted an update of tabs:
    #   - create eventReactive with an actionButton which will
    #     return list.files().
    #   - pass new names of files to this renderUi function.

    # be careful because "tabName" must not have a "." in it.

    req(my_files) # show tabs only if there are files in a directory

    # generate and save tabs in a list
    tabs <- lapply(seq_along(my_files), function(i) {
      menuSubItem(my_files[i], tabName = my_files[i], icon = icon("thumbs-up"))
    })

    menuItem("Files", tabName = "Files", icon = NULL, tabs)
  })
}
shinyApp(ui, server)

传递给parseStopEstimateJson()的数据有时是两个字节。 func getStopEstimation(routeId: String?, stopId: String?, completion: (result: String) -> Void) { let components: NSURLComponents = NSURLComponents(string: baseUrl + "GetMapStopEstimates")! var queryItems: [URLQueryItem] = [] let apiKeyQueryItem: URLQueryItem = URLQueryItem(name: "apikey", value: apiKey) queryItems.append(apiKeyQueryItem) if routeId != nil { let routeIdQueryItem: URLQueryItem = URLQueryItem(name: "routeID", value: routeId) queryItems.append(routeIdQueryItem) } if stopId != nil { let stopIdQueryItem: URLQueryItem = URLQueryItem(name: "routeStopID", value: stopId) queryItems.append(stopIdQueryItem) } components.queryItems = queryItems let url: URL = components.url! var request: URLRequest = URLRequest(url: url) request.httpMethod = "Get" let session: URLSession = URLSession.shared session.dataTask(with: request) { (data: Data?, response: URLResponse?, error: NSError?) in if ((error) != nil) { completion(result: "No vehciles on route") } completion(result: self.parseStopEstimateJson(data: data!, routeId: routeId!, stopId: stopId!)) }.resume() }

有谁知道为什么我的电话会导致不一致的回复?

1 个答案:

答案 0 :(得分:1)

如果数据以任何方式被截断(这是唯一可能在客户端出错的东西),它将无法解析并且您将获得nil或异常(不确定是哪个)。

因为你得到一个空数组,这意味着服务器正在发送一个空数组,问题出在服务器端代码中,而不是客户端代码。

N.B。 “GET”应该是全部大写。它也是默认设置,因此您根本不需要设置它。但这可能与失败无关。