Advertisement
Guest User

Untitled

a guest
May 22nd, 2016
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.69 KB | None | 0 0
  1. package com.example.idan.phpmysql;
  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. import org.apache.http.HttpResponse;
  12. import org.apache.http.client.HttpClient;
  13. import org.apache.http.client.methods.HttpGet;
  14. import org.apache.http.impl.client.DefaultHttpClient;
  15.  
  16. import android.content.Context;
  17. import android.os.AsyncTask;
  18. import android.widget.TextView;
  19.  
  20. public class SigninActivity extends AsyncTask<String,Void,String>{
  21. private TextView statusField,roleField;
  22. private Context context;
  23. private int byGetOrPost = 0;
  24.  
  25. //flag 0 means get and 1 means post.(By default it is get.)
  26. public SigninActivity(Context context,TextView statusField,TextView roleField,int flag) {
  27. this.context = context;
  28. this.statusField = statusField;
  29. this.roleField = roleField;
  30. byGetOrPost = flag;
  31. }
  32.  
  33. protected void onPreExecute(){
  34.  
  35. }
  36.  
  37. @Override
  38. protected String doInBackground(String... arg0) {
  39. if(byGetOrPost == 0){ //means by Get Method
  40.  
  41. try{
  42. String username = (String)arg0[0];
  43. String password = (String)arg0[1];
  44. String link = "http://192.168.56.1/helloworld/test.php?username="+username+"&password="+password;
  45.  
  46. URL url = new URL(link);
  47. HttpClient client = new DefaultHttpClient();
  48. HttpGet request = new HttpGet();
  49. request.setURI(new URI(link));
  50. HttpResponse response = client.execute(request);
  51. BufferedReader in = new BufferedReader(new 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. in.close();
  61. return sb.toString();
  62. }
  63.  
  64. catch(Exception e){
  65. return new String("Exception: " + e.getMessage());
  66. }
  67. }
  68. else{
  69. try{
  70. String username = (String)arg0[0];
  71. String password = (String)arg0[1];
  72.  
  73. String link="http://10.0.2.2/wamp/www/hundred/index.php";
  74. String data = URLEncoder.encode("username", "UTF-8") + "=" + URLEncoder.encode(username, "UTF-8");
  75. data += "&" + URLEncoder.encode("password", "UTF-8") + "=" + 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 InputStreamReader(conn.getInputStream()));
  87.  
  88. StringBuilder sb = new StringBuilder();
  89. String line = null;
  90.  
  91. // Read Server Response
  92. while((line = reader.readLine()) != null)
  93. {
  94. sb.append(line);
  95. break;
  96. }
  97. return sb.toString();
  98. }
  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