[英]how to handle exceptions an error occurred while executing doInBackground()
我在我的應用程序中使用Asynctask
從服務器檢索數據。 當我的應用程序連接到Internet時,它工作正常,但是當我斷開連接時,它會突然停止。
這是我的代碼:
try {
URL url = new URL("http://javalovers.net16.net/showdata.php");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.connect();
switch (connection.getResponseCode()) {
case HttpURLConnection.HTTP_OK:
InputStream stream = connection.getInputStream(); //here getting response
br = new BufferedReader(new InputStreamReader(stream));
String line = "";
while ((line = br.readLine()) != null) {
// buffer.append(line);
str = str + line;
}
break; // fine, go on
case HttpURLConnection.HTTP_GATEWAY_TIMEOUT:
break; // retry
case HttpURLConnection.HTTP_UNAVAILABLE:
break; // retry, server is unstable
default:
break; // abort
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
connection.disconnect();
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
我收到錯誤:
-FATAL EXCEPTION:AsyncTask#3進程:kuldeep.mourya.com.smartcollege,PID:10617 java.lang.RuntimeException:在android.os.AsyncTask $ 3.done執行doInBackground()時發生錯誤(AsyncTask.java:309) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:354)at java.util.concurrent.FutureTask.setException(FutureTask.java:223)at java.util.concurrent.FutureTask.run(FutureTask.java:242) )在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)的android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:234)java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor) .java:588)at java.lang.Thread.run(Thread.java:818)引起:java.lang.NullPointerException:嘗試在空對象上調用虛方法'void java.io.BufferedReader.close()'參考kuldeep.mourya.com.smartcollege.FragmentProfessor.CollegeNewsFragment $ JsonTask.doInBackground(CollegeNewsFragment.java:223)at kuldeep.mourya.com.smartcollege.FragmentP rofessor.CollegeNewsFragment $ JsonTask.doInBackground(CollegeNewsFragment.java:148)在android.os.AsyncTask $ 2.call(AsyncTask.java:295)
有誰知道我為什么會收到這個錯誤?
WOOW! 我在分離異常嘗試捕獲塊時得到了答案!
//URL url=new URL("http://javalovers.net16.net/showdata.php");
URL url = null;// this api link
try {
url = new URL("http://vcetsmart.netne.net/showdata.php");
} catch (MalformedURLException e) {
e.printStackTrace();
}
try {
connection = (HttpURLConnection) url.openConnection();
} catch (IOException e) {
e.printStackTrace();
}
try {
connection.setRequestMethod("POST");
} catch (ProtocolException e) {
e.printStackTrace();
}
try {
connection.connect();
} catch (IOException e) {
e.printStackTrace();
}
try{
if(connection.getResponseCode()==200)
{
//Toast.makeText(getBaseContext(),"Everything is right",Toast.LENGTH_SHORT).show();
InputStream stream=connection.getInputStream(); //here getting response
BufferedReader br = new BufferedReader(new InputStreamReader(stream));
String line = "";
while ((line = br.readLine()) != null) {
// buffer.append(line);
str=str+line;
}
}
else {
Toast toast= Toast.makeText(getActivity(),"Something goes wrong", Toast.LENGTH_LONG);
toast.show();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return str;
br = new BufferedReader(new InputStreamReader(stream));
在上面的代碼行中,您在哪里聲明並初始化BufferedReader?
當連接可用時,您很可能肯定會將數據導入流中。 但是當你沒有連接到互聯網時,流是空的。
我的建議是你的代碼中的一個小改動:
URL url = new URL("http://javalovers.net16.net/showdata.php");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.connect();
把它放在一個單獨的try..catch中並先做。 如果成功,請繼續閱讀流的代碼。
還有一個變化是,然后聲明BufferredReader以及每次使用的位置(這通常是一種很好的做法)。 就像下面的代碼片段一樣:
BufferredReader br = new BufferedReader(new InputStreamReader(stream));
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.