简体   繁体   中英

java.io.FileNotFoundException on getInputStream()

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?

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,

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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