7

我们使用Phil Sturgeons 为 codeigniter 构建了我们的 API 很酷的 Restful API 框架,该框架已准备好生产并用作我们移动应用程序实施的一部分。

在 Java 中使用 API 时遇到问题

httpConnection = (HttpConnection) Connector.open(url, Connector.READ, true);
// Set content type by given parameter......
httpConnection.setRequestProperty("Accept", contentType);
httpConnection.setRequestProperty("User-Agent", "Profile/MIDP-2.0 Configuration/FCLDC-1.0");
httpConnection.setRequestProperty("Content-Type", contentType);
httpConnection.setRequestProperty("TK-API-KEY", UrlFactory.TK_API_KEY);

// httpConnection.setRequestProperty("Model",
// StyleUtil.getDeviceModel());
if (httpConnection.getResponseCode() == 302)
{
  String redirectUrl =  httpConnection.getHeaderField("Location");
  httpConnection = (HttpConnection) Connector.open(redirectUrl, Connector.READ_WRITE, true);
}

if (httpConnection.getResponseCode() == HttpConnection.HTTP_OK)
{
  io = httpConnection.openInputStream();

  int ch;
  while ((ch = io.read()) != -1)
  {
    bo.write(ch);
  }

}

httpConnection.getResponseCode()无法获取状态码并返回格式错误的异常。我们的 API 服务器是 NGINX。

4

1 回答 1

1

如果未正确设置标头字段,则会引发 MalformedException。请检查不同的标题以及不同的用户代理。尝试使用 httpConnection.setDoOutput(true);

于 2013-03-12T05:45:33.130 回答