down votefavorite
<http://stackoverflow.com/questions/35371983/retrieving-entities-of-google-datastore-in-android-studio-with-google-endpoints#>
I managed to add an App Engine Java Endpoints Module to my Android project.
Also, as described in the documentation, I added an EndpointsAsyncTask to
my app module. I created a project in the google cloud console and set the
RootUrl of the ApiBuilder to my appspot.com adress of that project.
public class EndpointsAsyncTask extends AsyncTask<Pair<Context, String>, Void,
String> {private static MyApi myApiService = null;private Context context;
@Overrideprotected String doInBackground(Pair<Context, String>... params) {
if(myApiService == null) { // Only do this once
MyApi.Builder builder = new
MyApi.Builder(AndroidHttp.newCompatibleTransport(), new AndroidJsonFactory(),
null)
.setRootUrl("https://myapp.appspot.com/_ah/api/");
myApiService = builder.build();
}
context = params[0].first;
String name = params[0].second;
try {
return myApiService.sayHi(name).execute().getData();
} catch (IOException e) {
return e.getMessage();
}}
@Overrideprotected void onPostExecute(String result) {
Toast.makeText(context, result, Toast.LENGTH_LONG).show();} }
I deployed the whole module. Working with the default ApiMethod @ApiMethod(name
= "sayHi")works without problems. After adding new
EndpointsAsyncTask().execute(new Pair<Context,
String>(getApplicationContext(), "Manfred")); to my MainActivity my app
starts and shows the right Toast.
Because of that I thought my connection to google cloud is working fine. I
opened the google cloud console, went to datastore and added two entities
of kind "Episode" with some string properties.
I now want to receive both entities within my app. While debugging I just
want to show a property of them in the Toast. That's why I tried to modify
the default ApiMethod @ApiMethod(name = "sayHi"). Instead of returning "hi"
plus the name parameter, it shall return the value of a single property of
my entity. Here are some of my tries:
public class MyEndpoint {
/**
* A simple endpoint method that takes a name and says Hi back
*/@ApiMethod(name = "sayHi")public MyBean sayHi(@Named("name") String name) {
DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
//Key key= KeyFactory.createKey("Episode", "F001");
Query q = new Query("Episode");
PreparedQuery pq = datastore.prepare(q);
MyBean response = new MyBean();
String test = null;
for (Entity result : pq.asIterable()) {
test = result.toString();
}
response.setData("Hi, " + test);
return response;}
}
My variable "test" stays null. Also every call of .get(Key) returns an
exception, saying that there is no entity with the given Key.
I am missing something serious. So what am I doing wrong? Is it right to
get the entities via an ApiMethod or can I get them another way? *How can I
get a value of a property of a entities of my datastore in my app?*
--
You received this message because you are subscribed to the Google Groups
"Android Developers" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/android-developers.
To view this discussion on the web visit
https://groups.google.com/d/msgid/android-developers/113dccd9-982a-48b1-ab51-7717a4970e5d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.