java.io.FileNotFoundException on getInputStream()
12,075
The connection response code is 200
No it isn't. There is nothing in this code that checks the response code. A FileNotFoundException
means a response code of 404.
NB setDoOutput(true)
sets the method to POST. You don't need to set that yourself,
Author by
Mohammad
<?php $this->mode(EXCITED_TO_PROGRAM); $this->SoReadyToHelp(true); ?> NEXT GOAL? to be the first in this list: Top stackoverflow users in Lebanon
Updated on June 05, 2022Comments
-
Mohammad almost 2 years
I am trying to getInputStream from a URL, the connection response code is
200
, but I am getting an exceptionFileNotFoundException
when I try to getInputStream, here is my code:url = new URL("http://..."); connection = (HttpURLConnection) url.openConnection(); connection.setDoOutput(true); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); connection.setRequestMethod("POST"); int status = connection.getResponseCode(); if(status >= 400){ request = new OutputStreamWriter(connection.getOutputStream()); request.write(params); request.flush(); request.close(); String line; InputStreamReader isr = new InputStreamReader(connection.getInputStream()); BufferedReader reader = new BufferedReader(isr); StringBuilder sb = new StringBuilder(); while ((line = reader.readLine()) != null) { sb.append(line + "\n"); } }
the stack trace:
W/System.err: java.io.FileNotFoundException: http://... W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:238) W/System.err: at com.apps.topinformers.sharedreading.AddGroupMembersFragment.postText(AddGroupMembersFragment.java:112) W/System.err: at com.apps.topinformers.sharedreading.AddGroupMembersFragment.access$000(AddGroupMembersFragment.java:26) W/System.err: at com.apps.topinformers.sharedreading.AddGroupMembersFragment$PostDataAsyncTask.doInBackground(AddGroupMembersFragment.java:65) W/System.err: at com.apps.topinformers.sharedreading.AddGroupMembersFragment$PostDataAsyncTask.doInBackground(AddGroupMembersFragment.java:55) W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:295) W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237) W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:234) W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) W/System.err: at java.lang.Thread.run(Thread.java:818)
What is the problem, and how I can debug it?
-
Mohammad over 7 yearsI checked the
connection.getRespondeCode()
it returns200
-
Mohammad over 7 yearsI used it just for making sure that the connection, then I removed it. I don't want to handle or catch the error, I want to resolve it!
-
user207421 over 7 yearsYour test proves my point completely. The response code is not 200, it is >= 400, otherwise you don't execute
getInputStream()
and you don't get the exception. NB You should check it after writing the output. -
user207421 over 7 yearsThe response code was not 200 if you got a
FileNotFoundException
. It is impossible. One or other of those statements is untrue. You have provided exactly zero evidence to the contrary. -
Mohammad over 7 years@EJP answer edited, anyway the problem has been solved, I was distracted after 13 hours of continuous coding, thanks.
-
user207421 over 7 yearsThis does not answer the question that was asked. I suggest you delete your question. It never made sense in the first place.
-
pritam001 over 4 yearsHey my code is something 404..but I have some JSON responce to this code. How can I read it
-
user207421 about 4 years@pritam001 Hard to believe. If your server really delivers JSON along with a 404 there is something seriously wrong with it.