从Google App Engine后端检索实体到Android应用程序

时间:2014-02-13 08:50:11

标签: java android google-app-engine google-cloud-endpoints

我按照tutorial关于如何为移动设备构建后端,并能够为我的Android应用创建后端并在GAE上存储Entites。问题是,我不知道如何检索我的实体的属性。为了存储entites,我使用了以下代码:

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()。当我尝试运行该应用时,我什么都没得到。 我花了几个小时寻找如何做到这一点,但我只找到了关于保存entites的教程。 任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:2)

“User user = new User()”行应替换为“User result = ...”。您正在创建一个新的(可能是空的)用户实例,您将用它来填充TextView。您应该使用从服务器获取的实例。

编辑:像这样:https://gist.github.com/TomTasche/e574b4d98269f6533405

此外,最好在主线程中执行UI内容。所以你应该做的是返回“用户结果”并在onPostExecuted()中使用它来填充你的TextView。

答案 1 :(得分:2)

您的问题是您正在尝试在后台线程中更新ui。从GAE检索用户后,您应将其作为结果传递给onPostExecute,该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());
  }

}