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?

Mohammad
  • 3,449
  • 6
  • 48
  • 75
  • 1
    I suggest that you edit your question and post the entire Java stack trace associated with your crash. – CommonsWare Jan 03 '17 at 23:05
  • @CommonsWare see the edit – Mohammad Jan 03 '17 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](https://github.com/square/okhttp/blob/master/samples/guide/src/main/java/okhttp3/recipes/PostStreaming.java). – CommonsWare Jan 03 '17 at 23:13

2 Answers2

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,

user207421
  • 305,947
  • 44
  • 307
  • 483
  • I checked the `connection.getRespondeCode()` it returns `200` – Mohammad Jan 03 '17 at 23:10
  • 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! – Mohammad Jan 03 '17 at 23:13
  • 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. – user207421 Jan 03 '17 at 23:27
  • Hey my code is something 404..but I have some JSON responce to this code. How can I read it – pritam001 Oct 04 '19 at 14:09
  • 1
    @pritam001 Hard to believe. If your server really delivers JSON along with a 404 there is something seriously wrong with it. – user207421 Feb 24 '20 at 03:36
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.

Mohammad
  • 3,449
  • 6
  • 48
  • 75
  • 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. – user207421 Jan 03 '17 at 23:37
  • @EJP answer edited, anyway the problem has been solved, I was distracted after 13 hours of continuous coding, thanks. – Mohammad Jan 03 '17 at 23:44
  • This does not answer the question that was asked. I suggest you delete your question. It never made sense in the first place. – user207421 Jan 03 '17 at 23:46