Мой код ниже. Выдает ошибку в 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)
at java.lang.Thread.run (Thread.java:818)