Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class LoginActivity extends Activity {
- EditText ET_NAME, ET_PASS;
- String login_name, login_pass;
- @Override
- protected void onCreate(@Nullable Bundle savedState) {
- super.onCreate(savedState);
- setContentView(R.layout.activity_main);
- ET_NAME = findViewById(R.id.user_name);
- ET_PASS = findViewById(R.id.user_pass);
- }
- public void userLogin(View view) {
- login_name = ET_NAME.getText().toString();
- login_pass = ET_PASS.getText().toString();
- String method = "login";
- BackgroundTask backgroundTask = new BackgroundTask(this);
- backgroundTask.execute(method, login_name, login_pass);
- }
- public void userReg(View view) {
- startActivity(new Intent(this, RegisterActivity.class));
- }
- }
- public class BackgroundTask extends AsyncTask<String, Void, String> {
- AlertDialog alertDialog;
- Context ctx;
- public 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 reg_url = "http://192.168.111.1/webapp/register.php";
- String login_url = "http://192.168.111.1/webapp/login.php";
- String method = params[0];
- if (method.equals("register")) {
- String name = params[1];
- String user_name = params[2];
- String user_pass = params[3];
- try {
- URL url = new URL(reg_url);
- HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
- httpURLConnection.setRequestMethod("POST");
- httpURLConnection.setDoOutput(true);
- OutputStream OS = httpURLConnection.getOutputStream();
- BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS, "UTF-8"));
- String data = URLEncoder.encode("user", "UTF-8") + "=" + URLEncoder.encode(name, "UTF-8") + "&" +
- URLEncoder.encode("user_name", "UTF-8") + "=" + URLEncoder.encode(user_name, "UTF-8") + "&" +
- URLEncoder.encode("user_pass", "UTF-8") + "=" + URLEncoder.encode(user_pass, "UTF-8");
- bufferedWriter.write(data);
- bufferedWriter.flush();
- bufferedWriter.close();
- OS.close();
- InputStream IS = httpURLConnection.getInputStream();
- IS.close();
- return "Register Success...";
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- } else if (method.equals("login")) {
- String login_name = params[1];
- String login_pass = 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("login_name", "UTF-8") + "=" + URLEncoder.encode(login_name, "UTF-8") + "&" +
- URLEncoder.encode("login_pass", "UTF-8") + "=" + URLEncoder.encode(login_pass, "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();
- return response;
- } 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) {
- if (result.equals("Register Success..."))
- Toast.makeText(ctx, result, Toast.LENGTH_SHORT).show();
- else {
- alertDialog.setMessage(result);
- alertDialog.show();
- }
- }
- }
- public class MainActivity extends AppCompatActivity {
- String JSON_STRING;
- String json_string;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- }
- public void getJSON(View view) {
- BackgroundTask backgroundTask = new BackgroundTask(this);
- backgroundTask.execute();
- }
- public void parseJSON(View view) {
- if (json_string == null) {
- Toast.makeText(getApplicationContext(), "First Get JSON", Toast.LENGTH_SHORT).show();
- } else {
- Intent intent = new Intent(getApplicationContext(), DisplayListView.class);
- intent.putExtra("json_data", json_string);
- startActivity(intent);
- }
- }
- class BackgroundTask extends AsyncTask<Void, Void, String> {
- String json_url;
- Context context;
- public BackgroundTask(Context context) {
- this.context = context;
- }
- @Override
- protected String doInBackground(Void... aVoid) {
- try {
- URL url = new URL(json_url);
- HttpURLConnection hucon = (HttpURLConnection) url.openConnection();
- InputStream is = hucon.getInputStream();
- BufferedReader br = new BufferedReader(new InputStreamReader(is));
- StringBuilder sb = new StringBuilder();
- while ((JSON_STRING = br.readLine()) != null) {
- sb.append(JSON_STRING + "n");
- }
- br.close();
- is.close();
- hucon.disconnect();
- return sb.toString().trim();
- } catch (MalformedURLException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- return null;
- }
- @Override
- protected void onPreExecute() {
- json_url = "http://192.168.111.1/jsonparsing/json_get_data.php";
- }
- @Override
- protected void onProgressUpdate(Void... values) {
- super.onProgressUpdate(values);
- }
- @Override
- protected void onPostExecute(String result) {
- TextView textView = (TextView) findViewById(R.id.tv_json_value);
- textView.setText(result);
- json_string = result;
- }
- }
- }
- <? php
- $host = "localhost";
- $user="root";
- $password="";
- $db="app_db";
- $con=mysqli_connect($host,$user,$password,$db);
- $sql="select * from user;";
- $result=mysqli_query($con,$sql);
- $response=array();
- while($row=mysqli_fetch_array($result)){
- array_push($response,array('full_name'=>$row[1],'user_name'=>$row[2],'email'=>$row[3],'phone'=>$row[4]));
- }
- mysqli_close($con);
- echo json_encode(array("server_response"=>$response));
- ?>
Add Comment
Please, Sign In to add comment