How to handle exceptions, an error occurred while executing doInBackground ()
I am using Asynctask
in my application to fetch data from server. When my app is connected to the internet it works fine, but when I disconnect it suddenly stops.
Here's my code:
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();
}
}
I am getting the error:
-FATAL EXCEPTION: AsyncTask # 3 Process: kuldeep.mourya.com.smartcollege, PID: 10617 java.lang.RuntimeException: An error occurred while executing doInBackground () at android.os.AsyncTask $ 3.done (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) at android.os.AsyncTask $ SerialExecutor $ 1.run (AsyncTask.java:234) at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor. java: 1113) at java.util.concurrent.ThreadPoolExecutor $ Worker.run (ThreadPoolExecutor.java:588) at java.lang.Thread.run (Thread.java:818) Called by: java.lang.NullPointerException: Attempting to call virtual method 'void java.io.BufferedReader.close ()' on a null object Help at kuldeep.mourya.com.smartcollege.FragmentProfessor.CollegeNewsFragment $ JsonTask.doInBackground (CollegeNewsFrag3). mourya.com.smartcollege.FragmentProfessor.CollegeNewsFragment $ JsonTask.doInBackground (CollegeNewsFragment.java:148) at android.os.AsyncTask $ 2.call (AsyncTask.java:295)on the null object Help at kuldeep.mourya.com.smartcollege.FragmentProfessor.CollegeNewsFragment $ JsonTask.doInBackground (CollegeNewsFragment.java:223) at kuldeep.mourya.com.smartcollege.FragmentProfessorInewsground.CollegeNews CollegeNewsFragment. at android.os.AsyncTask $ 2.call (AsyncTask.java:295)on the null object Help at kuldeep.mourya.com.smartcollege.FragmentProfessor.CollegeNewsFragment $ JsonTask.doInBackground (CollegeNewsFragment.java:223) at kuldeep.mourya.com.smartcollege.FragmentProfessorInewsground.CollegeNews CollegeNewsFragment. at android.os.AsyncTask $ 2.call (AsyncTask.java:295)mourya.com.smartcollege.FragmentProfessor.CollegeNewsFragment $ JsonTask.doInBackground (CollegeNewsFragment.java:148) at android.os.AsyncTask $ 2.call (AsyncTask.java:295)mourya.com.smartcollege.FragmentProfessor.CollegeNewsFragment $ JsonTask.doInBackground (CollegeNewsFragment.java:148) at android.os.AsyncTask $ 2.call (AsyncTask.java:295)
Does anyone know why I am getting this error?
woow !!! i got the answer stripping the try catch block exception!
//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;
source to share
br = new BufferedReader(new InputStreamReader(stream));
In the above line of code, where did you declare and initialize BufferedReader?
When a connection is available, there is a high chance that you will definitely receive data in your stream. But when you are not connected to the Internet, the stream is zero.
What I advise you is a small change in your code:
URL url = new URL("http://javalovers.net16.net/showdata.php");
connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.connect();
Put this in a separate try..catch and do it first. If that is successful, go to the stream reading code.
Also one more change: declare BufferredReader and then and where you use each time (this is generally good practice). Like the code snippet below:
BufferredReader br = new BufferedReader(new InputStreamReader(stream));
source to share