Java 从Google应用程序引擎后端检索实体到Android应用程序

Java 从Google应用程序引擎后端检索实体到Android应用程序,java,android,google-app-engine,google-cloud-endpoints,Java,Android,Google App Engine,Google Cloud Endpoints,我遵循了关于如何为移动设备构建后端的说明,并能够为我的Android应用程序创建后端并在GAE上存储实体。问题是,我不知道如何检索实体的属性。要存储实体,我使用了以下代码: public class EndpointsTask extends AsyncTask<Context, Integer, Long> { protected Long doInBackground(Context... contexts) { Userendpoint.Builder

我遵循了关于如何为移动设备构建后端的说明,并能够为我的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;
    }
}
然后调用newendpointstask.executegetApplicationContext;在我的onCreate方法中。当我尝试运行应用程序时,我什么都没有得到。 我花了几个小时寻找如何做到这一点,但我只找到了关于节省实体的教程。
任何帮助都将不胜感激。

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

编辑:像这样:


此外,在主线程中进行UI操作是最佳实践。因此,您应该返回用户结果,并在onPostExecuted中使用它来填充文本视图。

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

编辑:像这样:


此外,在主线程中进行UI操作是最佳实践。因此,您应该返回用户结果并在onPostExecuted中使用它来填充您的文本视图。

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

}

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

}

您好。很抱歉,它仍然不起作用。但是我应该提醒您,getUsername、getAge等方法都属于UserEndpoint类。您能进一步解释一下我如何从服务器创建实例吗?第二部分呢?我对这个很陌生。好吧,忘了我回答的第二部分。这只是最佳实践,对你来说并不重要。下面是我认为您的代码应该是这样的:-不要忘记替换第14行的用户名。在您的示例中,您使用user.getAge等,但没有创建user.lol。您不是指结果为user吗?我想出来了,就这么做了,不走运。不过谢谢你的努力。你在主线程中做UI的事情是对的。你关于返回用户对象并在onPostExecute中使用它的建议也是正确的。嗨。很抱歉,但它仍然不起作用。我应该提醒你,方法getUsername,getAge等都属于UserEndpoint类。能否请您进一步解释如何从服务器创建实例?第二部分呢?我对这个很陌生。好吧,忘了我回答的第二部分。这只是最佳实践,对你来说并不重要。下面是我认为您的代码应该是这样的:-不要忘记替换第14行的用户名。在您的示例中,您使用user.getAge等,但没有创建user.lol。您不是指结果为user吗?我想出来了就这么做了,不走运。不过谢谢你的努力。你在主线程中做UI的事情是对的。你关于返回用户对象并在onPostExecute中使用它的建议也是正确的。你能发布你的用户实体源代码吗?还有你的Userendpoint getUsermethod@gomino请找到这两个好的,现在,您正在尝试加载所有用户,还是仅加载一个用户?我正在尝试加载登录到我的应用程序的用户。并以配置文件页面的形式显示详细信息。请发布您的用户实体源代码?以及您的Userendpoint getUsermethod@gomino请找到这两个Ok,那么现在,您是否正在尝试加载所有用户,或者只有一个?我正在尝试加载登录到我的应用程序的用户,并以配置文件页面的形式显示详细信息