注意:我已将此代码编写为Titanium Module Project并从Titanium项目调用。
protected final void callService(final byte[] data) throws IOException {
OutputStream outputStream = null;
DataOutputStream output = null;
InputStream inputStream = null;
String stringdata = "";
try {
String url = "http://localhost:8080/myproject/testService.do"
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.setRequestProperty("Cookie", sessionId);
urlConnection.setRequestMethod("POST");
urlConnection.setDoInput(true);
urlConnection.setDoOutput(false);
urlConnection.setReadTimeout(200000);
urlConnection.setConnectTimeout(200000);
urlConnection.connect();
outputStream = urlConnection.getOutputStream();
output = new DataOutputStream(outputStream);
// write the file data
if (data != null) {
output.write(data);
stringdata = new String(data);
Log.iTimer(TAG, "Data uploded to the server stream");
}
inputStream = connection.getInputStream();
String outData = inputStream.toString();
System.out.print(outData);
} catch (Exception exception) {
e.printStackTrace();
} finally {
if (output != null) {
output.flush();
// Close Output stream
output.close();
}
if (outputStream != null) {
outputStream.close();
}
}
}
答案 0 :(得分:1)
不确定它是否有帮助,但你可以试试这个:
urlConnection.setUseCaches(false);