【问题标题】:Retrieving Entity from Google App Engine backend to Android App从 Google App Engine 后端检索实体到 Android 应用
【发布时间】:2014-02-13 08:50:10
【问题描述】:

我关注了tutorial,了解如何为移动设备构建后端,并且能够为我的 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;
    }
}

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

【问题讨论】:

  • 你能发布你的用户实体源代码吗?
  • 还有你的 Userendpoint getUser 方法
  • @gomino 请找这两个here
  • 好的,现在,您是要加载所有用户,还是只加载一个?
  • 我正在尝试加载登录到我的应用程序的用户。并以“个人资料页面”的形式显示详细信息

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


【解决方案1】:

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

}

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

    • 嗨。对不起,但它仍然不起作用。我应该提醒你,虽然方法getUsername(),getAge()等都属于UserEndpoint类。请你解释一下进一步我如何从服务器创建一个实例?第二部分呢?我对此很陌生。
    • 好吧,忘记我回答的第二部分。这只是最佳实践,在您的情况下并不重要。我认为您的代码应该如下所示:gist.github.com/TomTasche/e574b4d98269f6533405 - 不要忘记在第 14 行替换用户名。
    • 在您的示例中,您使用 user.getAge() 等而不创建用户。
    • 大声笑。你不是说user 到result 吗?我想通了,就这么做了,没有运气。谢谢你的努力。
    • 你在主线程中做 UI 的事情是对的。你关于返回用户对象并在 onPostExecute() 中使用它的建议也是正确的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-10
    • 1970-01-01
    • 2012-08-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多