负数组大小异常 [英] Negative Array Size Exception
问题描述
我是新来的黑莓,我试图让后一个搜索词在XML中的服务器。不过,我不断收到此错误请求失败。原因Java.lang.NegativeArraySizeException
。
我要检查是否连接正常我解析数据之前,所以从这个方面,我希望接收的XML响应文本。下面是code:
公共无效的WebPost(字符串字){
字= EN code(字);
字符串的responseText;
尝试{
的HttpConnection连接=(HttpConnection的)Connector.open(HTTP://一些url.xml);
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
字符串POSTDATA =用户名= loginapi&放大器;密码= myapilogin&放术语=+字;
connection.setRequestProperty(内容长度,Integer.toString(postData.length()));
connection.setRequestProperty(用户代理,资料/ MIDP-2.0配置/ CLDC-1.0);
OutputStream的请求输出= connection.openOutputStream();
requestOut.write(postData.getBytes()); InputStream的detailIn = connection.openInputStream();
字节信息[] =新的字节[(INT)connection.getLength()];
detailIn.read(信息);
detailIn.close();
requestOut.close();
connection.close()时;
responseText的=新的String(信息);
requestSuceeded(requestOut.toString(),responseText的);
}
赶上(例外前){
requestFailed(ex.toString());
}
}
私人无效requestSuceeded(字符串结果,字符串的responseText){
如果(responseText.startsWith(文/ XML)){
字符串strResult =新的String(结果);
同步(UiApplication.getEventLock()){
textOutputField.setText(strResult);
}
}
其他{
同步(UiApplication.getEventLock()){
Dialog.alert(未知的内容类型:+ responseText的);
}
}
}
公共无效requestFailed(最后弦乐消息){
UiApplication.getUiApplication()的invokeLater(Runnable的新(){
公共无效的run(){
Dialog.alert(请求失败原因:+消息);
}
});
}
私人字符串连接code(字符串textIn){
// EN为HTTP POST code文本
textIn = textIn.replace('','+');
字符串的TextOut =;
的for(int i = 0; I< textIn.length();我++){
焦炭wcai = textIn.charAt(I)
如果(Character.isDigit(wcai)及!&放大器;!Character.isLowerCase(wcai)及&放大器;!Character.isUpperCase(wcai)及&放大器;!wcai ='+'){
开关(wcai){
案件 '。':
案件 '-':
案件 '*':
案件 '_':
的TextOut = +的TextOut wcai;
打破;
默认:
=的TextOut textout+\"%\"+Integer.toHexString(wcai).toUpperCase();//=textout.concat(\"%\").concat(Integer.toHexString(wcai));
}
}其他{
的TextOut = +的TextOut wcai; // = textout.concat(wcai +);
}
}
返回的TextOut;
}
找到了!我忘了开的输出流连接
=请求输出connection.openOutputStream();
和我介绍了 ByteArrayOutpuStream
这让我终于显示输入流。我也改变了我送参数的方式,并用 URLEn codedPostData code>键入代替。由于服务器是间preting我以前的请求作为GET而不是POST。而我现在要做的是解析信息进来。
{尝试
连接=(HttpConnection的)Connector.open(HTTP://someurl.xml,Connector.READ_WRITE);
URLEn codedPostData POSTDATA =新URLEn codedPostData(URLEn codedPostData.DEFAULT_CHARSET,FALSE);
postData.append(用户名,loginapi);
postData.append(密码,myapilogin);
postData.append(术语,字); connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty(内容类型,应用程序/ x-WWW的形式urlen codeD);
connection.setRequestProperty(用户代理,资料/ MIDP-2.0配置/ CLDC-1.0);
请求输出= connection.openOutputStream();
requestOut.write(postData.getBytes());
串的contentType = connection.getHeaderField(内容类型);
detailIn = connection.openInputStream();
INT长度=(int)的connection.getLength();
ByteArrayOutputStream BAOS =新ByteArrayOutputStream();
如果(长度大于0){
字节信息[] =新的字节[长度]
INT读取动作= detailIn.read(信息);
而(读取动作大于0){
baos.write(资讯,0,读取动作);
读取动作= detailIn.read(信息);
}
baos.close();
connection.close()时;
requestSuceeded(baos.toByteArray(),则contentType); detailIn.read(信息);
}
其他
{
的System.out.println(负数组大小);
}
requestOut.close();
detailIn.close();
connection.close()时;
}
PS。我张贴上述code,以帮助任何人同样的问题。
PPS。我还用卡莱的格式它帮助奇妙。
I'm new to Blackberry and I'm trying to get post a search term to a server in xml. But I keep getting this error Request Failed. Reason Java.lang.NegativeArraySizeException
.
I wanted to check if the connection works before I parse the data so from this connection, am expecting to receive the response text in xml. Below is the code:
public void webPost(String word) {
word = encode (word);
String responseText;
try{
HttpConnection connection = (HttpConnection)Connector.open("http://some url.xml");
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
String postData = "username=loginapi&password=myapilogin&term="+ word;
connection.setRequestProperty("Content-Length",Integer.toString(postData.length()));
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
OutputStream requestOut = connection.openOutputStream();
requestOut.write(postData.getBytes());
InputStream detailIn = connection.openInputStream();
byte info[]=new byte[(int)connection.getLength()];
detailIn.read(info);
detailIn.close();
requestOut.close();
connection.close();
responseText=new String(info);
requestSuceeded(requestOut.toString(), responseText);
}
catch(Exception ex){
requestFailed(ex.toString());
}
}
private void requestSuceeded(String result, String responseText) {
if(responseText.startsWith("text/xml")) {
String strResult = new String(result);
synchronized(UiApplication.getEventLock()) {
textOutputField.setText(strResult);
}
}
else{
synchronized(UiApplication.getEventLock()) {
Dialog.alert("Unknown content type: " + responseText);
}
}
}
public void requestFailed(final String message) {
UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
Dialog.alert("Request failed. Reason: " + message);
}
});
}
private String encode(String textIn) {
//encode text for http post
textIn = textIn.replace(' ','+');
String textout = "";
for(int i=0;i< textIn.length();i++){
char wcai = textIn.charAt(i);
if(!Character.isDigit(wcai) && !Character.isLowerCase(wcai) && !Character.isUpperCase(wcai) && wcai!='+'){
switch(wcai){
case '.':
case '-':
case '*':
case '_':
textout = textout+wcai;
break;
default:
textout = textout+"%"+Integer.toHexString(wcai).toUpperCase();//=textout.concat("%").concat(Integer.toHexString(wcai));
}
}else{
textout = textout+wcai;//=textout.concat(wcai+"");
}
}
return textout;
}
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.
这篇关于负数组大小异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!