我有一個應用程序可以在Asynctask中下載多個文件(並檢查下載速度)。但是,當我連接到3G(並有一個切換)或設備切換形式的WiFi到3G,我的應用程序凍結,我不能處理這個問題asynctask下載文件網絡連接丟失
@Override
protected Integer doInBackground(String... sUrl) {
try {
speed=0; //initial value
int i=0;
while ((i<sUrl.length)) {
URL url = new URL(sUrl[i]);
URLConnection connection = url.openConnection();
connection.setReadTimeout(10000);
connection.connect();
int fileLength = connection.getContentLength();
// download the file
InputStream input = new BufferedInputStream(url.openStream());
OutputStream output = new FileOutputStream(file);
byte data[] = new byte[1024*1024]; //1MB buffer
long total = 0;
int count;
long start = System.currentTimeMillis();
while ((count = input.read(data)) != -1) {
total += count;
publishProgress((int) (total * 100/fileLength));
output.write(data, 0, count);
}
long finish = System.currentTimeMillis();
long tempSpeed= (fileLength *8)/(finish-start);
if (tempSpeed>speed) {
speed=tempSpeed;
}
output.flush();
output.close();
input.close(); // connection is closed
i++;
}
}catch(SocketTimeoutException e) {
exceptions.add(e);
messageProgressDialog.setCancelable(true);
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(gui.getActivity()).create();
alertDialog.setTitle("Network problem");
alertDialog.setMessage("connection dropped");
alertDialog.show();
} catch (IOException e) {
exceptions.add(e);
messageProgressDialog.setCancelable(true);
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(gui.getActivity()).create();
alertDialog.setTitle("IOException");
alertDialog.setMessage("IOException error");
alertDialog.show();
} catch(Exception e) {
exceptions.add(e);
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(gui.getActivity()).create();
alertDialog.setTitle("Exception");
alertDialog.setMessage("Exception error");
alertDialog.show();
}
return 1;
}
我已經閱讀計算器許多議題,但他們中的任何一個都不能幫助我解決我在申請中遇到的問題。我有try/catch子句,但似乎沒有什麼區別。當我使用3G並且手機連接到其他天線時,或者存在網絡問題時,應用程序會凍結。我能做什麼 ?
我發現了這個問題。它是這條線InputStream input = new BufferedInputStream(url.openStream());
我用作輸入流的URL。我用下面的代碼替換了它:InputStream input = new BufferedInputStream(connection.getInputStream());
現在它似乎更好,當它超時,應用程序崩潰。
}catch(SocketTimeoutException e) {
String erro=Integer.toString(count);
Log.d("error socketTimeout",erro);//1st log
exceptions.add(e);
onPostExecute(1);
Log.d("error sockteTimeout", "here"); //2nd log to see if onPostExecute worked
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(gui.getActivity()).create();
alertDialog.setTitle("Network problem");
alertDialog.setMessage("connection dropped");
alertDialog.show();
這是我使用的捕捉。看起來,當我嘗試調用catch子句內onPostExecute方法我的應用程序崩潰。
protected void onPostExecute(Integer result) {
Log.d("onpost was called", "here");
messageProgressDialog.setMessage("Your speed is: " + speed +" KBit/sec");
messageProgressDialog.setCancelable(true);
}
看着日誌查看器,只有第一個日誌出現。當onPostExecute被調用時,應用程序崩潰。 onPostExecute永遠不會被實際執行。 任何想法?
不幸的是,它並沒有什麼區別。該應用程序仍然凍結。 P.S.我添加了catch(異常e)其他異常,我想不出來......但它仍然凍結/ –
因此,當input.read()實際上「凍結」時,它會返回什麼? –
我怎樣才能得到這個?我在哪裏可以看到input.read()返回的內容? 我是新來的android和努力學習。 –