2016-09-30 27 views
0

我正在開發一個通過HTTP調用RESTful服務器的Android應用程序。問題是,我收到以下錯誤:NegativeArraySizeException -1。我無法弄清爲什麼問題在發生。NegativeArraySizeException -1

我有兩個類:ReadHttpTaskHttpHelper。該錯誤似乎是由HttpHelper類中的StringBuilder引起的,或者至少是調試器顯示異常原因的地方。

ReadHttpTask:

public class ReadHttpTask extends AsyncTask<String, Void, CharSequence> { 
    @Override 
    protected CharSequence doInBackground(String...urls) { 
     String urlString = urls[0]; 
     try{ 
      CharSequence result = HttpHelper.GetHttpResponse(urlString); 
      return result; 
     } catch (IOException ex){ 
      cancel(true); 
      String errorMessage = ex.getMessage() + "\n" + urlString; 
      Log.e("Something went wrong", errorMessage); 
      return errorMessage; 
     } 
    } 
} 

HttpHelper:

public class HttpHelper { 

    public static CharSequence GetHttpResponse(String urlString) throws IOException { 
     URL url = new URL(urlString); 
     URLConnection connection = url.openConnection(); 
     if(!(connection instanceof HttpURLConnection)){ 
      throw new IOException("Not an HTTP connection"); 
     } 

     HttpURLConnection httpConnection = (HttpURLConnection) connection; 
     int responseCode = httpConnection.getResponseCode(); 
     if(responseCode != HttpURLConnection.HTTP_OK){ 
      String responseMessage = httpConnection.getResponseMessage(); 
      throw new IOException("HTTP response code: " + responseCode + " " + responseMessage); 

     } 
     InputStream inputStream = httpConnection.getInputStream(); 
     BufferedReader reader = null; 
     try{ 
      reader = new BufferedReader(new InputStreamReader(inputStream)); 
      StringBuilder result = new StringBuilder(httpConnection.getContentLength()); 
      while(true){ 
       String line = reader.readLine(); 
       if(line==null) break; 
       result.append(line); 
      } 
      return result; 
     } finally { 
      if(reader != null) reader.close(); 
     } 
    } 
} 
+2

你只能得到,如果你想創建一個數組'NegativeArraySizeException' - 我不能看到你這樣做,在該代碼。請包含完整的堆棧跟蹤,並在上面的代碼中指明相關的行號。 –

+2

閱讀此:https://developer.android.com/reference/java/net/URLConnection.html#getContentLength()就像你說的,這個來自Stringbuilder接收-1。爲什麼,請檢查您用於創建此實例的方法。該文告訴你,它可能在兩種情況下返回-1。 – AxelH

+0

@AndyTurner,StringBuilder也使用這個異常,因爲它是一個動態字符數組;) – AxelH

回答

1

您使用從URLConnection的的getContentLength方法創建的StringBuilder的一個實例。

首先,讓我們看到的StringBuilder:

StringBuilder public StringBuilder(int capacity)

Constructs a string builder with no characters in it and an initial capacity specified by the capacity argument. Parameters:capacity - the initial capacity. Throws:NegativeArraySizeException - if the capacity argument is less than 0.

如果您打開該文檔,您將看到:

Returns

int the content length of the resource that this connection's URL references, -1 if the content length is not known, or if the content length is greater than Integer.MAX_VALUE.

所以,你有你的解決方案。如果您收到一個值,您不應創建具有特定大小的StringBuilder(如果這是-1)。如果沒有,讓SB管理是自己的大小。

像這樣創建的StringBuilder:

StringBuilder result = new StringBuilder(); //create with a size of 16 character. 
+0

謝謝你的回答。我對如何讓SB管理自己的規模感到有點困惑。如果我正確地理解了你,那麼'httpConnection.getContentLength();'是指定SB大小的那個,但是如果不是那樣的話,你會得到什麼內容? – kennyYice23

+0

@ kennyYice23那麼,你應該知道一個stringBuilder是一個管理器本身就是一個字符數組,所以如果你追加實例並且數組很小,它將調整它的大小。基本上,只需創建一個不帶參數的StringBuilder。它會創建一個15的數組,我認爲,當更多的字符到達時,數組將會增長,不會出現超出限制的異常(無法說明數組的內存限制是什麼......)。 我已添加該行以創建SB – AxelH

+0

非常感謝!它現在有效:D – kennyYice23

相關問題