kaveer
3/24/2017 - 6:28 AM

AsyncTask

AsyncTask

public class Login  extends AsyncTask<String, String, String> {

    ProgressDialog progressDialog;
    HttpURLConnection connection = null;
    BufferedReader reader = null;

    Activity activity;

    public Login(Activity activity) {
        this.activity = activity;
    }


    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        // display a progress dialog for good user experiance
        progressDialog = new ProgressDialog(activity);
        progressDialog.setMessage("Please Wait");
        progressDialog.setCancelable(false);
        progressDialog.show();
    }

    @Override
    protected String doInBackground(String... params) {
        String result = "";
        try {
            URL url = new URL(params[0]);
            String username = params[1];
            String password = params[2];

            connection = (HttpURLConnection) url.openConnection();
            connection.setDoOutput(true);
            connection.setRequestMethod("POST");
            connection.setRequestProperty("Content-Type", "application/json");
            connection.connect();

            JSONObject jsonObject = new JSONObject();
            jsonObject.put("username", username);
            jsonObject.put("password", password);

            DataOutputStream wr = new DataOutputStream(connection.getOutputStream());
            wr.writeBytes(jsonObject.toString());
            wr.flush();
            wr.close();

            InputStream stream = connection.getInputStream();
            reader = new BufferedReader(new InputStreamReader(stream));
            StringBuffer buffer = new StringBuffer();

            String line ="";

            while ((line = reader.readLine()) != null){
                buffer.append(line);
            }

            String  jsonObjectHome = buffer.toString();
            result = jsonObjectHome;

        } catch (Exception e) {
            Log.e("MainActivity", e.getMessage(), e);
        } finally {
            if(connection != null) {
                connection.disconnect();
            }
            try {
                if(reader != null) {
                    reader.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return result;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        progressDialog.cancel();
    }

}


 private void ApiCallForLogin(Activity activity) {
        try {
            String item =  new Login(activity).execute(config.getLoginEndPoint(), "kaveer", "test").get();
            AccountViewModel model = DeserializeModel(item);


        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
    }




 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_accounts);
        
        if (isNetworkConnected()){
            ApiCallForLogin(AccountsActivity.this);
        }
        else {
            Toast messageBox = Toast.makeText(AccountsActivity.this , "No internet" , Toast.LENGTH_LONG);
            messageBox.show();
        }

    }



String result = "";
homeUrl = this.getBaseContext().getResources().getString(R.string.GetHomeScreenFromApEndPoint); 
//endpoint in string.xml

result = GetHomeScreenDetails();

 private String GetHomeScreenDetails() {
    String result = "";

    try {
        result =  new GetHomeScreenFromApi().execute(homeUrl).get();

    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    }

    return result;
}

private class GetHomeScreenFromApi extends AsyncTask<String, String, String> {
        @Override
        protected String doInBackground(String... params) {
            String result = "";
            try {
                URL url = new URL(params[0]);
                connection = (HttpURLConnection) url.openConnection();
                connection.connect();
                InputStream stream = connection.getInputStream();
                reader = new BufferedReader(new InputStreamReader(stream));
                StringBuffer buffer = new StringBuffer();

                String line ="";

                while ((line = reader.readLine()) != null){
                    buffer.append(line);
                }

                String  jsonObjectHome = buffer.toString();
                result = jsonObjectHome;

            } catch (Exception e) {

                Log.e("MainActivity", e.getMessage(), e);

            } finally {
                if(connection != null) {
                    connection.disconnect();
                }
                try {
                    if(reader != null) {
                        reader.close();
                    }
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }

            return result;
        }
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        protected void onPostExecute(String result) {
            super.onPostExecute(result);
        }

    }