Java 在Android上从Web API检索JSON
这里。。。我有一个带有MessageController的Web API,它将响应我来自Android的请求并发送JSON(Hello World-只是为了测试一下) 我得到了代码-1的回复。不知道为什么。在最终URL中,我将我的Web API定位在Azure上:Java 在Android上从Web API检索JSON,java,c#,android,json,asp.net-web-api,Java,C#,Android,Json,Asp.net Web Api,这里。。。我有一个带有MessageController的Web API,它将响应我来自Android的请求并发送JSON(Hello World-只是为了测试一下) 我得到了代码-1的回复。不知道为什么。在最终URL中,我将我的Web API定位在Azure上: finalUrl ="http://<hereIsMineWebApiURL>/api/message" finalUrl=”http:///api/message" 我确信我的API是错的 为什么您有(“内容长度”
finalUrl ="http://<hereIsMineWebApiURL>/api/message"
finalUrl=”http:///api/message"
我确信我的API是错的 为什么您有
(“内容长度”,“0”)
我删除了它,但在int status=c.getResponseCode()上仍然出现异常;,把我扔到决赛上。你检查你的api服务器了吗?e、 通过普通浏览器?当我进入我的Api时,它会打开我的索引。但当我将api/消息作为目标时,我得到:这个XML文件似乎没有任何与之相关联的样式信息。文档树如下所示。发生了一个错误。我确信我在Api方面犯了错误。不知道在哪里好的,至少我们知道,这不是应用程序,只需查看服务器,尝试缩小问题范围,如果找不到问题,请在SO中发布新问题
public void requestMessagesFromApi(View v)
throws ClientProtocolException, IOException {
final String response = getJSON(finalUrl);
TextView msgTV = (TextView) findViewById(R.id.msgTxt);
msgTV.setText(response);
}
public String getJSON(String url) {
HttpURLConnection c = null;
try {
URL u = new URL(url);
c = (HttpURLConnection) u.openConnection();
c.setRequestMethod("GET");
c.setRequestProperty("Content-length", "0");
c.setUseCaches(false);
c.setAllowUserInteraction(false);
int status = c.getResponseCode();
switch (status) {
case 200:
case 201:
BufferedReader br = new BufferedReader(new InputStreamReader(c.getInputStream()));
StringBuilder sb = new StringBuilder();
String line;
while ((line = br.readLine()) != null) {
sb.append(line+"\n");
}
br.close();
return sb.toString();
}
} catch (MalformedURLException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} catch (IOException ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
} finally {
if (c != null) {
try {
c.disconnect();
} catch (Exception ex) {
Logger.getLogger(getClass().getName()).log(Level.SEVERE, null, ex);
}
}
}
return null;
}
finalUrl ="http://<hereIsMineWebApiURL>/api/message"