Android Http url connection Throwing File Not Found Exception at getInputstream

被刻印的时光 ゝ 提交于 2019-12-05 07:49:16

In case of 4** Error HTTP Codes use

connection.getErrorStream();

Instead of

connection.getInputStream();

connection is type of HttpURLConnection.

FileNotFound on URL connection means that target document is not available aka URL provided is wrong, or server returns 4* code. Check if target resource is available

Try setting this: connection.setDoOutput(false); to your connection - hope it helps ;)

NSK

add "connection.connect()" line before writing data to the stream.

connection.connect();

OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
                    writer.write(formsubmissionJson);                   
writer.flush();

I dont know why, but after adding the following line of code the problem was solved.

connection.setInstanceFollowRedirects(false);
swapnil Pawar

If you used GET method on server side then it will give this error.

To make it works:

Change connection.setRequestMethod("POST"); to connection.setRequestMethod("GET"); and remove this following code.

connection.setDoOutput(true);       
            OutputStreamWriter writer = new OutputStreamWriter(connection.getOutputStream());
                    writer.write(formsubmissionJson);
                    writer.flush();

Or just simply change GET method to POST on server without doing changes above.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!