Мой код для файла Android находится по ссылке ниже.Я получаю сообщение об ошибке OnPostExecution
MainActivity не является включающим классом
Я хочу перенаправить свою текущую активность на другую активность после успешного входа в систему, я использую Php-MySQLдля базы данных.И я не могу использовать статические ключевые слова для входа в систему как username = "admin" && Password = "admin".
package helloworld.krish.com.login2;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.view.View;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import static android.support.v4.content.ContextCompat.startActivity;
public class BackgroundWorker extends AsyncTask <String, Void, String > {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx) {
context = ctx;
}
boolean statusOk = false;
@Override
protected String doInBackground(String... params) {
String type = params[0];
String login_url = "http://192.168.0.106/login.php";
String reg_url = "http://192.168.0.106/register.php";
System.out.println(1);
//Login Function
if(type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection htpurl = (HttpURLConnection)url.openConnection();
System.out.println(2);
htpurl.setRequestMethod("POST");
htpurl.setDoInput(true);
htpurl.setDoOutput(true);
System.out.println(3);
OutputStream outputStream = htpurl.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String Post_data = URLEncoder.encode("user_name","UTF-8")+"="+URLEncoder.encode(user_name,"UTF-8")+"&"
+URLEncoder.encode("enter code herepassword","UTF-8")+"="+URLEncoder.encode(password,"UTF-8");
System.out.println(4);
bufferedWriter.write(Post_data);
System.out.println(5);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//response for post request//
System.out.println(6);
InputStream inputStream = htpurl.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
System.out.println(6.5);
while((line = bufferedReader.readLine())!= null){
System.out.println(7);
result += line;
}
bufferedReader.close();
inputStream.close();
htpurl.disconnect();
System.out.println(7.5);
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
//Register Activity
else if (type.equals("register")){
try {
String name = params[1];
String designation = params[2];
String salary = params[3];
String cont = params[4];
URL url = new URL(reg_url);
HttpURLConnection htpurl = (HttpURLConnection)url.openConnection();
System.out.println(2);
htpurl.setRequestMethod("POST");
htpurl.setDoInput(true);
htpurl.setDoOutput(true);
System.out.println(3);
OutputStream outputStream = htpurl.getOutputStream();
//Main Code for Register//
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String Post_data = URLEncoder.encode("name","UTF-8")+"="+URLEncoder.encode(name,"UTF-8")+"&"
+URLEncoder.encode("designation","UTF-8")+"="+URLEncoder.encode(designation,"UTF-8")
+"&"+URLEncoder.encode("salary","UTF-8")+"="+URLEncoder.encode(salary,"UTF-8")
+"&"+URLEncoder.encode("cont","UTF-8")+"="+URLEncoder.encode(cont,"UTF-8");
System.out.println(4);
bufferedWriter.write(Post_data);
System.out.println(5);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
//response for post request//
System.out.println(6);
InputStream inputStream = htpurl.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
String result = "";
String line = "";
System.out.println(6.5);
while((line = bufferedReader.readLine())!= null){
System.out.println(7);
result += line;
}
bufferedReader.close();
inputStream.close();
htpurl.disconnect();
System.out.println(7.5);
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
System.out.println(8);
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
if(result.equals("login done, Welcome USer....")) {
//Getting error here
startActivity(new Intent(MainActivity.this, Insertion.class));
}else {
}
//alertDialog.setMessage(result);
//alertDialog.show();
//OkDialog.setMessage(result);
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
}