如何防止服务器未找到导致Android应用程序崩溃?

时间:2015-03-28 09:38:34

标签: android android-asynctask httpurlconnection

我正在使用HttpURLConnection连接到我的远程服务器。现在万一服务器关闭,应用程序崩溃了。

如何使用吐司或新活动更优雅地处理此事?

这是我的代码,

public class SignIn extends AsyncTask<Bundle,Void,Void> {

    public SignIn(Context context) {
        mContext = context;
    }


    @Override
    protected Void doInBackground(Bundle... params) {
        userData = params[0];

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;
        URL url;

        try{
            String base_url = "http://10.0.0.11:8080/SignIn";
            Uri builtUri = Uri.parse(base_url).buildUpon()
                    .appendQueryParameter("email", userData.getString("email"))
                    .appendQueryParameter("password", userData.getString("password"))
                    .build();

            url = new URL(builtUri.toString());
            //Log.e("URL",url.toString());
            urlConnection = (HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("GET");
            urlConnection.connect();

            //Some code to read the Input stream and parse JSON not included.

        } catch (Exception e) {
            Log.e("Network Error", e.toString(), e);

        }

        return null;
    }
}

Logcat输出

03-29 08:42:48.596: E/Network Error(1213): java.io.FileNotFoundException: http://52.0.81.130:8080/LoggerLog-1.0/SignIn?email=a&password=a
03-29 08:42:48.596: E/Network Error(1213): java.io.FileNotFoundException: http://52.0.81.130:8080/LoggerLog-1.0/SignIn?email=a&password=a
03-29 08:42:48.596: E/Network Error(1213):  at org.apache.harmony.luni.internal.net.www.protocol.http.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:521)
03-29 08:42:48.596: E/Network Error(1213):  at com.truebros.healthlog.LoginStuff.SignIn.doInBackground(SignIn.java:105)
03-29 08:42:48.596: E/Network Error(1213):  at com.truebros.healthlog.LoginStuff.SignIn.doInBackground(SignIn.java:21)
03-29 08:42:48.596: E/Network Error(1213):  at android.os.AsyncTask$2.call(AsyncTask.java:185)
03-29 08:42:48.596: E/Network Error(1213):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
03-29 08:42:48.596: E/Network Error(1213):  at java.util.concurrent.FutureTask.run(FutureTask.java:138)
03-29 08:42:48.596: E/Network Error(1213):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
03-29 08:42:48.596: E/Network Error(1213):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
03-29 08:42:48.596: E/Network Error(1213):  at java.lang.Thread.run(Thread.java:1019)
03-29 08:42:48.606: E/Parsing error(1213): SignIn.java

1 个答案:

答案 0 :(得分:0)

在URLConnection中使用setReadTimeout(TIMEOUT_CONNECTION)和setConnectTimeout(TIMEOUT_SOCKET)。这将有助于管理服务器故障和网络问题。有关详情,请参阅下面的链接[1] http://developer.android.com/reference/java/net/URLConnection.html#setConnectTimeout(int) [2] http://developer.android.com/reference/java/net/URLConnection.html#setReadTimeout(int)