Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Context context;
- AlertDialog alertDialog;
- boolean correct;
- public BackgrroundWorker(Context context){
- this.context = context;
- }
- @Override
- protected String doInBackground(String... params) {
- String type = params[0];
- String login_url = "random";
- String register_url = "random";
- if(type.equals("login")){
- try {
- String username = params[1];
- String password = params[2];
- URL url = new URL(login_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
- String post_data = URLEncoder.encode("username","UTF-8") + "=" +URLEncoder.encode(username,"UTF-8")+ "&"
- +URLEncoder.encode("password","UTF-8") + "=" +URLEncoder.encode(password,"UTF-8");
- bufferedWriter.write(post_data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
- String result = "";
- String line = "";
- correct = false;
- while((line = bufferedReader.readLine()) != null){
- result += line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- System.out.println(result);
- if (result.equalsIgnoreCase("login success"))
- correct=true;
- return result;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onPreExecute() {
- progressDialog = ProgressDialog.show(context, "Login",
- "Please wait for a while.", true);
- alertDialog = new AlertDialog.Builder(context).create();
- alertDialog.setTitle("Login status");
- }
- @Override
- protected void onPostExecute(String result) {
- if(!result.equals("login success")) {
- alertDialog.setMessage(result);
- alertDialog.show();
- }
- progressDialog.dismiss();
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement