HttpURLConnection connect()异常4.0& 4.1而不是2.3.3

时间:2012-11-17 18:26:18

标签: java android

  

可能重复:
  Android app doesn’t work on Android 4

我正在开发一款Android应用。试图找到解决方案几个小时。当我运行模拟器2.3.3时,我的应用程序与数据库和服务器一起工作正常,但在4.0或4.1或2.3.3以上的任何版本上都没有。应用程序在此方法中引发异常:尝试执行connection.connect();请求帮助时发生异常。

public void sendOutputLine(URL url, String outputLine) {
    try {
        connection = (HttpURLConnection) url.openConnection();
        Log.d(LogInActivity.DEBUG_TAG, "1");
        connection.setDoInput(true);
        Log.d(LogInActivity.DEBUG_TAG, "2");    
        connection.setDoOutput(true);
        Log.d(LogInActivity.DEBUG_TAG, "3");    
        connection.setRequestMethod("POST");
        Log.d(LogInActivity.DEBUG_TAG, "4");    
        connection.setFixedLengthStreamingMode(outputLine.length());
        Log.d(LogInActivity.DEBUG_TAG, "5");    
        connection.connect();
        Log.d(LogInActivity.DEBUG_TAG, "6");    
        out = connection.getOutputStream();
        PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
        pw.print(outputLine);
        pw.flush();
        out.close();
    } catch (SocketTimeoutException e) {
        Log.d(LogInActivity.DEBUG_TAG,
                "SocketTimeoutException sendOutputLine(): " + e.getMessage());          
    } catch (ProtocolException e) {
        Log.d(LogInActivity.DEBUG_TAG,
                "Protocol exception sendOutputLine(): " + e.getMessage());
    } catch (IOException e) {
        Log.d(LogInActivity.DEBUG_TAG, "IO exception sendOutputLine(): "
                + e.getMessage());
    } catch (Exception e) {
        Log.d(LogInActivity.DEBUG_TAG,
                "Exception bommed client: " + e.getMessage());  
    }
}

logcat的:

11-17 13:19:11.710: D/SocialFamilyTree(847): outputLint: Login%a@a%57963634
11-17 13:19:11.710: D/SocialFamilyTree(847): 1
11-17 13:19:11.710: D/SocialFamilyTree(847): 2
11-17 13:19:11.710: D/SocialFamilyTree(847): 3
11-17 13:19:11.710: D/SocialFamilyTree(847): 4
11-17 13:19:11.710: D/SocialFamilyTree(847): 5
11-17 13:19:11.740: D/SocialFamilyTree(847): Exception bommed client: null

修改: 我明白了,所以我需要使用AsyncTask

如何使用AsyncTask执行此任务?

2 个答案:

答案 0 :(得分:0)

通过这种方式使用AsyncTask ......

public class getConnection extends AsyncTask<String, Void, Void> 
    {


           @Override
            protected void onPreExecute() 
           {
               super.onPreExecute();
               Utils.clearDialogs();
               Utils.showActivityViewer(Mainctivity.this);
           }

           @Override
           protected Void doInBackground(String... arg0)
           {

            try 
            {
                   connection = (HttpURLConnection) url.openConnection();

                   Log.d(LogInActivity.DEBUG_TAG, "1");
                   connection.setDoInput(true);
                   Log.d(LogInActivity.DEBUG_TAG, "2");    
                   connection.setDoOutput(true);
                   Log.d(LogInActivity.DEBUG_TAG, "3");    
                   connection.setRequestMethod("POST");
                   Log.d(LogInActivity.DEBUG_TAG, "4");    
                   connection.setFixedLengthStreamingMode(outputLine.length());
                   Log.d(LogInActivity.DEBUG_TAG, "5");    
                   connection.connect();
                   Log.d(LogInActivity.DEBUG_TAG, "6");    
                   out = connection.getOutputStream();
                   PrintWriter pw = new PrintWriter(new OutputStreamWriter(out));
                   pw.print(outputLine);
                   pw.flush();
                   out.close();
            } 
            catch (Exception e) 
            {
             e.printStackTrace();
            }

            return null;
           }

           @Override
           protected void onPostExecute(Void result)
           {
               super.onPostExecute(result);
               Utils.hideActivityViewer();

            }
            catch (Exception e)
            {
             Log.v("log", e.toString());
            }
           }

          }

答案 1 :(得分:0)

不要盲目地捕捉异常。您通过隐藏错误来掩盖整个问题。如果我不得不猜测,我会说你遇到了NetworkOnMainThreadException,因为你可能没有在后台线程上运行,但是不可能告诉你提供的信息。

AsyncTask是解决NetworkOnMainThreadException问题的一种方法,但它不是唯一的方法,并且没有证据证明这是您的实际问题。