E/AndroidRuntime:致命异常:Android 的 AsyncTask #1 错误
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 ERROR for Android
我的代码如下。它在 Android Studio 中出错。任何人都请帮助我。我的 PHP 代码没问题,但为什么会出现此错误?
Register.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Register extends AppCompatActivity {
EditText et_name, et_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
et_name = (EditText) findViewById(R.id.nametxt);
et_number = (EditText) findViewById(R.id.numbertxt);
}
public void userReg(View view){
String names = et_name.getText().toString();
String numbers = et_number.getText().toString();
String method = "register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method,names,numbers);
finish();
}
}
我的Background.java代码是
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class BackgroundTask extends AsyncTask<String,Void,String> {
Context ctx;
BackgroundTask(Context ctx){
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://192.168.1.2/logins/register.php";
String method = params[0];
if(method.equals("register")){
String name = params[1];
String number = params[2];
try {
URL url = new URL(reg_url);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setDoOutput(true);
OutputStream os = httpsURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("names","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
+URLEncoder.encode("numbers","UTF-8")+"="+URLEncoder.encode(number,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
os.close();
InputStream IS = httpsURLConnection.getInputStream();
IS.close();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
而我的Androidmainfest.xml是
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Android Studio 错误是:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1
Process: com.example.smtanverhossain.myphpmysqlapplication, PID: 5445
java.lang.RuntimeException: An error occured while executing doInBackground()
at android.os.AsyncTask.done(AsyncTask.java:300)
at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
at java.util.concurrent.FutureTask.run(FutureTask.java:242)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
Caused by: java.lang.ClassCastException: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast
to javax.net.ssl.HttpsURLConnection
at com.example.smtanverhossain.myphpmysqlapplication.BackgroundTask.doInBackground(BackgroundTask.java:46)
at com.example.smtanverhossain.myphpmysqlapplication.BackgroundTask.doInBackground(BackgroundTask.java:19)
at android.os.AsyncTask.call(AsyncTask.java:288)
at java.util.concurrent.FutureTask.run(FutureTask.java:237)
at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
at java.lang.Thread.run(Thread.java:818)
日志中解释了错误:Caused by: java.lang.ClassCastException: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
这意味着您不能在这一行转换为 HttpsURLConnection
:
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
这是因为您的 URL 使用 HTTP
而不是 HTTPS
。当你用 HTTP
调用 url.openConnection()
时,它 returns 一个 HttpURLConnection
并且同样适用于 HTTPS
。 documentation.
中说明了这一点
解决方案应该是将所有 HttpsURLConnection
替换为 HttpURLConnection
并删除对 HttpsURLConnection
的强制转换。
@Tohu 的解决方案可能会解决您的强制转换错误,这是我想让您看到的,您正在将 activity 的上下文传递给 asynktask,您正在通过调用 finish()
,当您使用 activity 上下文显示吐司时,这可能会给您带来错误。
因此,请确保您提供了有效的上下文,例如:-.
getApplicationContext()
我的代码如下。它在 Android Studio 中出错。任何人都请帮助我。我的 PHP 代码没问题,但为什么会出现此错误?
Register.java
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
public class Register extends AppCompatActivity {
EditText et_name, et_number;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
et_name = (EditText) findViewById(R.id.nametxt);
et_number = (EditText) findViewById(R.id.numbertxt);
}
public void userReg(View view){
String names = et_name.getText().toString();
String numbers = et_number.getText().toString();
String method = "register";
BackgroundTask backgroundTask = new BackgroundTask(this);
backgroundTask.execute(method,names,numbers);
finish();
}
}
我的Background.java代码是
import android.content.Context;
import android.net.Uri;
import android.os.AsyncTask;
import android.widget.Toast;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import javax.net.ssl.HttpsURLConnection;
public class BackgroundTask extends AsyncTask<String,Void,String> {
Context ctx;
BackgroundTask(Context ctx){
this.ctx = ctx;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
}
@Override
protected String doInBackground(String... params) {
String reg_url = "http://192.168.1.2/logins/register.php";
String method = params[0];
if(method.equals("register")){
String name = params[1];
String number = params[2];
try {
URL url = new URL(reg_url);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
httpsURLConnection.setRequestMethod("POST");
httpsURLConnection.setDoOutput(true);
OutputStream os = httpsURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(os,"UTF-8"));
String data = URLEncoder.encode("names","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
+URLEncoder.encode("numbers","UTF-8")+"="+URLEncoder.encode(number,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
os.close();
InputStream IS = httpsURLConnection.getInputStream();
IS.close();
return "Registration Success...";
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(String result) {
Toast.makeText(ctx,result,Toast.LENGTH_LONG).show();
}
}
而我的Androidmainfest.xml是
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Android Studio 错误是:
E/AndroidRuntime: FATAL EXCEPTION: AsyncTask #1 Process: com.example.smtanverhossain.myphpmysqlapplication, PID: 5445 java.lang.RuntimeException: An error occured while executing doInBackground() at android.os.AsyncTask.done(AsyncTask.java:300) at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355) at java.util.concurrent.FutureTask.setException(FutureTask.java:222) at java.util.concurrent.FutureTask.run(FutureTask.java:242) at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818) Caused by: java.lang.ClassCastException: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection at com.example.smtanverhossain.myphpmysqlapplication.BackgroundTask.doInBackground(BackgroundTask.java:46) at com.example.smtanverhossain.myphpmysqlapplication.BackgroundTask.doInBackground(BackgroundTask.java:19) at android.os.AsyncTask.call(AsyncTask.java:288) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at android.os.AsyncTask$SerialExecutor.run(AsyncTask.java:231) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587) at java.lang.Thread.run(Thread.java:818)
日志中解释了错误:Caused by: java.lang.ClassCastException: com.android.okhttp.internal.http.HttpURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
这意味着您不能在这一行转换为 HttpsURLConnection
:
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
这是因为您的 URL 使用 HTTP
而不是 HTTPS
。当你用 HTTP
调用 url.openConnection()
时,它 returns 一个 HttpURLConnection
并且同样适用于 HTTPS
。 documentation.
解决方案应该是将所有 HttpsURLConnection
替换为 HttpURLConnection
并删除对 HttpsURLConnection
的强制转换。
@Tohu 的解决方案可能会解决您的强制转换错误,这是我想让您看到的,您正在将 activity 的上下文传递给 asynktask,您正在通过调用 finish()
,当您使用 activity 上下文显示吐司时,这可能会给您带来错误。
因此,请确保您提供了有效的上下文,例如:-.
getApplicationContext()