2016-03-27 154 views
0

我一直在尝试使用Google云端点为我的应用设置后端,并且在我实际尝试将实体插入数据存储区之前,一切似乎都进展顺利。Google云端点插入无法从客户端运行

我想使用生成的端点将GenericBike对象插入到数据存储中,并且代码编译并似乎成功运行时,在检查项目网页时没有实际插入任何内容。

这是我实际插入的代码。

private class AddGenericBikeAsyncTask extends AsyncTask<GenericBike, Void, Void> { 
    @Override 
    protected Void doInBackground(GenericBike... params) { 
     GenericBikeApi.Builder builder = new GenericBikeApi.Builder(AndroidHttp.newCompatibleTransport(), 
       new AndroidJsonFactory(), null) 
       .setRootUrl("https://banded-coder-125919.appspot.com/_ah/api/"); 
     GenericBikeApi service = builder.build(); 
     try { 
      GenericBike bike = params[0]; 
      Log.e("Debug", "Inserting bike..."); 
      service.insert(bike); 
      Log.e("Debug", "Done inserting bike."); 
     } catch (IOException e) {e.printStackTrace(); } 
     return null; 
    } 
} 

而这里就是我称之为

if (mGenericBikes == null) { // we need to preload the database still 
     for (int i=0; i<10; i++) { 
      GenericBike bike = new GenericBike(); 
      bike.setId(new Long(i+1)); 
      new AddGenericBikeAsyncTask().execute(bike); 
     } 
    } 

万一有帮助,这是我的GenericBike entitiy。

@Entity 
public class GenericBike { 

@Id Long mId; 
boolean mAtStation; 

public GenericBike() { 
    mAtStation = true; 
} 

public Long getId() { 
    return mId; 
} 

public void setId(Long id) { 
    mId = id; 
} 

public boolean isAtStation() { 
    return mAtStation; 
} 

public void setAtStation(boolean atStation) { 
    mAtStation = atStation; 
} 

编辑:下面是insert()方法生成的端点代码

/** 
* Inserts a new {@code GenericBike}. 
*/ 
@ApiMethod(
     name = "insert", 
     path = "genericBike", 
     httpMethod = ApiMethod.HttpMethod.POST) 
public GenericBike insert(GenericBike genericBike) { 
    // Typically in a RESTful API a POST does not have a known ID (assuming the ID is used in the resource path). 
    // You should validate that genericBike.mId has not been set. If the ID type is not supported by the 
    // Objectify ID generator, e.g. long or String, then you should generate the unique ID yourself prior to saving. 
    // 
    // If your client provides the ID then you should probably use PUT instead. 
    ofy().save().entity(genericBike).now(); 
    logger.info("Created GenericBike."); 

    return ofy().load().entity(genericBike).now(); 
} 
+0

小心描述问题? – Gendarme

+0

@Gendarme,对不起,我的大脑现在有点油炸了。我试图使用端点将GenericBike对象插入到数据存储中,并且在代码编译并似乎成功运行时,在检查项目网页时没有实际插入任何内容。 – noblera

+0

只有当您发布“GenericBikeApi.Insert”方法代码时,我们才能弄明白。 –

回答

1

看起来你是不是从启动您的客户端请求。我相信您需要将.execute()添加到您的客户端lib对象以实际启动请求。

service.insert(bike).execute(); 

而且取代

service.insert(bike); 

,检查你的App Engine纪录是一个很好的起点,以确认该请求实际上经历。

+0

这就是问题所在。我不确定我是如何错过它的,因为我使用list()方法正确地做了它......噢,非常感谢你。你为我节省了很多头痛。 – noblera