开发者

After catching exception ,the emulator still shows "Application has stopped unexpectedly "

private InputStream getISFromURL(String url) {
  //post
  InputStream is=null;
  try {
    HttpParams params=new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, 3000);
    HttpConnectionParams.setSoTimeout(params, 3000);
    HttpClient httpClient=new DefaultHttpClient(params);
    HttpPost httpPost=new HttpPost(url);
    HttpResponse httpResponce=httpClient.execute(httpPost);
    HttpEntity httpEntity=httpResponce.getEntity();
    is=httpEntity.getContent();
  } catch (Exception e) {
    this.context.startActivity(new Intent(this.context, Splash.class));
    Log.d("imsoft", "getJSONdataFromURL ="+e.toString());
  }
  return is;
}

This code works good if the internet connection is alive but when i disconnect my system(which dis开发者_C百科connects emulator too) then this method throws UnknownHostException and it is catched in the catch block by opening my Splash.java(splash screen) but at the same time it also give me "Application has stopped unexpectedly Please try again" .

So please give me answer or suggestions that can help me.


As asked by you, here's my comment in a more formal answer:

Maybe your problem is caused by the fact that you you return null in the case of an Exception.

The caller might throw a NullPointerException if it fails to check that your method result is not null before referencing it.

Try type adb logcat in a shell to see what happens and where it happens.


I think it is throwing exception because after catch block it is coming to this statement return is; which is think null if code throw exception.


You can use this function:

public static boolean isOnline(Context mContext) {
    ConnectivityManager cm = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
    NetworkInfo netInfo = cm.getActiveNetworkInfo();
    if (netInfo != null && netInfo.isConnectedOrConnecting())
        return true;
    else
        return false;
}

to control if you're online or not. You have to add permission to do this:

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_PHONE_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