FileNotFoundException for HttpURLConnection in Ice Cream Sandwich

前端 未结 4 1083
说谎
说谎 2020-12-02 22:20

I have an Android app that works fine with Android 2.x and 3.x, but it fails when run on Android 4.x.

The problem is in this section of code:

URL url         


        
相关标签:
4条回答
  • 2020-12-02 22:52

    A FileNotFoundException may also been thrown if the server returns a bad error code (e.g., 400 or 401). You can handle this as follows:

    int responseCode = con.getResponseCode(); //can call this instead of con.connect()
    if (responseCode >= 400 && responseCode <= 499) {
        throw new Exception("Bad authentication status: " + responseCode); //provide a more meaningful exception message
    }
    else {
        InputStream in = con.getInputStream();
        //etc...
    }
    
    0 讨论(0)
  • 2020-12-02 23:00

    A little late but you can also verify the accepted content. You can add this line to accept all kinds of contents

    urlConnection.setRequestProperty("Accept","*/*");
    
    0 讨论(0)
  • 2020-12-02 23:03

    Try removing the setDoOutput call. Taken from this blog: a blog

    Edit: This is needed when using a POST call.

    0 讨论(0)
  • 2020-12-02 23:09

    I Don't know why, but dealing manually with redirection resolves the problem.

    connection.setInstanceFollowRedirects(false);
    
    0 讨论(0)
提交回复
热议问题