Reputation: 27255
I followed the tutorial on how to build a backend for mobile devices and was able to create a backend for my Android app and store Entites on GAE.The problem is, i don't know how to retrieve the properties of my entity.To store entites i used the following code:
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
protected Long doInBackground(Context... contexts) {
Userendpoint.Builder endpointBuilder = new Userendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Userendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
User user = new User();
String username;
if (responseCheckbox.isChecked()) {
username = MainActivity.getPersonName();
}
else {
username = usernameTextField.getText().toString();
}
String location = locationTextField.getText().toString();
String tempAge = ageTextField.getText().toString();
String tempWeight = weightTextField.getText().toString();
String gender = genderTextField.getText().toString();
String occupation = occupationTextField.getText().toString();
int age = Integer.parseInt(tempAge);
int weight = Integer.parseInt(tempWeight);
user.setUsername(username);
user.setLocation(location);
user.setAge(age);
user.setWeight(weight);
user.setGender(gender);
user.setOccupation(occupation);
@SuppressWarnings("unused")
User result;
result = endpoint.insertUser(user).execute();
} catch (IOException e) {
e.printStackTrace();
}
return (long) 0;
}
}
with a call to new EndpointsTask().execute(getApplicationContext());
in my onCreate()
method.
To retrieve the properties of the Entity and display them using TextViews
this is what i have tried:
public class EndpointsTask extends AsyncTask<Context, Integer, Long> {
protected Long doInBackground(Context... contexts) {
Userendpoint.Builder endpointBuilder = new Userendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Userendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
try {
User user = new User();
usernameTextView.setText(user.getUsername());
locationTextView.setText(user.getLocation());
ageTextView.setText(user.getAge());
occupationTextView.setText(user.getOccupation());
weightTextView.setText(user.getWeight());
genderTextView.setText(user.getGender());
User result = endpoint.getUser(user.getUsername()).execute();
} catch (Exception e) {
e.printStackTrace();
}
return (long) 0;
}}
and then called new EndpointsTask().execute(getApplicationContext());
in my onCreate()
method.When i tried to run the app, i don't get anything.
I have spent several hours looking for how to do it but i only find tutorials on saving entites.
Any help would be appreciated.
Upvotes: 2
Views: 410
Reputation: 12347
Your problem is that you are trying to update the ui in a background thread. After retrieving the user from GAE, you should pass it as a result to onPostExecute which get executed on the main thread, and then update your UI there. Here is a quick draft of the change you need in your code to make it work.
public class EndpointsTask extends AsyncTask<Context, Integer, User> {
protected User doInBackground(Context... contexts) {
Userendpoint.Builder endpointBuilder = new Userendpoint.Builder(
AndroidHttp.newCompatibleTransport(),
new JacksonFactory(),
new HttpRequestInitializer() {
public void initialize(HttpRequest httpRequest) { }
});
Userendpoint endpoint = CloudEndpointUtils.updateBuilder(
endpointBuilder).build();
User user = null;
try {
user = endpoint.getUser("username").execute();
} catch (Exception e) {
e.printStackTrace();
}
return user;
}
protected void onPostExecute(User user) {
//update your UI here
usernameTextView.setText(user.getUsername());
locationTextView.setText(user.getLocation());
ageTextView.setText(Integer.toString(user.getAge()));
occupationTextView.setText(user.getOccupation());
weightTextView.setText(Integer.toString(user.getWeight()));
genderTextView.setText(user.getGender());
}
}
Upvotes: 2
Reputation: 5526
The line "User user = new User()" should be replaced with " User result = ...". You're creating a new (probably empty) instance of user, which you're using to fill the TextViews. You should use the instance fetched from the server instead.
EDIT: like this: https://gist.github.com/TomTasche/e574b4d98269f6533405
Moreover, it's best practice to do UI stuff in the main thread. So what you should do is return the "User result" and use it in onPostExecuted() to fill your TextView.
Upvotes: 2