我從諾基亞N97手機上傳文件到服務器,一切工作正常,但文件被上傳後,我想從服務器響應。問題是我只在半分鐘或更長時間後纔得到回覆。從我看到的httpConnection.getResponseCode()中的代碼塊等待響應。我在索尼愛立信上測試過它,並且我的響應速度非常快,所以我認爲它是諾基亞N97問題。 (不是服務器的問題,因爲它工作正常,在其他手機)J2ME的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
}
}