E / AndroidRuntime:致命异常:Android的AsyncTask#1错误

时间:2018-09-02 01:06:35

标签: android android-studio android-asynctask android-sqlite android-sql

我的代码在下面。 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:致命异常:AsyncTask#1                     进程:com.example.smtanverhossain.myphpmysqlapplication,PID:5445                     java.lang.RuntimeException:执行doInBackground()时发生错误                         在android.os.AsyncTask $ 3.done(AsyncTask.java:300)                         在java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)                         在java.util.concurrent.FutureTask.setException(FutureTask.java:222)                         在java.util.concurrent.FutureTask.run(FutureTask.java:242)                         在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)                         在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                         在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)                         在java.lang.Thread.run(Thread.java:818)                      造成原因:java.lang.ClassCastException:com.android.okhttp.internal.http.HttpURLConnectionImpl无法强制转换   到javax.net.ssl.HttpsURLConnection                         在com.example.smtanverhossain.myphpmysqlapplication.BackgroundTask.doInBackground(BackgroundTask.java:46)                         在com.example.smtanverhossain.myphpmysqlapplication.BackgroundTask.doInBackground(BackgroundTask.java:19)                         在android.os.AsyncTask $ 2.call(AsyncTask.java:288)                         在java.util.concurrent.FutureTask.run(FutureTask.java:237)                         在android.os.AsyncTask $ SerialExecutor $ 1.run(AsyncTask.java:231)                         在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)                         在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587)                         在java.lang.Thread.run(Thread.java:818)

2 个答案:

答案 0 :(得分:0)

错误在日志中说明: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。当您用url.openConnection()调用HTTP时,它返回一个HttpURLConnection,并且同样适用于HTTPSdocumentation中对此进行了说明。

解决方案应该是将所有HttpsURLConnection替换为HttpURLConnection,并删除对HttpsURLConnection的转换。

答案 1 :(得分:0)

@Tohu的解决方案可能会解决您的转换错误,这是我想带给您的,您正在将活动上下文传递给asynktask,而您要通过调用finish()来销毁这个任务,这在显示带有活动上下文的吐司时可能会给您一个错误。 因此,请确保您提供有效的上下文,例如:-。

getApplicationContext()