Advertisement
Guest User

Untitled

a guest
Mar 1st, 2017
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.66 KB | None | 0 0
  1. /** Called when the activity is first created. */
  2. @Override
  3. public void onCreate(Bundle savedInstanceState) {
  4. super.onCreate(savedInstanceState);
  5. LoginLayout.context = getApplicationContext();
  6. setContentView(R.layout.activity_login_layou);
  7. un = (EditText) findViewById(R.id.et_un);
  8. pw = (EditText) findViewById(R.id.et_pw);
  9. ok = (Button) findViewById(R.id.btn_login);
  10. finalResult = (TextView) findViewById(R.id.tv_error);
  11. ok.setOnClickListener(new View.OnClickListener() {
  12.  
  13. @Override
  14. public void onClick(View v) {
  15. AsyncTaskRunner runner=new AsyncTaskRunner();
  16. String userName=un.getText().toString();
  17. String password=pw.getText().toString();
  18. asyncTask=runner.execute(userName,password);
  19. try {
  20. String asyncResultText=asyncTask.get();
  21. response = asyncResultText.trim();
  22. } catch (InterruptedException e1) {
  23. response = e1.getMessage();
  24. } catch (ExecutionException e1) {
  25. response = e1.getMessage();
  26. } catch (Exception e1) {
  27. response = e1.getMessage();
  28. }
  29. finalResult.setText(response);
  30. }
  31. });
  32. }
  33.  
  34. public static Context getAppContext() {
  35. return LoginLayout.context;
  36. }
  37.  
  38. private String resp;
  39. @Override
  40. protected String doInBackground(String... params) {
  41. int count = params.length;
  42. if(count==2){
  43. ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
  44. postParameters.add(new BasicNameValuePair("username",params[0]));
  45. postParameters.add(new BasicNameValuePair("password",params[1]));
  46. String response = null;
  47. try {
  48. response = SimpleHttpClient.executeHttpPost("url of the page", postParameters);
  49. String res = response.toString();
  50. resp = res.replaceAll("\s+", "");
  51. } catch (Exception e) {
  52. e.printStackTrace();
  53. resp = e.getMessage();
  54. }
  55. }else{
  56. resp="Invalid number of arguments-"+count;
  57. }
  58. return resp;
  59. }
  60.  
  61. public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
  62.  
  63. /** Single instance of our HttpClient */
  64. private static HttpClient mHttpClient;
  65.  
  66. /**
  67. * Get our single instance of our HttpClient object.
  68. *
  69. * @return an HttpClient object with connection parameters set
  70. */
  71. private static HttpClient getHttpClient() {
  72. if (mHttpClient == null) {
  73. //sets up parameters
  74. HttpParams params = new BasicHttpParams();
  75. HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
  76. HttpProtocolParams.setContentCharset(params, "utf-8");
  77. params.setBooleanParameter("http.protocol.expect-continue", false);
  78. //registers schemes for both http and https
  79. SchemeRegistry registry = new SchemeRegistry();
  80. registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
  81. registry.register(new Scheme("https", newSslSocketFactory(), 443));
  82. ClientConnectionManager manager = new ThreadSafeClientConnManager(params, registry);
  83. mHttpClient = new DefaultHttpClient(manager, params);
  84. }
  85. return mHttpClient;
  86. }
  87.  
  88. private static SSLSocketFactory newSslSocketFactory() {
  89. try {
  90. KeyStore trusted = KeyStore.getInstance("BKS");
  91. InputStream in = LoginLayout.getAppContext().getResources().openRawResource(R.raw.keystore);
  92. try {
  93. // Keystore password comes in place of 222222
  94. trusted.load(in, "222222".toCharArray());
  95. } finally {
  96. in.close();
  97. }
  98. /*
  99. * If you use STRICT_HOSTNAME_VERIFIER, the the host name in the URL should match with
  100. * the host name in the server certificate. In this application it is 192.168.1.3
  101. *
  102. * If you do not want to check the host name and simply want to connect to the URL, then use ALLOW_ALL_HOSTNAME_VERIFIER
  103. *
  104. */
  105. //HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
  106. HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
  107. SSLSocketFactory socketFactory = new SSLSocketFactory(trusted);
  108. socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
  109. return socketFactory;
  110. } catch (Exception e) {
  111. throw new AssertionError(e);
  112. }
  113. }
  114.  
  115. /**
  116. * Performs an HTTP Post request to the specified url with the
  117. * specified parameters.
  118. *
  119. * @param url The web address to post the request to
  120. * @param postParameters The parameters to send via the request
  121. * @return The result of the request
  122. * @throws Exception
  123. */
  124. public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
  125. BufferedReader in = null;
  126. try {
  127. HttpClient client = getHttpClient();
  128. HttpPost request = new HttpPost(url);
  129. UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
  130. request.setEntity(formEntity);
  131. HttpResponse response = client.execute(request);
  132. in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  133.  
  134. StringBuffer sb = new StringBuffer("");
  135. String line = "";
  136. String NL = System.getProperty("line.separator");
  137. while ((line = in.readLine()) != null) {
  138. sb.append(line + NL);
  139. }
  140. in.close();
  141.  
  142. String result = sb.toString();
  143. return result;
  144. }
  145. finally {
  146. if (in != null) {
  147. try {
  148. in.close();
  149. } catch (IOException e) {
  150. e.printStackTrace();
  151. }
  152. }
  153. }
  154. }
  155.  
  156. /**
  157. * Performs an HTTP GET request to the specified url.
  158. *
  159. * @param url The web address to post the request to
  160. * @return The result of the request
  161. * @throws Exception
  162. */
  163. public static String executeHttpGet(String url) throws Exception {
  164. BufferedReader in = null;
  165. try {
  166. HttpClient client = getHttpClient();
  167. HttpGet request = new HttpGet();
  168. request.setURI(new URI(url));
  169. HttpResponse response = client.execute(request);
  170. in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
  171.  
  172. StringBuffer sb = new StringBuffer("");
  173. String line = "";
  174. String NL = System.getProperty("line.separator");
  175. while ((line = in.readLine()) != null) {
  176. sb.append(line + NL);
  177. }
  178. in.close();
  179.  
  180. String result = sb.toString();
  181. return result;
  182. }
  183. finally {
  184. if (in != null) {
  185. try {
  186. in.close();
  187. } catch (IOException e) {
  188. e.printStackTrace();
  189. }
  190. }
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement