从 Google App Engine 后端检索实体到 Android App



我遵循了有关如何为移动设备构建后端的教程,并能够为我的Android应用程序创建后端并在GAE上存储Enttes。问题是,我不知道如何检索我的实体的属性。为了存储实体,我使用了以下代码:

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;
    }
}

在我的onCreate()方法中调用new EndpointsTask().execute(getApplicationContext());

要检索实体的属性并使用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;
}}

然后在我的onCreate()方法中调用new EndpointsTask().execute(getApplicationContext());。当我尝试运行该应用程序时,我什么也没得到。我花了几个小时寻找如何做到这一点,但我只找到有关保存诱惑的教程。任何帮助将不胜感激。

您的问题是您正在尝试更新后台线程中的 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());
  }
}

行"用户用户 = 新用户()"应替换为"用户结果 = ..."。您正在创建一个新的(可能是空的)用户实例,用于填充 TextViews。您应该改用从服务器获取的实例。

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

此外,最佳做法是在主线程中执行UI操作。所以你应该做的是返回"用户结果",并在onPostExecuted()中使用它来填充你的TextView。

相关内容

最新更新