简体   繁体   English

Google Cloud端点,对象化和持久性

[英]Google Cloud Endpoints, Objectify and Persistence

I've follow this tuto for my project ( https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial ), and everything works fine. 对于我的项目,我已经遵循了此教程( https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial ),并且一切正常。

But when I wanted to use with objectify classes for my entities, It doesn't work, i can't retrieve data on my datastore with my android app ! 但是,当我想为我的实体使用对象化类时,它不起作用,我无法使用我的Android应用程序检索数据存储中的数据!

For example, with that code it's okay, I can get my user back from my android app: 例如,使用该代码就可以了,我可以从我的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;
    }



}

But I don't use Objectify.. When I want to use Objectify, with that code, it doesn't works : I'm just replace this code : 但是我不使用Objectify。当我想使用Objectify时,该代码不起作用:我只是替换此代码:

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

With this one : 有了这个:

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

and I get this error on my app engine: 我在我的应用程序引擎上收到此错误:

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

So my question is: Can we use Objectify with Android App or the data persistence doesnt work with it (And App Engine)? 所以我的问题是:我们可以在Android App中使用Objectify还是数据持久性不适用于它(以及App Engine)?

Edit: That is my code on my android app that call backend(Same code as here => https://cloud.google.com/developers/articles/how-to-build-mobile-app-with-app-engine-backend-tutorial ): 编辑:这是我在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;
          }

JPA and objectify are 2 alternatives for connecting to dataStore. JPA和objectify是用于连接到dataStore的2个替代方法。 Avoid using both together. 避免同时使用两者。 Both have their own conventions that work best if used alone. 两者都有自己的约定,如果单独使用,它们的效果最佳。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM