【问题标题】:Can't consume REST service from Android with HttpClient无法使用 HttpClient 从 Android 使用 REST 服务
【发布时间】:2013-01-04 16:47:01
【问题描述】:

我正在实现一个托管在 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 服务没有被调用。谁能告诉我我错过了什么?

【问题讨论】:

  • 很难看到,但看起来您的服务正在生成 400 响应。因此,您的服务正在被调用,但可能在它到达您的功能之前就被阻止了。是否存在身份验证,或者类型验证失败?
  • 没有,没有。但是为什么我没有看到我的日志呢?
  • 我推测您的请求存在格式错误,JSON 反序列化正在停止该请求(即在它到达函数中的代码之前)。你能从另一个客户端调用这个 REST API 吗?
  • 没错,史蒂夫,正如@Peter_Knego 下面所说,我只发送了一个 JSON 而不是一个数组。

标签: android google-app-engine rest httpclient


【解决方案1】:

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

【讨论】:

  • 非常感谢@Peter_Knego,这确实是问题之一。由于某种原因,我也无法使其与 JSONArray 一起使用,因此我更改了 saveDate 方法以仅接收一个用户。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 1970-01-01
  • 2012-09-26
  • 1970-01-01
相关资源
最近更新 更多