2012-09-26 189 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請求?