如果互联网不可用,Android应用会崩溃

时间:2016-07-24 05:35:14

标签: android android-asynctask

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();

}

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

1 个答案:

答案 0 :(得分:0)

我遇到异常,doInBackground返回null。你需要在onPostExecute上检查它,我们你将崩溃工作一个零点异常