2014-02-13 76 views
2

我跟随tutorial了解如何为移动设备构建后端,并且能够为我的Android应用程序创建后端并在GAE上存储Entites。问题是,知道如何找回我用下面的代码我entity.To店的entites的属性:从Google App Engine后台检索实体到Android应用程序

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() method.When叫new EndpointsTask().execute(getApplicationContext());我试图运行应用程序,我不没有任何东西。 我花了几个小时寻找如何做到这一点,但我只找到保存实体的教程。 任何帮助,将不胜感激。

+0

您可以发布您的用户实体源代码吗? – Gomino

+0

还你的Userendpoint getUser方法 – Gomino

+0

@gomino请找到这两个[这里](https://gist.github.com/JudeOchalifu/9128082) –

回答

2

你的问题是你正在尝试更新后台线程中的用户界面。在从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 = ...”。你正在创建一个新的(可能是空的)用户实例,用来填充TextViews。您应该使用从服务器获取的实例。

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

而且,它是做UI的东西在主线程中的最佳实践。所以你应该做的是返回“用户结果”,并用onPostExecuted()来填充你的TextView。

+0

嗨,很抱歉,但它仍然不行。我应该提醒你,getUsername(),getAge()等等方法都属于UserEndpoint类。请你进一步解释我可以如何从服务器创建一个实例?第二部分?我对此很新颖。 –

+0

好的,忘了我的答案的第二部分。这只是最好的做法,对你而言并不重要。以下是我认为你的代码应该如下所示:https://gist.github.com/TomTasche/e574b4d98269f6533405 - 不要忘记在#14行替换用户名。 – TomTasche

+0

在你的例子中,你使用'user.getAge()'等而不创建用户。 –

相关问题