android - 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;
}
解决方案
推荐阅读
- css - 从不同来源多次导入相同的字体有害吗?
- angular - Angular 5过滤管
- apache-spark - 如何解释 Spark 中precisionByThreshold 的返回结果?
- rust - 如何删除我在 Rust 中无权访问的目录?
- google-compute-engine - 在不关闭实例的情况下调整云 VM 磁盘大小(谷歌云)
- python - Keras:使用灰度蒙版和 ImageDataGenerator 类进行图像分割
- mdc-components - 如何在 lit 元素中使用 MDC 组件?
- r - 在 SpatialPolygonDataFrame 中计算人口
- migration - 用于优化构建的 Angular 6 升级运行时问题
- scala - 如何创建涉及某些功能在范围内的“块”的 DSL?