Google Cloud Endpoints,Objectify和Persistence

时间:2014-07-21 14:40:47

标签: android google-app-engine google-cloud-endpoints objectify google-cloud-datastore

我为我的项目(https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial)关注了这个tuto,一切正常。

但是当我想在我的实体中使用objectify类时,它不起作用,我无法使用我的Android应用程序在我的数据存储区中检索数据!

例如,使用该代码可以,我可以从我的Android应用程序中恢复用户:

import javax.jdo.annotations.Index;
import javax.persistence.Entity;
import javax.persistence.Id;

@Entity
@Index
public class Utilisateur {

    //Nos variables de classes
    @Id String num_portable; //Clé pour notre entité NOTION CLE PRIMAIRE String car c'est à nous de le définir.
    Boolean sexe;
    int date_naissance;
    String position_geo;
    String liste_contact;
    Boolean blacklistage;

    //Constructeur par défaut (Obligatoire pour Objectify)
    private Utilisateur(){}

    public Utilisateur (String num_portable, Boolean sexe, int date_naissance, String position_geo, String liste_contact, Boolean blacklistage) {

        this.num_portable=num_portable;
        this.sexe=sexe;
        this.date_naissance=date_naissance;
        this.position_geo=position_geo;
        this.liste_contact=liste_contact;
        this.blacklistage=blacklistage;                
    }

    public String getNum_portable() {
        return num_portable;
    }



}

但我不使用Objectify ..当我想使用Objectify时,使用该代码,它不起作用: 我只是替换这段代码:

import javax.jdo.annotations.Index;
import javax.persistence.Entity;
import javax.persistence.Id;

有了这个:

import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;

我在我的应用引擎上遇到了这个错误:

com.google.api.server.spi.SystemService invokeServiceMethod: Class Utilisateur for query has not been resolved. Check the query and any imports/aliases specification
javax.persistence.PersistenceException: Class Utilisateur for query has not been resolved. Check the query and any imports/aliases specification

所以我的问题是:我们可以将Objectify与Android App一起使用,还是数据持久性不能与它一起使用(和App Engine)?

编辑:这是我的Android应用上调用后端的代码(与此处相同的代码=> https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial):

public class JeveuxvoirActivity extends Activity {


      private ListView utilisateursList;
      private List<Utilisateur> utilisateurs = null;

    //Création de notre activité
      public void onCreate(Bundle savedInstanceState) {

            //Création de notre interface graphique
            super.onCreate(savedInstanceState);

            //Remove title bar
            this.requestWindowFeature(Window.FEATURE_NO_TITLE);
            //On définit le Layout de notre activité
            setContentView(R.layout.activity_jeveuxvoir);

            utilisateursList = (ListView) findViewById(R.id.list_principal_user);

            // start retrieving the list of nearby places
            new ListOfUtilisateursAsyncRetriever().execute();



        }    


        //*******************************************************
        //                  FONCTIONS ASYNC
        //*******************************************************
      /**
       * AsyncTask for retrieving the list of places (e.g., stores) and updating the
       * corresponding results list.
       */
      private class ListOfUtilisateursAsyncRetriever extends AsyncTask<Void, Void, CollectionResponseUtilisateur> {

        @Override
        protected CollectionResponseUtilisateur doInBackground(Void... params) {


          Utilisateurendpoint.Builder endpointBuilder = new Utilisateurendpoint.Builder(
              AndroidHttp.newCompatibleTransport(), new JacksonFactory(), null);

          endpointBuilder = CloudEndpointUtils.updateBuilder(endpointBuilder);


          CollectionResponseUtilisateur result;

          Utilisateurendpoint endpoint = endpointBuilder.build();

          try {
            result = endpoint.listUtilisateur().execute();
          } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            result = null;
          }
          return result;
        }

        @Override
        @SuppressWarnings("null")
        protected void onPostExecute(CollectionResponseUtilisateur result) {
          ListAdapter utilisateursListAdapter = createUtilisateurListAdapter(result.getItems());
          utilisateursList.setAdapter(utilisateursListAdapter);

          utilisateurs = result.getItems();
        }
      }

        private ListAdapter createUtilisateurListAdapter(List<Utilisateur> utilisateurs) {

            List<Map<String, Object>> data = new ArrayList<Map<String, Object>>();
            for (Utilisateur utilisateur : utilisateurs) {
              Map<String, Object> map = new HashMap<String, Object>();
              map.put("utilisateurIcon", R.drawable.ic_launcher);
              map.put("utilisateurPort", utilisateur.getNumPortable());
              data.add(map);
            }

            SimpleAdapter adapter = new SimpleAdapter(JeveuxvoirActivity.this, data, R.layout.utilisateur_item,
                new String[] {"utilisateurIcon", "utilisateurPort"},
                new int[] {R.id.utilisateur_Icon, R.id.utilisateur_port});

            return adapter;
          }

1 个答案:

答案 0 :(得分:1)

JPA和objectify是连接到dataStore的两种选择。避免一起使用。如果单独使用,两者都有自己的约定。