【问题标题】:Application crashing on HttpsURLConnection openConnection() The application may be doing too much work on its main thread应用程序在 HttpsURLConnection openConnection() 上崩溃 应用程序可能在其主线程上做了太多工作
【发布时间】:2016-09-01 18:32:13
【问题描述】:

我正在尝试调用 JSON AsynTack 服务来登录我的用户。不幸的是,它在 openConnection 上崩溃并显示错误消息:执行 doInBackground() 时发生错误。我是一名正在尝试学习 Android 的 iOS 开发人员,所以如果我遗漏了一些简单的东西,我深表歉意。

 public class UserLoginTask extends AsyncTask<Void, Void, Boolean> {

        private final String mEmail;
        private final String mPassword;

        HttpURLConnection urlConnection = null;
        BufferedReader reader = null;

        UserLoginTask(String email, String password) {
            mEmail = email;
            mPassword = password;
        }


        @Override
        protected Boolean doInBackground(Void... params) {
            ConnectivityManager cm = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE);
            NetworkInfo info = cm.getActiveNetworkInfo();

            if(info==null || !info.isConnected()) {
                return false;
            }
                try {

                    URL url = new URL("http://urlForLoginHere");
                    urlConnection = (HttpsURLConnection) url.openConnection();
                    urlConnection.setReadTimeout(10000);
                    urlConnection.setConnectTimeout(15000);
                    urlConnection.setRequestMethod("POST");
                    urlConnection.setDoInput(true);
                    urlConnection.setDoOutput(true);

                    Uri.Builder builder = new Uri.Builder()
                            .appendQueryParameter("username", mEmail)
                            .appendQueryParameter("password", mPassword);
                    String query = builder.build().getEncodedQuery();

                    OutputStream os = urlConnection.getOutputStream();
                    BufferedWriter writer = new BufferedWriter(
                            new OutputStreamWriter(os, "UTF-8"));
                    writer.write(query);
                    writer.flush();
                    writer.close();
                    os.close();

                    urlConnection.connect();


                    // Read the input stream into a String
                    InputStream inputStream = urlConnection.getInputStream();
                    StringBuffer buffer = new StringBuffer();
                    if (inputStream == null) {
                        // Nothing to do.
                        return false;
                    }
                   // reader = new BufferedReader(new InputStreamReader(inputStream));

                    if (buffer.length() == 0) {
                        // Stream was empty.  No point in parsing.
                        return false;
                    }

                    String jsonString = buffer.toString();


                    urlConnection.setConnectTimeout(3000);
                    urlConnection.connect();
                    if (urlConnection.getResponseCode() == 200) {
//                        try {
//
//                        }
                        return true;
                    }
                } catch (MalformedURLException e1) {
                    // TODO Auto-generated catch block
                    e1.printStackTrace();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            return  false;
        }

【问题讨论】:

  • 发布日志会有帮助

标签: android json httpurlconnection android-connectivitymanager


【解决方案1】:

不确定这是否是原因,但您的urlConnection 语句似乎不正确。试试下面的代码:

    URL url = new URL("http://urlForLoginHere");
    HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
    urlConnection.setReadTimeout(10000);
    urlConnection.setConnectTimeout(15000);
    urlConnection.setRequestMethod("POST");
    urlConnection.setDoInput(true);
    urlConnection.setDoOutput(true);

    Uri.Builder builder = new Uri.Builder()
            .appendQueryParameter("username", mEmail)
            .appendQueryParameter("password", mPassword);
    String query = builder.build().getEncodedQuery();

    OutputStream os = urlConnection.getOutputStream();
    BufferedWriter writer = new BufferedWriter(
            new OutputStreamWriter(os, "UTF-8"));
    writer.write(query);
    writer.flush();
    writer.close();
    os.close();

    // reader = new BufferedReader(new InputStreamReader(inputStream));

    String jsonString = buffer.toString();

    if (urlConnection.getResponseCode() == 200) {
        InputStream inputStream = urlConnection.getInputStream();
        StringBuffer buffer = new StringBuffer();
        // do stuff here
        return true;
    }
    else {
        InputStream inputStream = urlConnection.getErrorStream();
        // :(
    } 

【讨论】:

  • 我试过了,它在 HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection() 上崩溃了;错误 HostConnection::get() New Host Connection created 0x7f563540dd40, tid 5477 09-01 14:58:59.278 5382-5477/ctsapp.purple.com.setec I/OpenGLRenderer: Initialized EGL, version 1.4 09-01 14:59 :00.114 5382-5382/ctsapp.purple.com.setec I/Choreographer:跳过了 38 帧!应用程序可能在其主线程上做了太多工作。
  • 所以,和之前一样的错误。等待。你是通过手动调用doInBackground()来执行这个方法的吗?
  • 我已更新我的代码以显示所有内容。我用 mAuthTask = new UserLoginTask(email, password); 调用它mAuthTask.execute((Void) null);
  • @user1079052 你不需要在任何地方调用urlConnection.connect()(打开连接应该这样做)。此外,如果您愿意,请将更新后的代码发布在您的问题中,以便其他人也可以检查。
猜你喜欢
  • 2013-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多