Skip to content

Instantly share code, notes, and snippets.

Created March 30, 2016 18:20
Show Gist options
  • Save anonymous/e0bfcd7a9e954ae574d59c123cb0c4ae to your computer and use it in GitHub Desktop.
Save anonymous/e0bfcd7a9e954ae574d59c123cb0c4ae to your computer and use it in GitHub Desktop.
BackgroundTask.class
package com.alexander.loginapp_2;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.AsyncTask;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
public class BackgroundTask extends AsyncTask<String, Void, String> {
String register_url = "http://delivia.esy.es/loginapp/register_test.html";
Context ctx;
Activity activity;
AlertDialog.Builder builder;
ProgressDialog progressDialog;
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 hold on second..");
progressDialog.setMessage("Connecting you 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 name = params[1];
String email = params[2];
String password = params[3];
String data =
URLEncoder.encode("name", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
URLEncoder.encode("email", "UTF-8") + "=" + URLEncoder.encode(email, "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")) {
showDialogue("Registration was successful champ!", message, code);
} else if (code.equals("reg_false")) ;
{
showDialogue("Registration failed :(", message, code);
}
} catch (JSONException e) {
e.printStackTrace();
}
}
public void showDialogue(String title, String message, String code)
{
builder.setTitle(title);
if (code.equals("reg_true") || code.equals("reg_false")) {
builder.setMessage(message);
builder.setPositiveButton("Alright", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
activity.finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment