Giter VIP home page Giter VIP logo

deardiary-android's Introduction

Dear Diary BaasBox Sample

BaasBox is a tool that allows you to quickly build a back end for your app. You can install it on your local machine (ideal for development and testing) or your own server. It comes with an SDK that simplifies the integration of your Android app with the server. In this tutorial you will learn how to integrate an existing Android application with BaasBox.

To complete this walk through you need a working copy of the Android SDK. You can download the starter project from DearDiary-Android-Starter.

Unzip and run the project to see if it works as expected, it's a simple personal diary composed of three activities, a list of your notes, the details, and an activity to add new notes to the diary.

Even though this pretty simple app works, it has a big shortcoming, if you uninstall it from your device, you will loose all your notes, and there is no way to restore them.

Wouldn’t it be cool if we could save them on a back end? That’s exactly what we are going to do in this tutorial.

Installing BaasBox

The first step is to install BaasBox. For sake of simplicity we will show how to install it on a local machine. You will see that’s very easy. Download the latest version of BaasBox from here. Unzip the file, open Terminal, go to the directory unzipped, type “./start” and hit return. BaasBox is now running on your local machine. To test visit the following link: http://localhost:9000/ and you should see the following screen.

Importing the sdk

If you are on eclipse you can download the sdk from here. Put the jar in to your libs folder and you are ready to go. Using gradle is even simpler you just need to add the following line to your dependencies

compile 'com.baasbox:baasbox-android:0.7.3-alpha'

After refreshing your ide, you should be able to use the sdk classes in your project

Setup the client library

The BaasBox service could be installed anywhere on the web so you probably need to configure it a bit. We suggest to initialize the client in your Application onCreate method. The starter project includes an empty DearDiary class. Open the java file and add the following lines:

    private BaasBox box;

    @Override
    public void onCreate() {
        super.onCreate();
        BaasBox.Config config = new BaasBox.Config();
        config.API_DOMAIN = "10.0.0.2"; // the host address
        config.APP_CODE = "1234567890"; // your appcode
        config.HTTP_PORT = 9000; // your port

        box = BaasBox.initDefault(this,config);
    }

Also don't forget to add the required internet permissions to your manifest:

<uses-permission android:name="android.permission.INTERNET"/>

After all you are going to connect to a backend!!!

With this setup you are ready to use the sdk in your app. Don't forget to customize the configuration to adapt it to your environment.

Authenticate with the server

BaasBox provides means for authenticatication, so that different user can be distinguished on the backed. The sample project includes a barebone LoginActivity, we are going to integrate it in the app.

The first thing to do is to check if there is alrady an an authenticated user for this device. Open the NoteListActivity file and right at the start of onCreate, where is the todo 1 mark add the following lines.

if (BaasUser.current() == null){
            startLoginScreen();
            return;
}

BaasBox sdk remembers the currently logged in user. java BaasUser.current() will return the current one or null if no one is logged in, in this case we will start the login screen.

Now in the LoginActivity search for the two todo items for you. This is your first call to BaasBox:

private void signupWithBaasBox(boolean newUser){
        //todo 3.1
        BaasUser user = BaasUser.withUserName(mUsername);
        user.setPassword(mPassword);
        if (newUser) {
            user.signup(onComplete);
        } else {
            user.login(onComplete);
        }
    }
//todo 3.2
private final BaasHandler<BaasUser> onComplete =
    new BaasHandler<BaasUser>() {
        @Override
        public void handle(BaasResult<BaasUser> result) {
            if(result.isSuccess()){
                completeLogin(true);
            } else {
                Log.d(TAG,result.error().toString());
                completeLogin(false);
            }
        }
    };

you can login or signup using methods of the BaasUser class,they obviously are signup and login. You obtain an instance of a user through the factory method:

BaasUser.withUserName(...);

These methods are executed asynchronously so you need to pass a callback to be invoked upon completion, in this case the onComplete variable. The callback will receive the result of the rest api call, or possibly an error you can inspect docs of the methods of BaasResult to see how it works in details.

deardiary-android's People

Contributors

eliantor avatar ruilcsper avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

deardiary-android's Issues

Download/Retrive Files from Baasbox console

I am using Baasbox Files retrieving code as below as per code given on the official site for files retrieving-

// load file in memory
BaasFile file = ...;
file.stream(new BaasHandler() {
@OverRide
public void handle(BaasResult res) {
if ( res.isSuccess() ) {
byte[] data = res.value().getData();
Log.d("LOG","File received");
} else {
Log.e("LOG","Error while streaming",res.error());
}
}
});

I am getting trouble while accessing BaasFile file = ...; , I am using BaasFile file = new BaasFile();

after at the time of getting file.stream it throws me error says "ID can not be null" , then I am using file.stream("ef2162d9-919c-48f8-b76f-13e25ab25250",new BaasHandler() { ....}; but it still trows same error,

So can you please guide me How can I retreive files from Baasbox.

Not able to connect to Openshift Host server

I am using baasbox on Android sdk,
I have host baasbox on Openshift server

The problem is - DearDiary not able to connect to Openshift.
I am trying using below code

BaasBox.Config config;
config = new BaasBox.Config();
config.authenticationType= BaasBox.Config.AuthType.BASIC_AUTHENTICATION;
config.apiDomain = "baasbox-myapp.rhcloud.com";
I am getting below error
04-24 10:26:15.277: E/STEPPER(992): will commit now
04-24 10:26:15.277: E/STEPPER(992): last transition
04-24 10:26:15.277: E/STEPPER(992): handling
04-24 10:26:15.277: E/LOG(992): Show error
04-24 10:26:15.277: E/LOG(992): com.baasbox.android.BaasIOException: java.net.SocketTimeoutException: failed to connect to baasbox-baasbox.rhcloud.com/23.20.44.115 (port 9000) after 6000ms

Kindly please guide on my issue,
Thanks

Error when running the project

Hi,

I have this error when running the DearDiary project.
Please update the project for baasbox 0.9

04-07 23:22:01.790 15021-15021/com.baasbox.deardiary D/dalvikvm﹕ Late-enabling CheckJNI
04-07 23:22:02.090 15021-15021/com.baasbox.deardiary D/ActivityThread﹕ setTargetHeapUtilization:0.25
04-07 23:22:02.090 15021-15021/com.baasbox.deardiary D/ActivityThread﹕ setTargetHeapIdealFree:8388608
04-07 23:22:02.090 15021-15021/com.baasbox.deardiary D/ActivityThread﹕ setTargetHeapConcurrentStart:2097152
04-07 23:22:02.450 15021-15021/com.baasbox.deardiary D/AndroidRuntime﹕ Shutting down VM
04-07 23:22:02.450 15021-15021/com.baasbox.deardiary W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x411a0438)
04-07 23:22:02.470 15021-15021/com.baasbox.deardiary E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.baasbox.deardiary/com.baasbox.deardiary.ui.NoteListActivity}: java.lang.IllegalStateException: Trying to use implicit client, but no default initialized
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2114)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
at android.app.ActivityThread.access$700(ActivityThread.java:143)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4963)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.IllegalStateException: Trying to use implicit client, but no default initialized
at com.baasbox.android.BaasBox.getDefaultChecked(BaasBox.java:177)
at com.baasbox.android.BaasUser.current(BaasUser.java:200)
at com.baasbox.deardiary.ui.NoteListActivity.onCreate(NoteListActivity.java:39)
at android.app.Activity.performCreate(Activity.java:5184)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2078)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2139)
            at android.app.ActivityThread.access$700(ActivityThread.java:143)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1241)
            at android.os.Handler.dispatchMessage(Handler.java:99)
            at android.os.Looper.loop(Looper.java:137)
            at android.app.ActivityThread.main(ActivityThread.java:4963)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:511)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
            at dalvik.system.NativeStart.main(Native Method)
04-07 23:27:14.220 15021-15021/com.baasbox.deardiary I/Process﹕ Sending signal. PID: 15021 SIG: 9

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.