J2ME的HttpConnection getResponseCode()从服务器上诺基亚N97等待答案块
-
18-09-2019 - |
题
我上传从诺基亚N97手机,服务器上的文件,一切工作正常,但文件被上传后,我想从服务器响应。问题是,我得到的只有半分钟或更长时间后的反应。从我看到httpConnection.getResponseCode代码块()等待响应。我测试了索尼爱立信和我得到的响应速度非常快,所以我认为是诺基亚N97的问题。 (不是服务器的问题,因为它工作正常上其它电话)
有谁知道为什么这件事情只发生在诺基亚?
下面是一个代码段:
公共uploadFile(){
httpConn = (HttpsConnection) Connector.open(url, Connector.READ_WRITE);
...
//set httpConn parameters and request method
...
writeParametersAndFileName(os, "text/plain");
**writeFileToStream(os);**
os.write("\r\n".getBytes());
os.write("--".getBytes());
os.write(boundary.getBytes());
os.write("--".getBytes());
os.write("\r\n".getBytes());
*//here code blocks on Nokia N97. Same thing happens if I use os.flush() after
// I send chunks of the file*
.....
codeResp = httpConn.getResponseCode();
// check condition
checkResponseHeader();
}
公共的 writeFileToStream()强> {
InputStream is = null;
FileConnection c = null;
try
{
c = (FileConnection) Connector.open("file:///"+ sourcePath, Connector.READ);
if(c.exists()) {
is = c.openInputStream();
long fs = c.fileSize();
while (total < fs) {
byte[] data = new byte[512];
int readAmount = is.read(data,0,512);
total += readAmount;
os.write(data, 0, readAmount);
}
//os.flush();
}
catch (IOException ex) {
//
}
finally
{
//close connection
}
}
解决方案
您只想到你的文件已被上传。
您到getResponseCode()调用是使HttpConnection的第一个真正连接到该服务器。
它不会返回,直到其上载文件,这大概花费一分钟以上。
这是根据在JSR-118中的规范的HttpConnection完全有效的行为:
” 下列方法导致过渡到连接状态时,连接处于安装状态。
* openInputStream
* openDataInputStream
* getLength
* getType
* getEncoding
* getHeaderField
* getResponseCode
* getResponseMessage
* getHeaderFieldInt
* getHeaderFieldDate
* getExpiration
* getDate
* getLastModified
* getHeaderField
* getHeaderFieldKey
“
我希望你已经试过这在其他手机比N97那么强大,需要冲洗的OutputStream,因此连接到服务器,他们应该根据规范之前。
不隶属于 StackOverflow