Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.gokdemir.areyousafe;
- import android.annotation.SuppressLint;
- import android.app.ProgressDialog;
- import android.content.Intent;
- import android.icu.text.SimpleDateFormat;
- import android.location.Location;
- import android.os.Build;
- import android.support.annotation.RequiresApi;
- import android.util.Base64;
- import android.util.Log;
- import android.widget.Toast;
- import com.android.volley.DefaultRetryPolicy;
- import com.android.volley.Request;
- import com.android.volley.RequestQueue;
- import com.android.volley.Response;
- import com.android.volley.RetryPolicy;
- import com.android.volley.VolleyError;
- import com.android.volley.toolbox.StringRequest;
- import com.android.volley.toolbox.Volley;
- import org.json.JSONException;
- import org.xmlpull.v1.XmlPullParser;
- import org.xmlpull.v1.XmlPullParserFactory;
- import java.io.UnsupportedEncodingException;
- import java.net.URL;
- import java.net.URLEncoder;
- import java.util.Arrays;
- import java.util.Date;
- import java.util.HashMap;
- import java.util.Map;
- /**
- * Created by gokde on 7.01.2018.
- */
- public class VolleyHandler {
- //keys for the parameters
- public static final String KEY_NAME = "name";
- public static final String KEY_EMAIL = "mail";
- public static final String KEY_PASSWORD = "password";
- public static final String KEY_BLOODTYPE = "bloodType";
- public static final String KEY_ALLERGIE = "allergyName";
- public static final String KEY_ALLERGIES = "allergies"; //needed for full user signup
- public static final String KEY_CHRONIC = "chronicName";
- public static final String KEY_CHRONICS ="chronics";
- public static final String KEY_PHOTO = "photo";
- public static final String KEY_UPDATE_PHOTO = "update";
- public static final String KEY_MESSAGE = "message";
- public static final String KEY_XLOC = "latitude";
- public static final String KEY_YLOC = "longitude";
- public static final String KEY_ID = "id"; //same as email
- public static final String KEY_TIME = "time";
- public static final String KEY_MAC = "mac";
- public static final String KEY_USER_EMAIL = "email";
- public static final String KEY_NEARBY = "nearbyMACs";
- private ProgressDialog progressDialog;
- SessionManager session;
- public static void getUserInfo(final String umail, final UserProfileActivity.VolleyCallBack callBack){
- final User[] user = new User[1];
- RequestQueue queue = Volley.newRequestQueue(UserProfileActivity.context);
- StringRequest getRequest = new StringRequest(Request.Method.GET, URLUtils.getUserInfoURL(umail),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- Toast.makeText(UserProfileActivity.context, "Information obtained successfully!", Toast.LENGTH_SHORT).show();
- Log.v("Json", response);
- try {
- user[0] = JSONParser.parseUserJSON(response);
- callBack.onSuccess(user[0]);
- } catch (JSONException e) {
- e.printStackTrace();
- }
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- Toast.makeText(UserProfileActivity.context, "There was an error obtaining your information. Try again later!", Toast.LENGTH_SHORT).show();
- }
- }
- ) {
- };
- queue.add(getRequest);
- }
- public void addUser(final String uname,final String umail,final String upassword,final String ubloodType){
- RequestQueue queue = Volley.newRequestQueue(SignupActivity2.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserSignupURL(uname, umail, upassword, ubloodType),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- actOnResponseInAddingUser(response);
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- actOnError(error);
- }
- }
- ) {
- };
- queue.add(postRequest);
- }
- public void addMAC(final String email,final String mac){
- RequestQueue queue = Volley.newRequestQueue(Dashboard.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getMacUpdateURL(email,mac),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- }
- ) {
- };
- queue.add(postRequest);
- }
- public void addChronicDisease(final String userID, final String userDisease){
- RequestQueue queue = Volley.newRequestQueue(SignupActivity2.context);
- int socketTimeout = 30000; // 30 seconds. You can change it
- RetryPolicy policy = new DefaultRetryPolicy(socketTimeout,
- DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
- DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserChronicCreationURL(userID, userDisease),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- actOnResponseInAddingChronicDiseases(response);
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- actOnError(error);
- }
- }
- ) {
- };
- queue.add(postRequest);
- }
- public void addAllergicMedication(final String userID, final String userAllergic){
- RequestQueue queue = Volley.newRequestQueue(SignupActivity2.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserAllergyCreationURL(userID, userAllergic),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- actOnResponseInAddingAllergicMedications(response);
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- actOnError(error);
- }
- }
- ) {
- };
- queue.add(postRequest);
- }
- public void addPhoto(final String userID, final byte[] photo){
- final String stringImage = Arrays.toString(photo);
- RequestQueue queue = Volley.newRequestQueue(SignupActivity2.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserPhotoCreationURL(userID, stringImage),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- actOnResponseInAddingPhoto(response);
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- actOnError(error);
- }
- }
- ) {
- };
- queue.add(postRequest);
- }
- public void updatePhoto(final String userID, final byte[] photo, final String update){
- final String stringImage = Arrays.toString(photo);
- RequestQueue queue = Volley.newRequestQueue(SignupActivity2.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserPhotoUpdateURL(userID, stringImage, update),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- actOnResponseInAddingPhoto(response);
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- actOnError(error);
- }
- }
- ) {
- };
- queue.add(postRequest);
- }
- public void addUserAll(final String uname, final String umail, final String upassword, final String ubloodType, final String uallergies, final String uchronics, final byte[] uphoto) throws UnsupportedEncodingException {
- session = new SessionManager(SignupActivity2.activityContext);
- //encode the image to send to the database...
- final String stringImage = URLEncoder.encode(Base64.encodeToString(uphoto, Base64.DEFAULT),"UTF-8");
- Log.d("STR Image",stringImage);
- progressDialog = new ProgressDialog(SignupActivity2.activityContext);
- progressDialog.setMessage("Signing you up. Please wait...");
- progressDialog.show();
- RetryPolicy policy = new DefaultRetryPolicy(10000,
- DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
- DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
- RequestQueue queue = Volley.newRequestQueue(SignupActivity2.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserFullSignupURL(uname, umail, upassword, ubloodType, uallergies, uchronics, stringImage),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- actOnResponseInAddingAllUserInfo(response);
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- actOnError(error);
- }
- }
- ) {
- };
- postRequest.setRetryPolicy(policy);
- queue.add(postRequest);
- }
- @RequiresApi(api = Build.VERSION_CODES.N)
- public void setNearbys(final String nearbyMACs,Location location)
- {
- Log.d("SET NEARBYS","Test");
- session = new SessionManager(Dashboard.context);
- final String mail = session.getUserDetails().get(KEY_USER_EMAIL);
- SimpleDateFormat sdf = new SimpleDateFormat("dd;MM;yy;hh;mm;ss");
- String currentDateandTime = sdf.format(new Date());
- if(nearbyMACs.length() != 0)
- {
- try{
- Log.d("SENDING Nearby","TEST");
- RequestQueue queue = Volley.newRequestQueue(Dashboard.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserNearbyUpdateURL(mail, nearbyMACs, currentDateandTime,location),
- new Response.Listener<String>()
- {
- @Override
- public void onResponse(String response) {
- actOnResponseInSettingNearby(response);
- }
- },
- new Response.ErrorListener()
- {
- @Override
- public void onErrorResponse(VolleyError error) {
- Log.d("Nearby Send",error.toString());
- }
- }
- ) {
- };
- queue.add(postRequest);
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- }
- }
- public void updateLocationInformation(final String userID, final Location location,final String date){
- RetryPolicy policy = new DefaultRetryPolicy(10000,
- DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
- DefaultRetryPolicy.DEFAULT_BACKOFF_MULT);
- RequestQueue queue = Volley.newRequestQueue(Dashboard.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserLocationUpdateURL(userID, String.valueOf(location.getLatitude()),String.valueOf(location.getLongitude()),date,"80"),
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- if (response.toLowerCase().contains("true")) {
- Log.d("TEST",response);
- }
- else {
- Log.d("TEST","FAILED");
- }
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- }
- ) {
- };
- postRequest.setRetryPolicy(policy);
- queue.add(postRequest);
- }
- public void updateUserBloodType(final String userID, final String bloodType){
- RequestQueue queue = Volley.newRequestQueue(UserProfileActivity.context);
- StringRequest postRequest = new StringRequest(Request.Method.POST, URLUtils.getUserBloodTypeUpdateURL(userID, bloodType),
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- if (response.toLowerCase().contains("true")) {
- Toast.makeText(UserProfileActivity.context, "Your bloodtype has been updated successfully.", Toast.LENGTH_SHORT).show();
- }
- else {
- Toast.makeText(UserProfileActivity.context, "There was a problem updating your blood type. Please try again.", Toast.LENGTH_SHORT).show();
- }
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- }
- ) {
- };
- queue.add(postRequest);
- }
- public void deleteUserAllergy(final String userID, final String allergyName){
- RequestQueue queue = Volley.newRequestQueue(UserProfileActivity.context);
- StringRequest deleteRequest = new StringRequest(Request.Method.DELETE, URLUtils.getDeleteUserAllergyURL(userID, allergyName),
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- if (response.toLowerCase().contains("true")) {
- Toast.makeText(UserProfileActivity.context, "Your allergy has been deleted successfully.", Toast.LENGTH_SHORT).show();
- }
- else {
- Toast.makeText(UserProfileActivity.context, "There was a problem deleting your allergy. Please try again.", Toast.LENGTH_SHORT).show();
- }
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- }
- ) {
- };
- queue.add(deleteRequest);
- }
- public void deleteUserChronic(final String userID, final String chronicName){
- RequestQueue queue = Volley.newRequestQueue(UserProfileActivity.context);
- StringRequest deleteRequest = new StringRequest(Request.Method.DELETE, URLUtils.getDeleteUserChronicURL(userID, chronicName),
- new Response.Listener<String>() {
- @Override
- public void onResponse(String response) {
- if (response.toLowerCase().contains("true")) {
- Toast.makeText(UserProfileActivity.context, "Your chronic disease has been deleted successfully.", Toast.LENGTH_SHORT).show();
- }
- else {
- Toast.makeText(UserProfileActivity.context, "There was a problem deleting your chronic disease. Please try again.", Toast.LENGTH_SHORT).show();
- }
- }
- },
- new Response.ErrorListener() {
- @Override
- public void onErrorResponse(VolleyError error) {
- }
- }
- ) {
- };
- queue.add(deleteRequest);
- }
- private void actOnResponseInSettingNearby(String response) {
- /* XML Parser for extracting related information from response.
- TODO: Server will return a JSON object in the 2nd phase, this should be adjusted
- */
- try {
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParser xpp = factory.newPullParser();
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- /*
- Check Reponse to give related message!
- */
- if(response.toLowerCase().contains("false")){
- Log.d("Nearby Server","Failed!");
- }
- else{
- Log.d("Nearby Server","Succeeded!");
- }
- }
- private void actOnResponseInMarkingAsSafe(String response) {
- progressDialog.dismiss();
- /* XML Parser for extracting related information from response.
- TODO: Server will return a JSON object in the 2nd phase, this should be adjusted
- */
- try {
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParser xpp = factory.newPullParser();
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- /*
- Check Reponse to give related message!
- */
- if(response.toLowerCase().contains("false")){
- Toast.makeText(Dashboard.context, "The message couldn't be sent. Try again!", Toast.LENGTH_SHORT).show();
- }
- else{
- Toast.makeText(Dashboard.context, "Message successfully sent.", Toast.LENGTH_SHORT).show();
- }
- }
- private void actOnResponseInAddingAllUserInfo(String response) {
- progressDialog.dismiss();
- /* XML Parser for extracting related information from response.
- TODO: Server will return a JSON object in the 2nd phase, this should be adjusted
- */
- try {
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParser xpp = factory.newPullParser();
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- /*
- Check Reponse to give related message!
- */
- if(response.toLowerCase().contains("false")){
- Toast.makeText(SignupActivity2.activityContext, "Could not register at step 4. Check your image.", Toast.LENGTH_SHORT).show();
- }
- else{
- /*
- Direct the user to login page to enter the credentials..
- */
- Toast.makeText(SignupActivity2.activityContext, "Successfully signed up.", Toast.LENGTH_SHORT).show();
- Intent i = new Intent(SignupActivity2.context, MainActivity.class);
- i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- SignupActivity2.activityContext.startActivity(i);
- }
- }
- private void actOnResponseInAddingPhoto(String response) {
- progressDialog.dismiss();
- /* XML Parser for extracting related information from response.
- TODO: Server will return a JSON object in the 2nd phase, this should be adjusted
- */
- try {
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParser xpp = factory.newPullParser();
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- /*
- Check Reponse to give related message!
- */
- if(response.toLowerCase().contains("false")){
- Toast.makeText(SignupActivity2.activityContext, "Could not register at step 4. Check your image.", Toast.LENGTH_SHORT).show();
- }
- else{
- Toast.makeText(SignupActivity2.activityContext, "Step 4 Successful!", Toast.LENGTH_SHORT);
- /*
- Direct the user to login page to enter the credentials..
- */
- Toast.makeText(SignupActivity2.activityContext, "Successfully signed up.", Toast.LENGTH_SHORT).show();
- Intent i = new Intent(SignupActivity2.context, MainActivity.class);
- i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- SignupActivity2.activityContext.startActivity(i);
- }
- }
- private void actOnResponseInAddingChronicDiseases(String response) {
- //progressDialog.dismiss();
- /* XML Parser for extracting related information from response.
- TODO: Server will return a JSON object in the 2nd phase, this should be adjusted
- */
- try {
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParser xpp = factory.newPullParser();
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- /*
- Check Response to give related message!
- */
- if(response.toLowerCase().contains("false")){
- Toast.makeText(SignupActivity2.activityContext, "Could not register at step 2. Check your credentials.", Toast.LENGTH_SHORT).show();
- }
- else{
- Toast.makeText(SignupActivity2.activityContext, "Step 2 Successful!", Toast.LENGTH_SHORT);
- /*
- Direct the user to login page to enter the credentials..
- Toast.makeText(SignupActivity2.activityContext, "Successfully signed up.", Toast.LENGTH_SHORT).show();
- Intent i = new Intent(SignupActivity2.context, MainActivity.class);
- i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- SignupActivity2.activityContext.startActivity(i);
- */
- }
- }
- private void actOnResponseInAddingAllergicMedications(String response) {
- //progressDialog.dismiss();
- /* XML Parser for extracting related information from response.
- TODO: Server will return a JSON object in the 2nd phase, this should be adjusted
- */
- try {
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParser xpp = factory.newPullParser();
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- /*
- Check Reponse to give related message!
- */
- if(response.toLowerCase().contains("false")){
- Toast.makeText(SignupActivity2.activityContext, "Could not register. Check your credentials.", Toast.LENGTH_SHORT).show();
- }
- else{
- Toast.makeText(SignupActivity2.activityContext, "Step 3 Successful!", Toast.LENGTH_SHORT);
- /*
- Direct the user to login page to enter the credentials..
- Toast.makeText(SignupActivity2.activityContext, "Successfully signed up.", Toast.LENGTH_SHORT).show();
- Intent i = new Intent(SignupActivity2.context, MainActivity.class);
- i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- SignupActivity2.activityContext.startActivity(i);
- */
- }
- }
- private void actOnResponseInAddingUser(String response) {
- //progressDialog.dismiss();
- /* XML Parser for extracting related information from response.
- TODO: Server will return a JSON object in the 2nd phase, this should be adjusted
- */
- try {
- XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
- factory.setNamespaceAware(true);
- XmlPullParser xpp = factory.newPullParser();
- }catch (Exception e)
- {
- e.printStackTrace();
- }
- /*
- Check Reponse to give related message!
- */
- if(response.toLowerCase().contains("false")){
- Toast.makeText(SignupActivity2.activityContext, "Could not register at Step 1. Check your credentials.", Toast.LENGTH_LONG).show();
- }
- else{
- Toast.makeText(SignupActivity2.activityContext, "Step 1 Successful!", Toast.LENGTH_SHORT).show();
- /*
- Direct the user to login page to enter the credentials..
- Toast.makeText(SignupActivity2.activityContext, "Successfully signed up.", Toast.LENGTH_SHORT).show();
- Intent i = new Intent(SignupActivity2.context, MainActivity.class);
- i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
- i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- SignupActivity2.activityContext.startActivity(i);
- */
- }
- }
- private void actOnError(VolleyError error) {
- progressDialog.dismiss();
- //displaying the error...
- Toast.makeText(SignupActivity2.activityContext, error.toString(), Toast.LENGTH_SHORT).show();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement