简体   繁体   English

调用REST api会返回FileNotFoundException

[英]Call to REST api returns FileNotFoundException

I am making my first, simple android app. 我正在制作我的第一个简单的Android应用程序。 For the logging in I'm trying to make a request call to a simple API on a webserver: 对于登录,我试图在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");

Whenever I try to run this, the following comes back: 每当我尝试运行它时,会返回以下内容:

java.io.FileNotFoundException: http://www.gildeict.nl/api/gateway1.0.php at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:250)

I also have the following in my AndroidManifest.xml 我的AndroidManifest.xml也有以下内容

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

The weird thing is that the file does exist. 奇怪的是文件确实存在。 I'm not a pro in java though, so it could be that I'm missing something obvious. 我不是java中的专业人士,所以可能是我错过了一些明显的东西。

How can it be that something so simple isn't functional? 怎么这么简单的东西不起作用呢?

Thanks. 谢谢。

Your code seems to be OK. 你的代码似乎没问题。 The reason for your error is that the server returns error HTTP code for your request. 您的错误的原因是服务器返回您的请求的错误HTTP代码。

For instance, when I try to execute your request with cURL: 例如,当我尝试使用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"

I get this: 我明白了:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM