Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //define constructor
- public BackgroundTask (Context ctx){
- this.ctx = ctx;
- activity = (Activity)ctx;
- }
- @Override
- protected void onPreExecute() {
- builder = new AlertDialog.Builder(activity);
- progressDialog = new ProgressDialog(ctx);
- progressDialog.setTitle("Please wait");
- progressDialog.setMessage("Connecting to server.....");
- progressDialog.setIndeterminate(true);
- progressDialog.setCancelable(false);
- progressDialog.show();
- }
- @Override
- protected String doInBackground(String... params){
- String method = params[0];
- if (method.equals("register"))
- {
- try {
- URL url = new URL(register_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 email = params[1];
- String username = params[2];
- String password = params[3];
- String data = URLEncoder.encode("email", "UTF-8")+"="+URLEncoder.encode(email,"UTF-8")+"&"+
- URLEncoder.encode("username", "UTF-8")+"="+URLEncoder.encode(username,"UTF-8")+"&"+
- URLEncoder.encode("password", "UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
- StringBuilder stringBuilder = new StringBuilder();
- String line = "";
- while ((line=bufferedReader.readLine())!=null);
- {
- stringBuilder.append(line+"n");
- }
- httpURLConnection.disconnect();
- Thread.sleep(5000);
- return stringBuilder.toString().trim();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- return null;
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(String json) {
- try{
- progressDialog.dismiss();
- JSONObject jsonObject = new JSONObject(json);
- JSONArray jsonArray = jsonObject.getJSONArray("server_response");
- JSONObject JO = jsonArray.getJSONObject(0);
- String code = JO.getString("code");
- String message = JO.getString("message");
- if (code.equals("reg_true")){
- showDialog("Registration Success",message,code);
- }
- else if(code.equals("reg_false")){
- showDialog("Registration Failed!",message,code);
- }
- } catch (JSONException e) {
- Log.e("JSON Parser", "Error parsing data:" + e.toString());
- }
- }
- public void showDialog(String title,String message,String code){
- builder.setTitle(title);
- if (code.equals("reg_true")||code.equals("reg_false")){
- builder.setMessage(message);
- builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- dialog.dismiss();
- activity.finish();
- }
- });
- AlertDialog alertDialog = builder.create();
- alertDialog.show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement