Negative Array Size Exception

喜你入骨 提交于 2019-12-04 11:28:58

connection.getLength() is returning -1.

Before creating info array, check the length of the connection.

int length = (int) connection.getLength();

if(length > 0){
     byte info[]=new byte[length];
     // perform operations

}else{
     System.out.println("Negative array size");
}

I'm assuming connection.getLength() returns -1 when you try to initialize your array here:

byte info[]=new byte[(int)connection.getLength()];

And that being the reason for the NegativeArraySizeException.

I guess when you do

byte info[]=new byte[(int)connection.getLength()];

InputStream does not know its length, so it returns -1.

see http://www.velocityreviews.com/forums/t143704-inputstream-length.html

selladurai

Ref: http://supportforums.blackberry.com/t5/Java-Development/HttpConnection-set-to-POST-does-not-work/m-p/344946

Ref1: Blackberry send a HTTPPost request

Ref2: http://www.blackberryforums.com/developer-forum/181071-http-post-passing-parameters-urls.html

Something like this:

URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, true); 
postData.append("name",name); 
michael92

Found it! I forgot to open the Output Stream connection

requestOut = connection.openOutputStream();

and I introduced ByteArrayOutpuStream which helped me finally display the input stream. I also, changed the way I was sending parameters, and used URLEncodedPostData type instead. Since the server was interpreting my former request as a GET instead of a POST. And all I have to do now is to parse the info coming in.

try{
     connection = (HttpConnection)Connector.open("http://someurl.xml",Connector.READ_WRITE);
     URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
     postData.append("username", "loginapi");
     postData.append("password", "myapilogin");
     postData.append("term", word);

     connection.setRequestMethod(HttpConnection.POST);
     connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
     connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
     requestOut = connection.openOutputStream();
     requestOut.write(postData.getBytes());
     String contentType = connection.getHeaderField("Content-type"); 
     detailIn = connection.openInputStream();         
     int length = (int) connection.getLength();
     ByteArrayOutputStream baos = new ByteArrayOutputStream(); 
     if(length > 0){
         byte info[] = new byte[length];
         int bytesRead = detailIn.read(info);
         while(bytesRead > 0) { 
             baos.write(info, 0, bytesRead); 
             bytesRead = detailIn.read(info); 
             }
         baos.close();
         connection.close();
         requestSuceeded(baos.toByteArray(), contentType);

         detailIn.read(info);
     }
     else
     {
          System.out.println("Negative array size");
     }
           requestOut.close();
           detailIn.close();
           connection.close();
    }

PS. I posted the above code to help anyone with the same problem.

PPS. I also used Kalai's format and it helped wonderfully.

java.lang.NegativeArraySizeException indicates that you are trying to initialize an array with a negative length.

The only code that is initializing is -

byte info[]=new byte[(int)connection.getLength()];

You might want to add a check for length before you initialize the array

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!