Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.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
如果互联网不可用,Android应用程序将崩溃_Android_Android Asynctask - Fatal编程技术网

如果互联网不可用,Android应用程序将崩溃

如果互联网不可用,Android应用程序将崩溃,android,android-asynctask,Android,Android Asynctask,这是我的背景任务。如果internet不可用,我的应用程序将崩溃。我在getsystemservices上使用了很多技术,但由于它是一个非活动类,因此无法访问。请给我一些建议。如果出现异常,doInBackground将返回null。您需要在onPostExecute上检查这一点,您将崩溃工作一个空点异常问题几乎肯定在您的doInBackground中。可能是因为您假设下载工作正常,并尝试使用空输出,但如果没有正确的代码,我们无法确定是否告诉您。这是我的完整类如果这是您的完整类,则它必须显示编译

这是我的背景任务。如果internet不可用,我的应用程序将崩溃。我在
getsystemservices
上使用了很多技术,但由于它是一个非活动类,因此无法访问。请给我一些建议。

如果出现异常,doInBackground将返回null。您需要在onPostExecute上检查这一点,您将崩溃工作一个空点异常

问题几乎肯定在您的doInBackground中。可能是因为您假设下载工作正常,并尝试使用空输出,但如果没有正确的代码,我们无法确定是否告诉您。这是我的完整类如果这是您的完整类,则它必须显示编译错误。因为你必须实现AsyncTask类的doInBackground方法。如果你真的知道这个问题,最好在发布之前搜索一下。相关:。
ProgressDialog loading;
Context ctx;
AlertDialog alertDialog;

@Override
protected void onPreExecute() {
    // TODO Auto-generated method stub
    progress.setMessage("Please Wait");
    progress.setMessage("Please Wait");
    //progress.requestWindowFeature(Window.FEATURE_NO_TITLE);
    //progress.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
    progress.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);

    progress.show();

    alertDialog=new AlertDialog.Builder(ctx).create();
    alertDialog.setTitle("Invalid Information");
}

public BackgroundTask(Context ctx) {
    // TODO Auto-generated constructor stub
    this.ctx=ctx;
    progress = new ProgressDialog(this.ctx);
}

