Quantcast
Channel: Active questions tagged crash - Stack Overflow
Viewing all articles
Browse latest Browse all 7193

App crashes when connection is lost while using HttpUrlConnection

$
0
0

i'm making a class that get json data from an external url, it is working fine if there is a connection. also shows an error if there isn't.the problem is if the connection lost while the class is getting data the app crashes.

here is my code:

//I always check connection before calling the classclass GetPost extends AsyncTask<String, String, String>{    Context c;    String res;    public GetPost(Context c){        this.c=c;    }    @Override    protected void onPreExecute()    {        super.onPreExecute();        Earn.statu.setText("Loading post...");    }    @Override    protected String doInBackground(String[] p1)    {        try{        URL u = new URL(p1[0]);        HttpURLConnection con = (HttpURLConnection) u.openConnection();        InputStream is = con.getInputStream();        BufferedReader br = new BufferedReader(new InputStreamReader(is));        res = br.readLine();        }           catch (MalformedURLException e){}        catch (IOException e){}        return res;    }    @Override    protected void onPostExecute(String result)    {        super.onPostExecute(result);            try{            JSONObject   jo = new JSONObject(res);            Earn.getVideoData(c,            jo.getString("pid"),            jo.getInt("duration"),            jo.getInt("id")            );        }catch(JSONException a){}    }

so guys is there any solution to prevent the app from crashing?


Viewing all articles
Browse latest Browse all 7193

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>