mic*_*l92 5 post blackberry http java-me
我是 Blackberry 的新手,我正在尝试以 xml 格式将搜索词发布到服务器。但我不断收到此错误Request Failed. Reason Java.lang.NegativeArraySizeException。
我想在解析数据之前检查连接是否有效,所以从这个连接中,我希望收到 xml 中的响应文本。下面是代码:
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;
}
Run Code Online (Sandbox Code Playgroud)
找到了!我忘记打开输出流连接
requestOut = connection.openOutputStream();
我介绍了ByteArrayOutpuStream 它帮助我最终显示输入流。我还改变了发送参数的方式,并改用URLEncodedPostData类型。由于服务器将我之前的请求解释为 GET 而不是 POST。我现在要做的就是解析传入的信息。
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();
}
Run Code Online (Sandbox Code Playgroud)
附言。我发布了上面的代码来帮助遇到同样问题的人。
聚苯硫醚。我还使用了Kalai 的格式,它很有帮助。
| 归档时间: |
|
| 查看次数: |
31654 次 |
| 最近记录: |