我的客户端有时会遇到两个异常SocketException:当我尝试连接soap web服务器时,Socket被关闭并且EOFException。
我使用此代码:
private String callWebService(String soapAction, String envelope, boolean runInBackground) {
if (soapAction == null || envelope == null) {
return null;
}
int retry = RETRY_COUNT;
int count = 0;
boolean shouldRetry = false;
if (mShouldRetryWhenError == false) {
retry = 1;
}
while (count < retry) {
count++;
URL address = null;
try {
address = new URL(URL);
} catch (MalformedURLException e1) {
e1.printStackTrace();
}
boolean errorOcurred = false;
HttpsURLConnection connection = null;
OutputStream outputStream = null;
InputStream responseStream = null;
try {
System.setProperty("http.keepAlive", "false");
connection = (HttpsURLConnection)address.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestMethod("POST");
connection.setRequestProperty("SOAPAction", NAMESPACE + "#" + soapAction);
connection.setRequestProperty("Content-type", "text/xml; charset=utf-8");
connection.setRequestProperty("Accept-Charset", "utf-8");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) {
connection.setRequestProperty("Connection", "close");
}
connection.setReadTimeout(TIMEOUT);
connection.setConnectTimeout(TIMEOUT);
connection.setFixedLengthStreamingMode(envelope.length());
} catch (SocketTimeoutException e) {
errorOcurred = true;
shouldRetry = true;
} catch (Exception e) {
errorOcurred = true;
Utils.sendErrorReport(TAG + " - error while openning connection: " + e.toString(), mContext);
if (!runInBackground) {
handleError(null);
}
}
if (errorOcurred) {
if (connection != null) {
connection.disconnect();
}
if (!shouldRetry) {
return null;
}
}
try {
outputStream = connection.getOutputStream();
} catch (SocketTimeoutException e) {
errorOcurred = true;
shouldRetry = true;
} catch (IOException e) {
errorOcurred = true;
if (!(e instanceof UnknownHostException)) {
Utils.sendErrorReport(TAG + " - no output stream could be created: " + e.toString(), mContext);
}
if (!runInBackground) {
handleError(null);
}
}
if (errorOcurred) {
if (connection != null) {
connection.disconnect();
}
if (!shouldRetry) {
return null;
}
}
Writer out = null;
try {
out = new OutputStreamWriter(outputStream, "utf-8");
out.write(envelope);
out.flush();
//out.close();
int responseCode = connection.getResponseCode();
if (responseCode >= 300) {
InputStream errorStream = connection.getErrorStream();
String error = readDataFromInputStream(errorStream);
Utils.sendErrorReport(TAG + ": responseCode " + responseCode + ", error: " + error, mContext);
if (errorStream != null) {
errorStream.close();
}
}
} catch (SocketTimeoutException e) {
errorOcurred = true;
shouldRetry = true;
} catch (IOException e) {
errorOcurred = true;
Utils.sendErrorReport(TAG + " - writing to the output stream: " + e.toString(), mContext);
if (!runInBackground) {
handleError(null);
}
}
if (errorOcurred || connection.getContentLength() <= 0) {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
}
}
if (connection != null) {
connection.disconnect();
}
if (!shouldRetry) {
return null;
}
}
try {
responseStream = connection.getInputStream();
} catch (SocketTimeoutException e) {
errorOcurred = true;
shouldRetry = true;
} catch (IOException e) {
errorOcurred = true;
Utils.sendErrorReport(TAG + " - input stream could not be created: " + e.toString(), mContext);
if (!runInBackground) {
handleError(null);
}
}
if (errorOcurred) {
if (out != null) {
try {
out.close();
} catch (IOException e) {
}
}
if (outputStream != null) {
try {
outputStream.close();
} catch (IOException e) {
}
}
if (connection != null) {
connection.disconnect();
}
if (!shouldRetry) {
return null;
}
}
String response = null;
try {
response = readDataFromInputStream(responseStream);
} catch (SocketTimeoutException e) {
shouldRetry = true;
} catch (Exception e) {
Utils.sendErrorReport(TAG + " - reading data from the input stream: " + e.toString(), mContext);
if (!runInBackground) {
handleError(null);
}
} finally {
try {
if (out != null) {
out.close();
}
if (outputStream != null) {
outputStream.close();
}
if (responseStream != null) {
responseStream.close();
}
} catch (IOException e) {
}
if (connection != null) {
connection.disconnect();
}
}
if (!shouldRetry) {
return response;
}
}
return null;
}
SocketException发生在这里:
outputStream = connection.getOutputStream();
或
response = readDataFromInputStream(responseStream);
EOFException发生在这里:
int responseCode = connection.getResponseCode();
有人知道这里有什么问题吗?