@Override
protected String doInBackground(String... params) {
    // TODO Auto-generated method stub
    //http://getdoctor.comlu.com/findDoctorConnect%20.php
    String reg_url="http://finddoctor.site88.net//registerf.php";
    String login_url="http://finddoctor.site88.net//loginf.php";
    String Appointment_url="http://finddoctor.site88.net//userappointef.php";
    String info_url="http://finddoctor.site88.net//updatef.php";
    String confirm_url="http://finddoctor.site88.net//actionf.php";
    String security="http://finddoctor.site88.net//securityf.php";
    String doctorTiming="http://finddoctor.site88.net//profile1f.php";

    String method=params[0];
    if(method.equals("register"))
    {
        String type=params[1];
        String YourName=params[2];
        String regNum=params[3];
        String fatherName=params[4];
        String gender=params[5];
        String mobileNumber=params[6];
        String password=params[7];
        String  specialist=params[8];
        String  rating=params[9];
        String area=params[10];
        String  building=params[11];
        String  room=params[12];
        String cityy=params[13];
        String address=params[14];
        confirmmobile=mobileNumber;

        try {
            URL url=new URL(reg_url);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStream OS=connection.getOutputStream();

            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS));
            String Data=URLEncoder.encode("type","UTF-8")+"="+URLEncoder.encode(type,"UTF-8")+"&"+
                    URLEncoder.encode("YourName","UTF-8")+"="+URLEncoder.encode(YourName,"UTF-8")+"&"+
                    URLEncoder.encode("regNum","UTF-8")+"="+URLEncoder.encode(regNum,"UTF-8")+"&"+
                    URLEncoder.encode("fatherName","UTF-8")+"="+URLEncoder.encode(fatherName,"UTF-8")+"&"+
                    URLEncoder.encode("gender","UTF-8")+"="+URLEncoder.encode(gender,"UTF-8")+"&"+
                    URLEncoder.encode("mobileNumber","UTF-8")+"="+URLEncoder.encode(mobileNumber,"UTF-8")+"&"+
                    URLEncoder.encode("password","UTF-8")+"="+URLEncoder.encode(password,"UTF-8")+"&"+
                    URLEncoder.encode("specialist","UTF-8")+"="+URLEncoder.encode(specialist,"UTF-8")+"&"+
                    URLEncoder.encode("rating","UTF-8")+"="+URLEncoder.encode(rating,"UTF-8")+"&"+
                    URLEncoder.encode("area","UTF-8")+"="+URLEncoder.encode(area,"UTF-8")+"&"+
                    URLEncoder.encode("building","UTF-8")+"="+URLEncoder.encode(building,"UTF-8")+"&"+
                    URLEncoder.encode("room","UTF-8")+"="+URLEncoder.encode(room,"UTF-8")+"&"+
                    URLEncoder.encode("cityy","UTF-8")+"="+URLEncoder.encode(cityy,"UTF-8")+"&"+
                    URLEncoder.encode("address","UTF-8")+"="+URLEncoder.encode(address,"UTF-8");

            bufferedWriter.write(Data);
            bufferedWriter.flush();
            bufferedWriter.close();

            OS.close();
            InputStream IS=connection.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(IS));
            String line;
            StringBuilder builder = new StringBuilder();
            while((line=reader.readLine()) != null)
                builder.append(line);
            IS.close();
            return builder.toString();
        }
        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    } else if(method.equals("login")) {
            MobileNumber = params[1];
            password = params[2];

            try {

                URL url = new URL(login_url);

                HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("POST");
                urlConnection.setDoOutput(true);
                urlConnection.setDoInput(true);
                OutputStream outputStream = urlConnection.getOutputStream();

                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                String data = URLEncoder.encode("MobileNumber", "UTF-8") + "=" + URLEncoder.encode(MobileNumber, "UTF-8") + "&" +
                        URLEncoder.encode("password", "UTF-8") + "=" + URLEncoder.encode(password, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                outputStream.close();

                InputStream inputStream = urlConnection.getInputStream();

                BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));

                while ((linee = bufferedReader.readLine()) != null) {

                    response += linee;

                }


                bufferedReader.close();
                inputStream.close();

                urlConnection.disconnect();
                return response;

            } catch (MalformedURLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    else if(method.equals("Appointment")){

        String name=params[1];
        String dphone=params[2];
        String pphone=params[3];
        String patientname=params[4];

        try {

            URL url=new URL(Appointment_url);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStream OS=connection.getOutputStream();

            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS));
            String Data=URLEncoder.encode("nameq","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"+
                    URLEncoder.encode("number","UTF-8")+"="+URLEncoder.encode(dphone,"UTF-8")+"&"+
                    URLEncoder.encode("usernumber","UTF-8")+"="+URLEncoder.encode(pphone, "UTF-8")+"&"+
                    URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(patientname, "UTF-8");

            bufferedWriter.write(Data);
            bufferedWriter.flush();
            bufferedWriter.close();

            OS.close();
            InputStream IS=connection.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(IS));
            String line;
            StringBuilder builder = new StringBuilder();
            while((line=reader.readLine()) != null)
                builder.append(line);
            IS.close();
            return builder.toString();
        }
        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }

    else if(method.equals("info")){

        String area=params[1];
        String building=params[2];
        String room=params[3];
        String passmobile=params[4];
        String floor=params[5];
        String city=params[6];

        try {

            URL url=new URL(info_url);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStream OS=connection.getOutputStream();

            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS));
            String Data=URLEncoder.encode("area","UTF-8")+"="+URLEncoder.encode(area,"UTF-8")+"&"+
                    URLEncoder.encode("building","UTF-8")+"="+URLEncoder.encode(building,"UTF-8")+"&"+
                    URLEncoder.encode("room","UTF-8")+"="+URLEncoder.encode(room, "UTF-8")+"&"+
                    URLEncoder.encode("passmobile","UTF-8")+"="+URLEncoder.encode(passmobile, "UTF-8")+"&"+
                    URLEncoder.encode("Floor","UTF-8")+"="+URLEncoder.encode(floor, "UTF-8")+"&"+
                    URLEncoder.encode("city","UTF-8")+"="+URLEncoder.encode(city, "UTF-8");

            bufferedWriter.write(Data);
            bufferedWriter.flush();
            bufferedWriter.close();

            OS.close();
            InputStream IS=connection.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(IS));
            String line;
            StringBuilder builder = new StringBuilder();
            while((line=reader.readLine()) != null)
                builder.append(line);
            IS.close();
            return builder.toString();
        }
        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else if(method.equals("confirm")){

        String time=params[1];
        String date=params[2];
        String identifier=params[3];

        try {

            URL url=new URL(confirm_url);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStream OS=connection.getOutputStream();

            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS));
            String Data=URLEncoder.encode("time","UTF-8")+"="+URLEncoder.encode(time,"UTF-8")+"&"+
                    URLEncoder.encode("date","UTF-8")+"="+URLEncoder.encode(date,"UTF-8")+"&"+
                    URLEncoder.encode("identifier","UTF-8")+"="+URLEncoder.encode(identifier, "UTF-8");

            bufferedWriter.write(Data);
            bufferedWriter.flush();
            bufferedWriter.close();

            OS.close();
            InputStream IS=connection.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(IS));
            String line;
            StringBuilder builder = new StringBuilder();
            while((line=reader.readLine()) != null)
                builder.append(line);
            IS.close();
            return builder.toString();
        }
        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    else if(method.equals("security")) {

        String code=params[1];
        String mobileNumber=params[2];

        try {

            URL url=new URL(security);

            HttpURLConnection urlConnection=(HttpURLConnection) url.openConnection();
            urlConnection.setRequestMethod("POST");
            urlConnection.setDoOutput(true);
            urlConnection.setDoInput(true);
            OutputStream outputStream=urlConnection.getOutputStream();

            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
            String data=URLEncoder.encode("code","UTF-8")+"="+URLEncoder.encode(code,"UTF-8")+"&"+
                    URLEncoder.encode("mobileNumber","UTF-8")+"="+URLEncoder.encode(mobileNumber, "UTF-8");;
            bufferedWriter.write(data);
            bufferedWriter.flush();
            bufferedWriter.close();
            outputStream.close();

            InputStream inputStream=urlConnection.getInputStream();

            BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));

            while ((linee = bufferedReader.readLine())!=null ) {

                response+=linee;

            }


            bufferedReader.close();
            inputStream.close();

            urlConnection.disconnect();
            return response;

        } catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
    else if(method.equals("doctortimming")){

        String STH=params[1];
        String STM=params[2];
        String ETH=params[3];
        String ETM=params[4];
        String MobileNumber=params[5];
        String day1=params[6];
        String day2=params[7];
        String duration=params[8];

        try {

            URL url=new URL(doctorTiming);
            HttpURLConnection connection=(HttpURLConnection) url.openConnection();
            connection.setRequestMethod("POST");
            connection.setDoOutput(true);
            OutputStream OS=connection.getOutputStream();

            BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(OS));
            String Data=URLEncoder.encode("STH","UTF-8")+"="+URLEncoder.encode(STH,"UTF-8")+"&"+
                    URLEncoder.encode("STM","UTF-8")+"="+URLEncoder.encode(STM,"UTF-8")+"&"+
                    URLEncoder.encode("ETH","UTF-8")+"="+URLEncoder.encode(ETH, "UTF-8")+"&"+
                    URLEncoder.encode("ETM","UTF-8")+"="+URLEncoder.encode(ETM, "UTF-8")+"&"+
                    URLEncoder.encode("MobileNumber","UTF-8")+"="+URLEncoder.encode(MobileNumber, "UTF-8")+"&"+
                    URLEncoder.encode("day1","UTF-8")+"="+URLEncoder.encode(day1, "UTF-8")+"&"+
                    URLEncoder.encode("day2","UTF-8")+"="+URLEncoder.encode(day2, "UTF-8")+"&"+
                    URLEncoder.encode("duration","UTF-8")+"="+URLEncoder.encode(duration, "UTF-8");

            bufferedWriter.write(Data);
            bufferedWriter.flush();
            bufferedWriter.close();

            OS.close();
            InputStream IS=connection.getInputStream();

            BufferedReader reader = new BufferedReader(new InputStreamReader(IS));
            String line;
            StringBuilder builder = new StringBuilder();
            while((line=reader.readLine()) != null)
                builder.append(line);
            IS.close();
            return builder.toString();
        }
        catch (MalformedURLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    return null;
}

