Advertisement
Guest User

Untitled

a guest
Sep 22nd, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.67 KB | None | 0 0
  1. package com.example.marcin.gumekomagazyn;
  2.  
  3. import java.io.BufferedReader;
  4. import java.io.InputStreamReader;
  5. import java.io.OutputStreamWriter;
  6. import java.net.URI;
  7. import java.net.URL;
  8. import java.net.URLConnection;
  9. import java.net.URLEncoder;
  10.  
  11.  
  12. import org.apache.http.HttpResponse;
  13. import org.apache.http.client.HttpClient;
  14. import org.apache.http.client.methods.HttpGet;
  15. import org.apache.http.impl.client.DefaultHttpClient;
  16.  
  17. import android.content.Context;
  18. import android.os.AsyncTask;
  19. import android.widget.TextView;
  20.  
  21. public class SigninActivity  extends AsyncTask{
  22.     private TextView statusField,roleField;
  23.     private Context context;
  24.     private int byGetOrPost = 0;
  25.  
  26.     //flag 0 means get and 1 means post.(By default it is get.)
  27.     public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) {
  28.         this.context = context;
  29.         this.statusField = statusField;
  30.         this.roleField = roleField;
  31.         byGetOrPost = flag;
  32.     }
  33.  
  34.  
  35.  
  36.     @Override
  37.     protected String doInBackground(String... arg0) {
  38.         if(byGetOrPost == 0){ //means by Get Method
  39.  
  40.             try{
  41.                 String username = (String)arg0[0];
  42.                 String password = (String)arg0[1];
  43.                 String link = "http://localhost:3306/login.php?username="+username+"& password="+password;
  44.  
  45.                 URL url = new URL(link);
  46.                 HttpClient client = new DefaultHttpClient();
  47.                 HttpGet request = new HttpGet();
  48.                 request.setURI(new URI(link));
  49.                 HttpResponse response = client.execute(request);
  50.                 BufferedReader in = new BufferedReader(new
  51.                         InputStreamReader(response.getEntity().getContent()));
  52.  
  53.                 StringBuffer sb = new StringBuffer("");
  54.                 String line="";
  55.  
  56.                 while ((line = in.readLine()) != null) {
  57.                     sb.append(line);
  58.                     break;
  59.                 }
  60.  
  61.                 in.close();
  62.                 return sb.toString();
  63.             } catch(Exception e){
  64.                 return new String("Exception: " + e.getMessage());
  65.             }
  66.         } else{
  67.             try{
  68.                 String username = (String)arg0[0];
  69.                 String password = (String)arg0[1];
  70.  
  71.                 String link="http://localhost:3306/login.php";
  72.                 String data  = URLEncoder.encode("username", "UTF-8") + "=" +
  73.                         URLEncoder.encode(username, "UTF-8");
  74.                 data += "&" + URLEncoder.encode("password", "UTF-8") + "=" +
  75.                         URLEncoder.encode(password, "UTF-8");
  76.  
  77.                 URL url = new URL(link);
  78.                 URLConnection conn = url.openConnection();
  79.  
  80.                 conn.setDoOutput(true);
  81.                 OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
  82.  
  83.                 wr.write( data );
  84.                 wr.flush();
  85.  
  86.                 BufferedReader reader = new BufferedReader(new
  87.                         InputStreamReader(conn.getInputStream()));
  88.  
  89.                 StringBuilder sb = new StringBuilder();
  90.                 String line = null;
  91.  
  92.                 // Read Server Response
  93.                 while((line = reader.readLine()) != null) {
  94.                     sb.append(line);
  95.                     break;
  96.                 }
  97.  
  98.                 return sb.toString();
  99.             } catch(Exception e){
  100.                 return new String("Exception: " + e.getMessage());
  101.             }
  102.         }
  103.     }
  104.  
  105.     @Override
  106.     protected void onPostExecute(String result){
  107.         this.statusField.setText("Login Successful");
  108.         this.roleField.setText(result);
  109.     }
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement