javaandroidsyncano

Android - Syncano SDK not working


I am developing an android application that uses syncano to send data to a syncano server but the data refuses to send. We have verified that the conditions for our send work, but it does not work. Here is the activity that is used to send the data:

package com.crash.beacon;

import android.content.Context;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import com.crash.beacon.Book;
import com.syncano.library.Syncano;
import com.syncano.library.api.Response;


public class MainActivity extends AppCompatActivity implements SensorEventListener, LocationListener {
    boolean isEnabled = false;
    Button btn;
    private Syncano syncano;
    String[] listItems = new String[]{"Home", "About", "Website"};
    float lat = 0, lng = 0, alt = 0;
    Sensor accel;
    SensorManager manager;
    float x = 0, y = 0, z = 0;
    DrawerLayout dl;
    private ActionBarDrawerToggle mDrawerToggle;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn = (Button)findViewById(R.id.crash);
        syncano = new Syncano("api_key", "late-surf-9471");
        gui(isEnabled);
        manager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        accel = manager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

        // Get the location manager
        LocationManager locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        // Define the criteria how to select the locatioin provider -> use
        // default
        Criteria criteria = new Criteria();
        String provider = locationManager.getBestProvider(criteria, false);

        Location location = locationManager.getLastKnownLocation(provider);

        // Initialize the location fields
        if (location != null) {
           onLocationChanged(location);
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
    public void onCrash(View view){
          isEnabled = !isEnabled;
          gui(isEnabled);



    }
    public void gui(boolean state){
        if(state){
            btn.setText("Enabled");
        }
        else{
            btn.setText("Disabled");
        }
    }

    @Override
    protected void onResume(){
        super.onResume();
        manager.registerListener(this, accel, SensorManager.SENSOR_DELAY_NORMAL);

    }
    @Override
    protected void onPause(){
        super.onPause();
        manager.unregisterListener(this);
    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        x = event.values[0];
        y = event.values[1];
        z = event.values[2];
        //Demo values
        //For actual deployment, use lat, alt, and lng variables
        float hLat = 39, hLng = -75, hAlt = 25;
        final Book book = new Book();
        book.latitude = lat;
        book.longitutde = lng;
        book.altitude = alt;
        book.force_x = x;
        book.force_y = y;
        book.force_y = z;


        if(isEnabled){
            if(Math.abs(x) > 20 && Math.abs(y) > 20 && Math.abs(z) > 20){
            new Network(book, syncano).execute();
            }
        }
    }

    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {

    }
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public void onLocationChanged(Location location) {
        lat = (float)location.getLatitude();
        lng = (float)location.getLongitude();
        alt = (float)location.getAltitude();
    }

    @Override
    public void onStatusChanged(String provider, int status, Bundle extras) {

    }

    @Override
    public void onProviderEnabled(String provider) {

    }

    @Override
    public void onProviderDisabled(String provider) {

    }
}

Network.java

package com.crash.beacon;

import android.os.AsyncTask;
import android.util.Log;

import com.syncano.library.Syncano;
import com.syncano.library.api.Response;

import java.util.List;

/**
 * Created by Tienvo on 11/8/2015.
 */
public class Network extends AsyncTask<String, Void, String> {
    Book b;
    Syncano syn;
    public Network(Book book, Syncano syncano){
        this.b = book;
        this.syn = syncano;
    }
    @Override
    protected String doInBackground(String... params) {
       //Response<Book> responseCreateObject = syn.createObject(b).send();
        Log.d("code", String.valueOf(Response.CODE_SUCCESS));
        Response<List<Book>> responseGetBooks = syn.createObject(b).send();
        return "Executed";
    }
}

Book.java

package com.crash.beacon;

import com.syncano.library.annotation.SyncanoClass;
import com.syncano.library.annotation.SyncanoField;
import com.syncano.library.data.SyncanoObject;
/**
 * Created by tom on 11/7/15.
 */
@SyncanoClass(name = "ltds")
public class Book extends SyncanoObject{
    public static final String FIELD_LAT = "latitude";
    public static final String FIELD_LNG = "longitude";
    public static final String FIELD_ALT = "altitude";
    public static final String FIELD_X =   "force_x";
    public static final String FIELD_Y =   "force_y";
    public static final String FIELD_Z =   "force_z";

    @SyncanoField(name = FIELD_LAT)
    public float latitude;

    @SyncanoField(name = FIELD_LNG)
    public float longitutde;

