0

我正在实现一个托管在 GAE 中的 RESTful 服务,试图将数据保存到数据存储区。

@Path("/db")
public class DBAccess {

    private static final Logger log = Logger.getLogger(DBAccess.class.getName());

    // Allows to insert contextual objects into the class,
    // e.g. ServletContext, Request, Response, UriInfo
    @Context
    UriInfo uriInfo;
    @Context
    Request request;

    @POST
    @Produces(MediaType.TEXT_HTML)
    @Consumes(MediaType.APPLICATION_JSON)
    public void saveDataBase(final List<User> data,
            @Context HttpServletResponse servletResponse,
            @Context HttpServletRequest servletRequest) throws IOException {

        log.info("saveDatabae REST Service has been called.");

        Key dbKey = KeyFactory.createKey("DataBase", "default");

        Entity user;
        DatastoreService datastore = DatastoreServiceFactory
                .getDatastoreService();

        for (int i = 0; i < data.size(); i++) {
            user = new Entity("User", dbKey);
            user.setProperty("idUser", data.get(i).getIdUser());
            user.setProperty("idGMC", data.get(i).getIdGMC());

            datastore.put(user);
        }

        servletResponse.sendRedirect("../index.jsp");
    }
}

另一方面,我有一个 Android 应用程序试图像使用它一样使用它

private static void post(String endpoint, Map<String, String> params)
            throws IOException, JSONException {

        Iterator<Entry<String, String>> iterator = params.entrySet().iterator();

        Log.v(TAG, "Posting params to " + endpoint);

        HttpClient httpClient = new DefaultHttpClient();

        HttpPost post = new HttpPost(endpoint);

        post.setHeader("content-type", "application/json");

        // Build JSON
        JSONObject dato = new JSONObject();

        while (iterator.hasNext()) {
            Entry<String, String> param = iterator.next();
            dato.put(param.getKey(), param.getValue());
        }

        StringEntity entity = new StringEntity(dato.toString());
        post.setEntity(entity);

        HttpResponse resp = httpClient.execute(post);
        String respStr = EntityUtils.toString(resp.getEntity());

        Log.v(TAG, "HttpResponse is " + respStr);

    }

这被称为

Map<String, String> params = new HashMap<String, String>();
params.put("regId", regId);
post("http://pickapp-server.appspot.com/rest/db", params);

在 GAE 管理控制台中,我得到以下日志条目:

在此处输入图像描述

关键是,用户实体永远不会在 GAE 数据存储中创建。我猜 REST 服务没有被调用。谁能告诉我我错过了什么?

4

1 回答 1

1

您的 REST 方法似乎需要一组 JSON 对象,而 Android POST 提供一个 JSON 对象。尝试将其包装在JSONArray.

于 2013-01-04T19:55:42.040 回答