@Override
protected void onPostExecute(String result) {
    // TODO Auto-generated method stub

    alertDialog.setMessage(result);
    alertDialog.show();
    if (result.endsWith("Registration complete")) {
        Intent ia =new Intent(ctx,Confirm.class);
        ia.putExtra("PERSON_NAME", confirmmobile);
        ctx.startActivity(ia);
        Toast.makeText(ctx, "code is send to you mobile ",Toast.LENGTH_LONG).show();
    }
    else if(result.endsWith("already found")){
        alertDialog.setMessage("This mobile number is already used");
        alertDialog.show();
    }
    else if(result.endsWith("something wrong")){
        alertDialog.setMessage("Some fields Missing");
        alertDialog.show();
    }
    else if(result.endsWith("appointment sent")){
        Toast.makeText(ctx,"Request is send to the Doctor",Toast.LENGTH_LONG).show();

        Intent ia =new Intent(ctx,UserDashboard.class);
        ctx.startActivity(ia);
    }
    else if(result.endsWith("2 appointment taken")){
        alertDialog.setMessage("You can not take more then 3 appointments");
        alertDialog.show();
    }
    else if(result.endsWith("complete")){
        alertDialog.setMessage("Successfully Updated");
        alertDialog.show();
    }
    else if (result.endsWith("doctor")) {

        Intent doctor=new Intent(ctx,Doctor_Profile.class);
        doctor.putExtra("PERSON_NAME", MobileNumber);

        ctx.startActivity(doctor);
    }
    else if (result.endsWith("user")) {

        Intent doctor=new Intent(ctx,UserDashboard.class);
        ctx.startActivity(doctor);
        Toast.makeText(ctx, "user is here",Toast.LENGTH_LONG).show();
        doctor.putExtra("PERSON_NAME", MobileNumber);

        ctx.startActivity(doctor);
    }

    else if (result.endsWith("Username or Password is incorrect")) {
        Toast.makeText(ctx, result,Toast.LENGTH_LONG).show();
    }
    else if (result.endsWith("success")) {
        Intent ia =new Intent(ctx,MainActivity.class);
        ctx.startActivity(ia);
        Toast.makeText(ctx, "Login here",Toast.LENGTH_LONG).show();
    }
    else if (result.endsWith("invalid")) {
        Toast.makeText(ctx, "The code you entered is not valid",Toast.LENGTH_LONG).show();
    }

    else if (result.endsWith("security")) {
        Intent ia =new Intent(ctx,Confirm.class);
        ia.putExtra("PERSON_NAME", MobileNumber);
        ctx.startActivity(ia);

        //Toast.makeText(ctx, "The code you entered is not valid",Toast.LENGTH_LONG).show();
    }

    progress.hide();

}