• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Rest api in android

Try this

Java:
public final class FromUrlHelper {

    public static JSONObject getJSONObject(String url) throws IOException {
        if (Constants.debug) Log.d("API Call", url);
        JSONObject jsonObject = null;
        try {
            jsonObject = new JSONObject(FromUrlHelper.getJSONFromUrl(url));
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return jsonObject;
    }
}

then call for example API like this

Java:
public class AuthService {
    private static AuthService ourInstance = null;

    public static AuthService getInstance() {
        if (ourInstance == null) {
            ourInstance = new AuthService();
        }

        return ourInstance;
    }

    private String currentSession;
    private boolean loggined;

    private AuthService() {
    }

    public boolean login(String email, String password) {

        if (!email.isEmpty() && !password.isEmpty()) {
            try {
                JSONObject result = FromUrlHelper.getJSONObject(Constants.serviceBaseUrl + "login?email=" + email + "&password=" + password);
                try {
                    int errorCode = result.getInt("errorCode");
                    if (errorCode == 0) {
                        this.currentSession = result.getString("sessionId");
                        loggined = true;
                        return true;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

        return false;
    }

    public boolean logout() {
        loggined = false;
        this.currentSession = null;
        return true;
    }
}

In Activity you can call this

Code:
AuthService authService = AuthService.getInstance();

if(authService.login("email","password")) {
    // do something
} else {
    // do something
}

In PHP API server it can look like this.

PHP:
        $customer = $this->entityManager->getRepository(Customer::class)->findOneBy(['email' => $request->get('email'), 'password' => md5($request->get('password')), 'eshop' => $eshop]);
        if ($customer) {
            /** @var Session $session */
            $session = $this->getSessionFromCustomer($customer);
            if(!$session) {
                $sessionId = $this->createSession($customer);
            } else {
                $sessionId = $session->getCode(); 
            }
            $response->errorCode = self::ERR_OK;
        } else {
            $sessionId = null;
            $response->errorCode = self::ERR_AUTH;
        }

        $response->sessionId = $sessionId;

        return new JsonResponse($response);

Next in contactless payments...Smile to Pay.

Having been an Annual Passholder for what I believe to be twice in the last ten years, I am fully aware and have experience with that personally. There is no way out of it, I guess.

I have used those Disney fingerprint scanners at Shanghai and Hong Kong Disney parks, and I now know their real purpose, to stop people cheating with the season passes. But they did give me thoughts of big brother 1984, and chilling effects. Especially as the only other organization that's taken an interest in recording my fingerprints is the Ministry of Public Security of the PRC, and have to use similar scanners to the Disney ones every-time I pass through Beijing Airport.

Does The Walt Disney Company give law enforcement and other govt TLAs access to their fingerprint database?

No sound from phone speakers on Bluetooth

Up until about a month ago, if I connected my phone via Bluetooth to my car, my phone notifications still made sound on the phone. Then an app called Smart Things seemed to hijack my phone any time I used Bluetooth. Do I need to be using that app to play through the car speakers (which I do with podcasts, etc). I still want my phone to play the notifications.

I did turn off (force stop) Smart Things, but the notifications still aren't playing when on Bluetooth. Any ideas what I've done?

AsyncThread cancelled even is success background opration onPostExecute not called

I found solution error was in the wrong URL of API service it causes that returns HTML 404 instead of JSON


Hi,

i have problem with my application, i have following AsyncTask and even is
executed it goes to onCancelled instead of onPostExecute.
Java:
    private class UserRegisterTask extends AsyncTask<Boolean, Boolean, Boolean> {

        private final String mEmail;
        private final String mPassword;
        private final String mFirstName;
        private final String mLastName;
        private final String mAddress1;
        private final String mAddress2;
        private final String mCity;
        private final String mZip;
        private final String mState;
        private final ProgressDialog dialog;
        private final boolean mNewsletter;

        private boolean registered = false;

        public UserRegisterTask(Activity activity, String email, String password, String firstname, String lastname, String address1, String address2, String city, String zip, String state, boolean newsletter) {
            mEmail = email;
            mPassword = password;
            mFirstName = firstname;
            mLastName = lastname;
            mAddress1 = address1;
            mAddress2 = address2;
            mCity = city;
            mZip = zip;
            mState = state;
            mNewsletter = newsletter;
            dialog = new ProgressDialog(activity);
        }


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            registering = true;
            dialog.setMessage("Registruji a přihlašuji, prosím čekejte...");
            dialog.setCanceledOnTouchOutside(false);
            dialog.show();
        }

        @Override
        protected void onCancelled() {
            super.onCancelled();
            dialog.dismiss();
            registering = false;
            Toast.makeText(getBaseContext(), R.string.registration_cancelled, Toast.LENGTH_SHORT).show();
        }

        @Override
        protected Boolean doInBackground(Boolean... booleans) {
            registered = authService.register(mFirstName, mLastName, mEmail, mPassword, mCity, mAddress1, mAddress2, mZip, mState, mNewsletter);
            return Boolean.TRUE;
        }

        @Override
        protected void onPostExecute(Boolean success) {
            super.onPostExecute(success);
            dialog.dismiss();

            if (success && registered) {
                Toast.makeText(getBaseContext(), R.string.registration_success, Toast.LENGTH_LONG).show();
                redirectToHomepage();
            } else {
                email.setError(getString(R.string.email_is_already_registered));
                email.requestFocus();
            }
        }
    }

Runs slow

The Settings >> Battery menu is pretty basic but it's a good way to keep track of what apps/processes are running in the background too, and it's functionality that's already a part of your Android OS. If you want something more elaborate, try installing the Glasswire Data Usage app:
https://play.google.com/store/apps/details?id=com.glasswire.android&hl=en_US
Note that it's also a multi-function utility so look into the app's Settings menu to disable what you don't want or need.

Emulator Problem

Tony,
I was on lesson 10. Java Arrays but I backed up trying to see if I screwed up. I'm going through #6 now trying to ignore my inability to make my project respond like the instructor's project. Don't worry I'll hang in there trying to get what I can out of the course. Thanks for your efforts and comments. I'm concurrently learning B4A app code. That, I've been working on for a year.

Is Spotify using up my Data?

Of course! It will use your data when you are listening to Spotify music, especially for you are using a free account.
Although it's a free streaming music service, it only allows you to listen to them online.
If you subscribed to its premium plan, you can download them offline when you are under WiFi connection.
Don't want to pay for that, you can choose to install AudFree Spotify Music Converter to download and convert Spotify music offline and then it's also available for using Spotify without using data.

Filter

Back
Top Bottom