FileNotFoundException when calling webservice

后端 未结 5 833
天涯浪人
天涯浪人 2021-02-04 01:47

Hello I got past my initial problem. I\'m a total android noob, this is my first app. I\'m testing this on the Android emulator. I try to connect to a .NET webservice at h

相关标签:
5条回答
  • 2021-02-04 02:31

    Make sure you can access this URL from your web browser http://192.168.3.47/service.asmx

    and make sure there is no proxy configured with your web browser, if so configure your code accordingly

    0 讨论(0)
  • 2021-02-04 02:36

    Try removing:

    connection.setDoInput(true);
    connection.setDoOutput(true);
    
    0 讨论(0)
  • 2021-02-04 02:38

    It's the same problem I was having: HttpUrlConnection returns FileNotFoundException if you try to read the getInputStream() from the connection.
    You should instead use getErrorStream() when the status code is higher than 400.

    More than this, please be careful since it's not only 200 to be the success status code, even 201, 204, etc. are often used as success statuses.

    Here is an example of how I went to manage it

    ... connection code code code ...
    
    // Get the response code 
    int statusCode = connection.getResponseCode();
    
    InputStream is = null;
    
    if (statusCode >= 200 && statusCode < 400) {
       // Create an InputStream in order to extract the response object
       is = connection.getInputStream();
    }
    else {
       is = connection.getErrorStream();
    }
    
    ... callback/response to your handler....
    

    In this way, you'll be able to get the needed response in both success and error cases.

    Hope this helps!

    0 讨论(0)
  • 2021-02-04 02:39

    The HttpURLConnection class is misleading in that it will throw a FileNotFoundException for any HTTP error code of 400 or above.

    So it's not necessarily an incorrect URL (404) it could be 400 (bad request), 403 (forbidden), 500 (internal server error) or something else.

    Use the getResponseCode method to get a more precise indication of the problem.

    0 讨论(0)
  • 2021-02-04 02:42

    I had this error while calling the API. I realized that I was missing the API key. Anyone finding this thread for the same problem, don't forget to include the API key in the API call, if it is required by the API.

    0 讨论(0)
提交回复
热议问题