捕获异常后,模拟器仍然显示“应用程序已意外停止”

发布于 2024-12-08 14:42:43 字数 860 浏览 0 评论 0原文

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;
}

如果互联网连接处于活动状态,则此代码可以正常工作,但是当我断开系统连接(这也会断开模拟器)时,此方法会抛出 UnknownHostException 并通过打开我的 Splash.java(启动屏幕)将其捕获在 catch 块中,但同时它还给我“应用程序已意外停止,请重试”。

所以请给我可以帮助我的答案或建议。

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 disconnects 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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

吾家有女初长成 2024-12-15 14:42:43

正如您所要求的,这是我在更正式的答案中的评论:

也许您的问题是由于您在异常情况下返回 null 这一事实引起的。

如果调用者在引用方法结果之前未能检查它是否不为 null,则可能会抛出 NullPointerException。

尝试在 shell 中输入 adb logcat 来查看发生了什么以及发生在哪里。

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.

放血 2024-12-15 14:42:43

我认为它抛出异常,因为在 catch 块之后,它会到达此语句 return is; ,如果代码抛出异常,则认为 null。

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.

碍人泪离人颜 2024-12-15 14:42:43

您可以使用此功能:

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;
}

控制您是否在线。您必须添加权限才能执行此操作:

<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" />

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" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文