2015-11-14 126 views
4

我正在開發一個使用syncano將數據發送到syncano服務器但拒絕發送數據的android應用程序。我們已經驗證了我們發送工作的條件,但它不起作用。下面是用於發送數據的活動:那怎麼你的發送方法可能看起來像Android - Syncano SDK不起作用

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; 


} 

回答

4


創建對象時,結果代碼應爲HTTP_CODE_CREATED 201,響應類型爲Response<Book>。 當您下載的對象,你的響應代碼應該是HTTP_CODE_SUCCESS 200和響應型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"; 
    } 

我簡化你的代碼一點,把重點放在Syncano連接和它的作品!它在你的Syncano實例上創建一個對象。

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; 
    } 
} 
+1

@ atf1999共享API密鑰不'忽略ACL'設置是安全的 - 但你永遠不應該分享您的管理員帳戶的關鍵 - 這一個允許修改整個帳戶,改變計費信息等。 用戶登錄後沒有將API密鑰與用戶密鑰組合在一起,任何人都無法做任何事情。 但我同意,作爲一項規則,不要在SO上共享密鑰以防萬一。 –

+0

我沒有意識到我已經添加了密鑰 – atf1999

相關問題