【问题标题】:HttpUrlConnection is freezing my AsyncTaskHttpUrlConnection 冻结了我的 AsyncTask
【发布时间】:2013-01-28 22:04:39
【问题描述】:

在 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 以外的其他类吗?

【问题讨论】:

  • 你没有对超时异常做任何事情。
  • 我刚刚更新了我的问题以添加我正在做的 try/catch。捕获永远不会被调用。

标签: java android http httpurlconnection


【解决方案1】:

您是否从该函数返回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;
            }

【讨论】:

  • 嗨!我的 doInBackground 函数正在返回一个字符串。是的,我写了 onPostExecute,但里面没有重要的逻辑。我需要处理身份验证的函数。
  • 查看上面的代码。它使用不同的身份验证方案。但其他一切都与您的代码相同。
  • 我不确定您的解决方案,因为我不知道要放置用户名和密码。但我找到了这个难题的最后一块,现在它工作得非常好!我编辑了您的帖子以使其更清晰。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-13
  • 2020-01-05
  • 2021-03-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多