在Swift中发送HTTP请求并获得1001,999错误

时间:2018-03-02 11:12:48

标签: swift xcode http info.plist

我获得了以下代码行,旨在将值发送到Java中的IP。根据我的理解,它似乎是一种GET方法。但是,没有发送参数。随意纠正我,因为我在Android编程中完全是文盲。

private class Background_get extends AsyncTask<String, Void, String> {
  @Override
  protected String doInBackground(String... params) {
    try {
      /* Change the IP to the IP you set in the arduino sketch */
      URL url = new URL("http://192.168.43.142/?" + params[0]);
      HttpURLConnection connection = (HttpURLConnection) url.openConnection();
      connection.setConnectTimeout(10000);
      BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
      StringBuilder result = new StringBuilder();
      String inputLine;
      while ((inputLine = in.readLine()) != null)
        result.append(inputLine).append("\n");
      in.close();
      connection.disconnect();
      return result.toString();
    } catch (IOException e) {
      e.printStackTrace();
    }
    return null;
  }
}

接收者是arduino,它处理发送数据的方式是检查并比较最后一个字符。有关进一步说明,请参阅以下内容:

  char c = client.read();
        Serial.write(c);
        buffered +=c;
        if(buffered.endsWith("O")){ // this is for TV
           //digitalWrite(pin2,HIGH);
           irsend.sendPanasonic(0x4004,0x100BCBD);    
           delay(100);
         } else if (buffered.endsWith("X")) {
          irsend.sendPanasonic(0x4004,0x1004C4D); 
         } else if (buffered.endsWith("P")) {
          irsend.sendPanasonic(0x4004,0x1000405); 
         } else if (buffered.endsWith("M")) {
          irsend.sendPanasonic(0x4004,0x1008485); 
         } else if (buffered.endsWith("1")) 

我目前在这里做的是首先为HTTP通信设置info.plist,如下所示:

<key>NSAppTransportSecurity</key>
    <dict>
        <key>NSAllowsArbitraryLoads</key>
        <false/>
        <key>NSExceptionDomains</key>
        <dict>
            <key>192.168.43.142</key> 
            <dict>
                <key>NSIncludesSubdomains</key>
                <true/>
                <key>NSTemporaryExceptionAllowsInsecureHTTPLoads</key>
                <true/>
                <key>NSTemporaryExceptionMinimumTLSVersion</key>
                <string>TLSv1.1</string>
            </dict>
        </dict>
    </dict>

在这里我定义了我的IP地址:

enum API : String {
    case url = "http://192.168.43.142/?"

}

我用以下方式使用它:

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
         var httpRequest = API.url.rawValue
        let clicked_btn = indexPath.row
        switch clicked_btn {

        case 0 :

            httpRequest = "\(httpRequest)CP"


        case 1 :
           httpRequest = "\(httpRequest)1"


        case 2 :
            httpRequest = "\(httpRequest)2"

        case 3 :
            httpRequest = "\(httpRequest)3"

        case 4 :
            httpRequest = "\(httpRequest)CM"

        case 5:
           httpRequest = "\(httpRequest)4"

        case 6 :
            httpRequest = "\(httpRequest)5"

        case 7 :
            httpRequest = "\(httpRequest)6"
        case 8 :
            httpRequest = "\(httpRequest)P"

        case 9 :
            httpRequest = "\(httpRequest)7"

        case 10 :
            httpRequest = "\(httpRequest)8"

        case 11:
            httpRequest = "\(httpRequest)9"

        case 12 :
            httpRequest = "\(httpRequest)M"

        case 13 :
            httpRequest = "\(httpRequest)X"
        case 14 :
            httpRequest = "\(httpRequest)0"

        case 15 :
            httpRequest = "\(httpRequest)O"


        default :
            print("deaf")
            break
    }
        //send the HTTP Request :
        print( "my http request is ====> \(httpRequest)")

        let url = URL(string: "\(httpRequest)")!
        let request = URLRequest(url: url)

        let task = URLSession.shared.dataTask(with: url) { (data, response, error) in
            //do stuff with data, response, error
            print("Response : \(response)")


        }

        task.resume()

}

这里的问题是我在控制台中不断收到以下错误。我不知道我在哪里弄错了。我检查了大多数其他类似的问题,但无法找到任何答案。

 my http request is ====> http://192.168.43.142/?O
    2018-03-02 19:06:43.749016+0800 Master_Project[1687:584006] Task <8BCA1C8B-6571-491D-85D7-97BE6633E720>.<1> finished with error - code: -1001
    2018-03-02 19:06:43.766883+0800 Master_Project[1687:584006] Task <8BCA1C8B-6571-491D-85D7-97BE6633E720>.<1> HTTP load failed (error code: -999 [1:89])
    Response : nil

我非常感谢您的帮助。值得一提的是,我在模拟器和真实设备上都尝试了这一点,他们最终都给了我同样的错误{{3}}

1 个答案:

答案 0 :(得分:1)

错误代码-1001表示您的连接超时。看起来您的设置无法连接到服务器。出于测试目的,您可以简化NSAppTransportSecurity设置,只需将NSAllowsArbitraryLoads设置为true;如果连接以这种方式工作,您就知道它与配置有关。

Here列出了一些与网络相关的错误代码。