2012-09-26 141 views
1

我已经在amazon ec2实例中创建了我的服务器。通过我的Android应用程序,我使用HttpUrlConnection连接到服务器。但我得到的响应代码为-1。有没有人有任何想法?这是我的代码。 私人字符串getHttpResponse(最后弦乐srcUrl){HttpUrlConnection响应代码总是返回-1

HttpURLConnection urlConnection = null; 
    FileOutputStream fos = null; 
    try { 
     URL url = new URL(srcUrl); 
     urlConnection = (HttpURLConnection) url.openConnection(); 
     urlConnection.setRequestMethod("GET"); 
     urlConnection.setRequestProperty("Content-Type", "application/json"); 
     mETag = readETagFromPrefForCategory(); 

     urlConnection.setRequestProperty("If-None-Match", mETag); 

     Authenticator.setDefault(new Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication("xyz", "abc".toCharArray()); 
      } 
     }); 

     urlConnection.connect(); 

     mETag = urlConnection.getHeaderField("ETag"); 

     if (urlConnection.getResponseCode() == HttpURLConnection.HTTP_NOT_MODIFIED) { 
      Log.v("http","not modifed"); 
      return readLocalJson(); 
     } 

     if (urlConnection.getResponseCode() != HttpURLConnection.HTTP_OK) { 
      Log.w(TAG, "Bad response [" + urlConnection.getResponseCode() + "] from (" + srcUrl + ")"); 
      return null; 
     }}//end of try block 

回答

0

在这个岗位的答案似乎是解决这个问题的几个人:

Android Https Status code -1

希望有所帮助。

+0

感谢您的回复..我试过System.setProperty(“http.keepAlive”,“false”);但可悲的是它并没有为我工作:(..其他任何帮助表示赞赏.. – user1479604

0

你为什么要设置 “内容类型” 的GET请求?