2016-09-30 40 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