Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/221.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java wifi超出范围时应用程序崩溃_Java_Android_Httpurlconnection_Android Wireless - Fatal编程技术网

Java wifi超出范围时应用程序崩溃

Java wifi超出范围时应用程序崩溃,java,android,httpurlconnection,android-wireless,Java,Android,Httpurlconnection,Android Wireless,我有一个应用程序,当手机上的wifi超出范围时会崩溃。它主要从一个在线txt获取字符串,我在完成之后断开了HTTPURLConnection,所以我没有预料到崩溃。以下是相关代码 要检查网络可用性(使用internet连接的所有代码都将首先通过此项进行检查): 在onResume上运行的我的checkForPromptPassword异步任务: private class CheckForPromptPasswordAgain extends AsyncTask<Void, Void, B

我有一个应用程序,当手机上的wifi超出范围时会崩溃。它主要从一个在线txt获取字符串,我在完成之后断开了HTTPURLConnection,所以我没有预料到崩溃。以下是相关代码

要检查网络可用性(使用internet连接的所有代码都将首先通过此项进行检查):

在onResume上运行的我的checkForPromptPassword异步任务:

private class CheckForPromptPasswordAgain extends AsyncTask<Void, Void, Boolean>
    {
        //    ProgressDialog pdLoading = new ProgressDialog(MainScreenActivity.this);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //this method will be running on UI thread
            //  pdLoading.setMessage("\tFetching Database...");
            //     pdLoading.show();
        }
        @Override
        protected Boolean doInBackground(Void... params) {

            //this method will be running on background thread so don't update UI frome here
            //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here
            //view GONE by default of update button



                    if (isNetworkAvailable()){
                        if (PromptForPasswordAgain()){
                            //TODO: //show update button or dialog
                            return true;
                        }else{
                            //TODO: //proceed as normal 
                            return false;
                        }
                    }


                    return false;

        }





        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);


            //this method will be running on UI thread


            if (result==true){
                promptForPassword();
            }else{

                //  showDialog("string", "string",0);
            }



            //  pdLoading.dismiss();
        }
    }

我们将非常感谢您的帮助。谢谢。

对于崩溃,请始终查看logcat中的异常stacktrace。当然,我真傻,我不能在我的模拟器上重现这个错误,也没有安卓设备。将尽快用stacktrace更新,但如果我能从代码中获得任何见解,我将非常感激。但是,我将使用设备并运行错误检查。请检查preexecute()中的internet连接功能。。此检查internet连接方法不能在postExecute()中调用。请尝试{}捕获(异常e){}
private class CheckForPromptPasswordAgain extends AsyncTask<Void, Void, Boolean>
    {
        //    ProgressDialog pdLoading = new ProgressDialog(MainScreenActivity.this);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //this method will be running on UI thread
            //  pdLoading.setMessage("\tFetching Database...");
            //     pdLoading.show();
        }
        @Override
        protected Boolean doInBackground(Void... params) {

            //this method will be running on background thread so don't update UI frome here
            //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here
            //view GONE by default of update button



                    if (isNetworkAvailable()){
                        if (PromptForPasswordAgain()){
                            //TODO: //show update button or dialog
                            return true;
                        }else{
                            //TODO: //proceed as normal 
                            return false;
                        }
                    }


                    return false;

        }





        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);


            //this method will be running on UI thread


            if (result==true){
                promptForPassword();
            }else{

                //  showDialog("string", "string",0);
            }



            //  pdLoading.dismiss();
        }
    }
Private class CheckForUpdate extends AsyncTask<Void, Void, Boolean>
        {
            //    ProgressDialog pdLoading = new ProgressDialog(MainScreenActivity.this);

        @Override
        protected void onPreExecute() {
            super.onPreExecute();

            //this method will be running on UI thread
            //  pdLoading.setMessage("\t string.");
            //     pdLoading.show();
        }
        @Override
        protected Boolean doInBackground(Void... params) {

            //this method will be running on background thread so don't update UI frome here
            //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here
            //view GONE by default of update button


            if (fileExistance("data.txt")){
                try {
                    if (isNetworkAvailable()){
                        if (isDatabaseContentDifferent()){
                            //TODO: //show update button or dialog
                            return true;
                        }else{
                            //TODO: //proceed as normal 
                            return false;
                        }
                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block

                    e.printStackTrace();
                }
            }else{
                Log.i("error","file data.txt does not exist in internal");
                return false;
            }

            return false;
        }


        protected void onPostExecute(Boolean result) {
            super.onPostExecute(result);


            //this method will be running on UI thread

            Button updateButton = (Button) findViewById(R.id.UpdateDatabase);
            updateButton.setVisibility(View.GONE);
            if (result==true){
                showDialog("Database Updated On Server", "The  Database App has detected a change in the database, press \"Update Database\" to account for the change(s).  ",0);
                updateButton.setVisibility(View.VISIBLE);
            }else{
                updateButton.setVisibility(View.GONE);
                //  showDialog("No Update Detected", "The Database App has detected a change in the database, press \"Update Database\" to account for the change(s).  ",0);
            }



            //  pdLoading.dismiss();
        }
    }
public boolean isDatabaseContentDifferent() throws IOException{
        String page = null;
        try{
            URL url = new URL(_data);
            HttpURLConnection.setFollowRedirects(true);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setDoOutput(false);
            con.setReadTimeout(20000);
            con.setRequestProperty("Connection", "keep-alive");
            //get etag for update check
            //String etag= "";

            con.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:16.0) Gecko/20100101 Firefox/16.0");
            ((HttpURLConnection) con).setRequestMethod("GET");
            //System.out.println(con.getContentLength()) ;
            con.setConnectTimeout(5000);
            BufferedInputStream in = new BufferedInputStream(con.getInputStream());
            //make seperate function for etag it doesn't work with GET
            //String etag = con.getHeaderField("etag");

            int responseCode = con.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                System.out.println(responseCode);
            }
            StringBuffer buffer = new StringBuffer();
            int chars_read;
            //int total = 0;
            while ((chars_read = in.read()) != -1) 
            {
                char g = (char) chars_read;
                buffer.append(g);
            }
            page = buffer.toString();
            //create password.txt to internal
//TODO: checkkk
            con.disconnect();
        }catch(Exception e){
            showDialog("Database Fetch Failure","Unable to Fetch Password Database, check your internet" +
                    " connection and try again later.",0);
            Log.i("Page", "Error in isDatabaseContentDifferent()");
            return false;
        }

        if (fileExistance("data.txt")){
            if (isTextInFileDifferent(page,"data.txt")){

                return true;

            }else{
                return false;
            }

    }else{
        Log.i("Page","file data.txt does not exist IN isDatabaseContentDifferent()");
        return false;
        }
    }