Restful API codeigniter - JAVA问题

7
我们使用Phil Sturgeons很酷的Restful API框架 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
你能发布一下它返回的内容吗? - Broncha
如果不需要CI,你可以尝试一下jabsorb - quantme
首先,从Java控制台应用程序中尝试一个URL,然后继续进行。 - Kinjal Dixit
1
尝试使用类似高级 REST 客户端(https://chrome.google.com/webstore/detail/advanced-rest-client/hgmloofddffdnphfgcellkdfbfbjeloo)的客户端查看服务器返回的内容。如果您能发布响应头,将会非常有帮助。 - lyomi
尝试使用Poster(https://addons.mozilla.org/en-us/firefox/addon/poster/)或SOAP UI(http://www.soapui.org/)来测试您的REST请求/响应。如果您在*NIX机器上,甚至可以使用命令行中的CURL。这样做将验证您正在请求的信息是否可用,并且您可以从那里进行更多故障排除。 - bh5k
mkyong文章中的第二个代码块是一个Java客户端,用于执行@BinaryNights建议的操作:http://www.mkyong.com/webservices/jax-rs/restfull-java-client-with-java-net-url/ - bh5k
1个回答

1

如果头字段没有正确设置,将会抛出MalformedException异常。请尝试使用不同的头和不同的用户代理。尝试使用httpConnection.setDoOutput(true)。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接