在調試WCF服務時,我遇到了這個問題。我使用一個HttpURLConnection
與服務進行通信。在調試服務時,我遇到了一個問題,即當Android應用程序等待響應時,必須停止服務器,導致Android應用程序意外停止。當連接到服務器丟失時,Android應用程序崩潰
什麼是最好的方法來處理服務器意外突然停止,並讓Android客戶端優雅地處理它。我請求/通過輸入反應的的AsyncTask在後臺工作:
@Override
protected JSONObject doInBackground(BLHttpJSONPostModel ...models) {
JSONObject jsonObject = null;
BLHttpJSONPostModel postModel = models[0];
URL url = null;
byte[] postData = null;
String protocol = null;
String response = null;
InputStream responseStream = null;
OutputStream outStream = null;
HttpURLConnection connection = null;
BufferedReader bufferedReader = null;
StringBuilder builder = null;
try {
url = new URL(postModel.getUrlString());
postData = postModel.getPostDataAsJsonObject().toString().getBytes();
protocol = url.getProtocol();
} catch (MalformedURLException e1) {
e1.printStackTrace();
return null;
}
if (protocol.equals("https")) {
connection = this.setUpHttps(url); // establishes a secure connection if protocol is HTTPS
}
else if (protocol.equals("http")) {
try {
connection = (HttpURLConnection)url.openConnection(); // establishes un-secured connection
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
else
return null; // handles unsupported protocol
try {
connection.setRequestProperty("Content-Length", String.valueOf(postData.length));
connection.setRequestProperty("CONTENT-TYPE", "application/json");
connection.setRequestMethod("POST");
connection.setUseCaches(false);
connection.setDoInput(true);
connection.setDoOutput(true);
connection.connect();
outStream = connection.getOutputStream();
outStream.write(postData);
outStream.close();
String _type = connection.getContentType(); // for debugging only
String _message = connection.getResponseMessage(); // ""
int size = connection.getContentLength(); // ""
bufferedReader = new BufferedReader(new InputStreamReader(connection.getInputStream(), "UTF-8"));
builder = new StringBuilder();
while((response = bufferedReader.readLine()) != null) {
builder.append(response);
}
} catch (IOException e) {
e.printStackTrace(); // no exceptio caught here
} finally {
connection.disconnect();
}
if ((builder != null) && (! builder.toString().equals(""))) {
try {
jsonObject = new JSONObject(builder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
}
return jsonObject;
}
堆棧跟蹤:
02-20 12:13:33.935: E/AndroidRuntime(1173): FATAL EXCEPTION: AsyncTask #1
02-20 12:13:33.935: E/AndroidRuntime(1173): java.lang.RuntimeException: An error occured while executing doInBackground()
02-20 12:13:33.935: E/AndroidRuntime(1173): at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.util.concurrent.FutureTask.run(FutureTask.java:137)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.lang.Thread.run(Thread.java:1096)
02-20 12:13:33.935: E/AndroidRuntime(1173): Caused by: java.lang.NullPointerException
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.io.Reader.<init>(Reader.java:65)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.io.InputStreamReader.<init>(InputStreamReader.java:93)
02-20 12:13:33.935: E/AndroidRuntime(1173): at com.beslogic.remotpayment.connection.PostJSONTask.doInBackground(PostJSONTask.java:136)
02-20 12:13:33.935: E/AndroidRuntime(1173): at com.beslogic.remotpayment.connection.PostJSONTask.doInBackground(PostJSONTask.java:1)
02-20 12:13:33.935: E/AndroidRuntime(1173): at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-20 12:13:33.935: E/AndroidRuntime(1173): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
02-20 12:13:33.935: E/AndroidRuntime(1173): ... 4 more
你捕獲了堆棧跟蹤嗎? – 2012-02-20 17:04:53
@NickCampion更新了堆棧跟蹤。 – 2012-02-20 17:21:07