Restful API codeigniter - JAVA问题

时间:2012-08-29 12:21:18

标签: java api codeigniter

我们使用Phil Sturgeons cool Restful API framework for codeigniter构建了我们的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。

1 个答案:

答案 0 :(得分:1)

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