Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class MainActivity extends AppCompatActivity {
- EditText userName , password;
- String res = "toomy";
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- userName= (EditText)findViewById(R.id.userName);
- password= (EditText)findViewById(R.id.password);
- }
- public void login(View view) {
- String name = userName.getText().toString();
- String pass = password.getText().toString();
- String type="login";
- BackgroundTask background= new BackgroundTask(this);
- background.execute(type, name, pass).toString();
- }
- public class BackgroundTask extends AsyncTask<String,Void ,String> {
- AlertDialog alertDialog ;
- Context ctx;
- BackgroundTask(Context ctx){
- this.ctx=ctx;
- }
- @Override
- protected void onPreExecute(){
- alertDialog= new AlertDialog.Builder(ctx).create();
- alertDialog.setTitle("Login information...");
- }
- @Override
- protected String doInBackground(String... params) {
- String login_url="http://192.168.173.1/loginf.php";
- String method= params[0];
- if(method.equals("login")){
- String name = params[1];
- String password = params[2];
- try {
- URL url= new URL(login_url);
- HttpURLConnection httpURLConnection= (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- httpURLConnection.setDoInput(true);
- OutputStream outputStream = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
- String data = URLEncoder.encode("name","UTF-8") +"="+ URLEncoder.encode(name,"UTF-8")+"&"+
- URLEncoder.encode("password","UTF-8")+"="+ URLEncoder.encode(password,"UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- outputStream.close();
- InputStream inputStream = httpURLConnection.getInputStream();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
- String response= "";
- String line= "";
- while ((line= bufferedReader.readLine())!= null){
- response+= line;
- }
- bufferedReader.close();
- inputStream.close();
- httpURLConnection.disconnect();
- res = response;
- Log.d("hgjh",res);
- return res;
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }//end of if
- return null;
- }//end of method doInBackground
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(String result) {
- if (result.equals("Login success")) {
- Toast.makeText(ctx, result, Toast.LENGTH_LONG).show();
- Intent in = new Intent(getApplicationContext(), Main.class);
- startActivity(in);
- finish();
- }
- else {
- Toast.makeText(ctx, "login faild", Toast.LENGTH_LONG).show();
- }
- }
- }//end of class BackgroundTask
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement