1

可能重复:
Android 应用无法在 Android 4 上运行

我正在开发一个安卓应用程序。试图找到解决方案几个小时。当我运行模拟器 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());  
    }
}

日志猫:

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这个任务?

4

2 回答 2

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());
            }
           }

          }
于 2012-11-17T19:19:14.437 回答
0

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

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

于 2012-11-17T19:33:43.293 回答