首页 > 解决方案 > Android:致命异常:AsyncTask #1

问题描述

我正在关注 Udemy 的教程。我遇到了一个问题,我无法解决,所以我来到了这里。

错误说:

2019-06-14 01:20:26.192 2849-3891/com.example.mysqlnandroid E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
    Process: com.example.mysqlnandroid, PID: 2849
    java.lang.RuntimeException: An error occurred while executing doInBackground()" 

Caused by: 
Caused by: java.lang.ClassCastException: com.android.okhttp.internal.huc.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
        at com.example.mysqlnandroid.BackgroundTask.doInBackground(BackgroundTask.java:42)
        at com.example.mysqlnandroid.BackgroundTask.doInBackground(BackgroundTask.java:20)

- - - 
Line 42: httpsURLConnection = (HttpsURLConnection) url.openConnection(); 
Line 20: class BackgroundTask extends AsyncTask<String, Void, String> {

Logcat 和 Debug 的问题是两行

protected String doInBackground(String... params) {
        //Store the firs parametro xd patamere?
        //TIene que ser igual a -> Registro
        String method=params[0];
        //Path -> Localhost, humm .> * Search why not -> 127.0.0.1
        String reg_url="http://10.0.2.2/project/registro.php";
        Log.d("Registro","Iniciando");
        if (method.equals("Registro")) {
            String usuario = params[1];
            Log.d("Usuario: ",params[1]);
            String contrasena = params[2];
            Log.d("Password-> : ",params[2]);
            try {
                URL url = new URL(reg_url);
                HttpsURLConnection httpsURLConnection;
                httpsURLConnection = (HttpsURLConnection) url.openConnection();
                httpsURLConnection.setRequestMethod("POST");
                httpsURLConnection.setDoOutput(true);
                //Crea el objeto del os, y nos permite enviar
                //la data aia
                //Toast.makeText(ctx,"Dentro de do in baclgrund",Toast.LENGTH_SHORT).show();
                OutputStream os = httpsURLConnection.getOutputStream();
                BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os, StandardCharsets.UTF_8));
                String data = URLEncoder.encode("usuario", "UTF-8") + "=" + URLEncoder.encode(usuario, "UTF-8") + "&" +
                              URLEncoder.encode("contrasena", "UTF-8") + "=" + URLEncoder.encode(contrasena, "UTF-8");
                bufferedWriter.write(data);
                bufferedWriter.flush();
                bufferedWriter.close();
                os.close();
                InputStream is = httpsURLConnection.getInputStream();
                is.close();
                return "Register complete";
            } catch (MalformedURLException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return null;
    }

标签: androidmysql

解决方案


推荐阅读