5

I am trying to getInputStream from a URL, the connection response code is 200, but I am getting an exception FileNotFoundException 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?

3
  • 2
    I suggest that you edit your question and post the entire Java stack trace associated with your crash. Commented Jan 3, 2017 at 23:05
  • @CommonsWare see the edit Commented Jan 3, 2017 at 23:08
  • 1
    I agree with EJP -- this fits a 404 response code from your request. You may find it easier to use a different HTTP client API, such as OkHttp. Commented Jan 3, 2017 at 23:13

2 Answers 2

4

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,

Sign up to request clarification or add additional context in comments.

5 Comments

I checked the connection.getRespondeCode() it returns 200
I 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!
Your 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.
Hey my code is something 404..but I have some JSON responce to this code. How can I read it
@pritam001 Hard to believe. If your server really delivers JSON along with a 404 there is something seriously wrong with it.
0

with credits to @EJP I have checked the response as follow:

url = new URL("http://...");
connection = (HttpURLConnection) url.openConnection();
connection.setDoOutput(true);
connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");
connection.setRequestMethod("POST");

request = new OutputStreamWriter(connection.getOutputStream());
request.write(params);
request.flush();
request.close();
String line;

int status = connection.getResponseCode();
if(status < 400) {

    InputStreamReader isr = new InputStreamReader(connection.getInputStream());
    ...
} else{
    InputStreamReader isr = new InputStreamReader(connection.getErrorStream());
    ...
}

the error stream showed me that there are some problems with the implementation code of the URL that I am requesting.

3 Comments

The 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.
@EJP answer edited, anyway the problem has been solved, I was distracted after 13 hours of continuous coding, thanks.
This does not answer the question that was asked. I suggest you delete your question. It never made sense in the first place.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.