2

AsyncTask の「doInBackground」関数で、次のコードを取得しました。

try 
{ 
    URL url = new URL(myUrl);       
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    conn.setReadTimeout(10000);
    conn.setConnectTimeout(15000);
    conn.setRequestMethod("POST");
    conn.setDoInput(true);  



    Authenticator.setDefault(new Authenticator(){
                protected PasswordAuthentication getPasswordAuthentication() {                                              

        return new PasswordAuthentication ("myUsername", "myPassword".toCharArray());
        }           
    });

    conn.connect();
    int response = conn.getResponseCode();
    inputStream = conn.getInputStream();
    String content = convertInputStreamToString(inputStream);

    return content;

catch (Exception e) {
    e.printStackTrace();
    return null;            
}

資格情報に問題がない場合、すべてが正常に機能し、ResponseCode は 200 です。しかし、間違った資格情報を入力すると、getResponseCode() によって AsyncTask が応答を無期限に待機します (タイムアウトは機能しません)。HttpUrlConnection オブジェクトを見ると、ResponseCode が -1 であることがわかります。

ユーザーが間違った資格情報を提供したとしても、あらゆる状況に対処する必要があります。どうすれば有用な答えを得ることができますか? HttpUrlConnection 以外のクラスを使用する必要がありますか?

4

1 に答える 1

4

この関数から戻っStringて、例外を処理していますか?書きましたonPostExecuteか?

これが私にとって完璧に機能するコードです:

        URL url = new URL(strUrl);  

            conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setRequestProperty("Host", "myhost.com");
            conn.setRequestProperty("Authorization", "Basic " + Base64.encodeToString(toencode, Base64.DEFAULT));
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; U; PPC; en-US; rv:1.3.1)");
            conn.setRequestProperty("Accept-Charset", "UTF-8");

            conn.setConnectTimeout (5000) ; 
            conn.setDoOutput(true); 
            conn.setDoInput(true); 

            BufferedInputStream in = new BufferedInputStream(conn.getInputStream());
            result = Utilities.readStream(in);

            status_code = conn.getResponseCode();

            return result;  
        } catch (MalformedURLException e) {
            return e.getMessage();
          } catch (ProtocolException e) {

                try {
                    status_code = conn.getResponseCode();
                } catch (IOException e1) {
                    status_code = -1;
                }


              return e.getMessage();
          } catch (IOException e) {
                try {
                    status_code = conn.getResponseCode();
                } catch (IOException e1) {
                    status_code = -1;
                }

              return e.getMessage();
          } catch ( Exception e ) {
                try {
                    status_code = conn.getResponseCode();
                } catch (IOException e1) {
                    status_code = -1;
                }
              return e.getMessage();
          } 
            finally
            {
                conn = null;
            }
于 2013-01-28T22:41:10.723 に答える