我正在制作我的第一个简单的Android应用程序。对于登录,我试图在Web服务器上调用一个简单的API:
URL url = new URL(API);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setRequestProperty("Accept", "application/json");
connection.setRequestProperty("Content-Type", "application/json; charset=UTF-8");
每当我尝试运行时,会发生以下情况:
java.io.FileNotFoundException: http://www.gildeict.nl/api/gateway1.0.php at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)
我的AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
奇怪的是文件确实存在。我不是java的专业人士,所以可能是我错过了一些明显的东西。
如此简单的东西怎么可能不起作用?
感谢。
答案 0 :(得分:0)
您的代码似乎没问题。您的错误原因是服务器返回您的请求的错误HTTP代码。
例如,当我尝试使用cURL执行您的请求时:
curl -i -X GET -H "Accept: application/json" -H "Content-Type: application/json; charset=UTF-8" "http://www.gildeict.nl/api/gateway1.0.php"
我明白了:
HTTP/1.1 500 Internal Server Error
Server: nginx
Date: Wed, 19 Oct 2016 08:16:25 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/5.6.26
Access-Control-Allow-Origin: *
Cache-Control: no-cache, no-store, max-age=0, must-revalidate
X-Powered-By: PleskLin