    @SyncanoField(name = FIELD_ALT)
    public float altitude;

    @SyncanoField(name = FIELD_X)
    public float force_x;

    @SyncanoField(name = FIELD_Y)
    public float force_y;

    @SyncanoField(name = FIELD_Z)
    public float force_z;


}

Solution

  • That's how your sending method may look like.
    When you create an object, your result code should be HTTP_CODE_CREATED 201 and response type Response<Book>. When you download objects, your response code should be HTTP_CODE_SUCCESS 200 and response type Response<List<Book>>

        @Override
        protected String doInBackground(String... params) {
            // send object to server
            Response<Book> responseCreateObject = syn.createObject(b).send();
            if (responseCreateObject.getHttpResultCode() == Response.HTTP_CODE_CREATED) {
                Log.d(TAG, "Success. Item created.");
            } else {
                Log.d(TAG, "Fail.");
            }
    
            // get objects from server
            Response<List<Book>> responseGetObjects = syn.getObjects(Book.class).send();
            if (responseGetObjects.getHttpResultCode() == Response.HTTP_CODE_SUCCESS) {
                List<Book> books = responseGetObjects.getData();
                Log.d(TAG, "Success. Items downloaded: " + books.size());
            } else {
                Log.d(TAG, "Fail.");
            }
    
            return "Executed";
        }
    

    I simplified your code a little, to focus on Syncano connection and it works! It creates an object on your Syncano instance.

    package com.chimeraprime.syncano4libcheck;
    
    import android.os.AsyncTask;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.util.Log;
    import android.view.View;
    import android.widget.Button;
    
    import com.syncano.library.Syncano;
    import com.syncano.library.annotation.SyncanoClass;
    import com.syncano.library.annotation.SyncanoField;
    import com.syncano.library.api.Response;
    import com.syncano.library.data.SyncanoObject;
    
    import java.util.List;
    import java.util.Random;
    
    
    public class OtherActivity extends AppCompatActivity {
        private Button btn;
        private Syncano syncano;
    
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_other);
    
            syncano = new Syncano("<api_key>", "<instance>");
            btn = (Button) findViewById(R.id.crash);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Random rnd = new Random();
    
                    Book book = new Book();
                    book.latitude = rnd.nextFloat();
                    book.longitutde = rnd.nextFloat();
                    book.altitude = rnd.nextFloat();
                    book.force_x = rnd.nextFloat();
                    book.force_y = rnd.nextFloat();
                    book.force_y = rnd.nextFloat();
    
                    new Network(book, syncano).execute();
                }
            });
        }
    
    
        public static class Network extends AsyncTask<String, Void, String> {
            Book b;
            Syncano syn;
            String TAG = Network.class.getSimpleName();
    
            public Network(Book book, Syncano syncano) {
                this.b = book;
                this.syn = syncano;
            }
    
            @Override
            protected String doInBackground(String... params) {
                // send object to server
                Response<Book> responseCreateObject = syn.createObject(b).send();
                if (responseCreateObject.getHttpResultCode() == Response.HTTP_CODE_CREATED) {
                    Log.d(TAG, "Success. Item created.");
                } else {
                    Log.d(TAG, "Fail.");
                }
    
                // get objects from server
                Response<List<Book>> responseGetObjects = syn.getObjects(Book.class).send();
                if (responseGetObjects.getHttpResultCode() == Response.HTTP_CODE_SUCCESS) {
                    List<Book> books = responseGetObjects.getData();
                    Log.d(TAG, "Success. Items downloaded: " + books.size());
                } else {
                    Log.d(TAG, "Fail.");
                }
    
                return "Executed";
            }
        }
    
        @SyncanoClass(name = "ltds")
        public class Book extends SyncanoObject {
            public static final String FIELD_LAT = "latitude";
            public static final String FIELD_LNG = "longitude";
            public static final String FIELD_ALT = "altitude";
            public static final String FIELD_X = "force_x";
            public static final String FIELD_Y = "force_y";
            public static final String FIELD_Z = "force_z";
    
            @SyncanoField(name = FIELD_LAT)
            public float latitude;
    
            @SyncanoField(name = FIELD_LNG)
            public float longitutde;
    
            @SyncanoField(name = FIELD_ALT)
            public float altitude;
    
            @SyncanoField(name = FIELD_X)
            public float force_x;
    
            @SyncanoField(name = FIELD_Y)
            public float force_y;
    
            @SyncanoField(name = FIELD_Z)
            public float force_z;
        }
    }