我遵循了有关如何为移动设备构建后端的教程 ,并能够为我的Android应用创建后端并将实体存储在GAE上。问题是,我不知道如何检索实体的属性。实体我使用以下代码:
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;
}
}
调用new EndpointsTask().execute(getApplicationContext());
在我的onCreate()
方法中。
要检索实体的属性并使用TextViews
显示它们,这就是我尝试过的方法:
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;
}}
然后调用new EndpointsTask().execute(getApplicationContext());
在我的onCreate()
方法中。当我尝试运行该应用程序时,我什么也没得到。 我花了几个小时寻找方法,但是我只找到有关保存实体的教程。 任何帮助,将不胜感激。
“ User user = new User()”行应替换为“ User result = ...”。 您正在创建一个新的(可能为空)用户实例,用于填充TextViews。 您应该使用从服务器获取的实例。
编辑:像这样: https : //gist.github.com/TomTasche/e574b4d98269f6533405
此外,最佳做法是在主线程中执行UI任务。 因此,您应该做的是返回“用户结果”,并在onPostExecuted()中使用它来填充TextView。
您的问题是您正在尝试在后台线程中更新ui。 从GAE检索用户后,您应将其作为结果传递给在主线程上执行的onPostExecute,然后在那里更新您的UI。 这是您在代码中进行更改所需的快速草稿。
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());
}
}