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

}

This is my background task. My application is crashing if internet is not available. I've used lot of technics like on getsystemservices but as it's a non activity class, it's not accessible. Please suggests me something.

Inzimam Tariq IT
  • 6,548
  • 8
  • 42
  • 69
Naveed Abbas
  • 220
  • 3
  • 10
  • The problem is almost certainly in your doInBackground. Probably because you assume the download works and try to use the output which is null, but without the right code we can't tell you for sure. – Gabe Sechan Jul 24 '16 at 05:49
  • this is my complete class – Naveed Abbas Jul 24 '16 at 05:58
  • If this is your complete class then it must show compilation error. As you must have to implement doInBackground method of AsyncTask class. – Md Sufi Khan Jul 24 '16 at 05:58
  • If you really knew the problem, it would've been better if you searched before posting. Related: [How to manage connection lost with HttpURLConnection?](http://stackoverflow.com/questions/17628688/how-to-manage-connection-lost-with-httpurlconnection). – Sufian Jul 24 '16 at 07:54

1 Answers1

0

I the case of an exception, doInBackground returns null. You need to check for that on onPostExecute our you'll crash work a null point exception

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127