Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- LoginLayout.context = getApplicationContext();
- setContentView(R.layout.activity_login_layou);
- un = (EditText) findViewById(R.id.et_un);
- pw = (EditText) findViewById(R.id.et_pw);
- ok = (Button) findViewById(R.id.btn_login);
- finalResult = (TextView) findViewById(R.id.tv_error);
- ok.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- AsyncTaskRunner runner=new AsyncTaskRunner();
- String userName=un.getText().toString();
- String password=pw.getText().toString();
- asyncTask=runner.execute(userName,password);
- try {
- String asyncResultText=asyncTask.get();
- response = asyncResultText.trim();
- } catch (InterruptedException e1) {
- response = e1.getMessage();
- } catch (ExecutionException e1) {
- response = e1.getMessage();
- } catch (Exception e1) {
- response = e1.getMessage();
- }
- finalResult.setText(response);
- }
- });
- }
- public static Context getAppContext() {
- return LoginLayout.context;
- }
- private String resp;
- @Override
- protected String doInBackground(String... params) {
- int count = params.length;
- if(count==2){
- ArrayList<NameValuePair> postParameters = new ArrayList<NameValuePair>();
- postParameters.add(new BasicNameValuePair("username",params[0]));
- postParameters.add(new BasicNameValuePair("password",params[1]));
- String response = null;
- try {
- response = SimpleHttpClient.executeHttpPost("url of the page", postParameters);
- String res = response.toString();
- resp = res.replaceAll("\s+", "");
- } catch (Exception e) {
- e.printStackTrace();
- resp = e.getMessage();
- }
- }else{
- resp="Invalid number of arguments-"+count;
- }
- return resp;
- }
- public static final int HTTP_TIMEOUT = 30 * 1000; // milliseconds
- /** Single instance of our HttpClient */
- private static HttpClient mHttpClient;
- /**
- * Get our single instance of our HttpClient object.
- *
- * @return an HttpClient object with connection parameters set
- */
- private static HttpClient getHttpClient() {
- if (mHttpClient == null) {
- //sets up parameters
- HttpParams params = new BasicHttpParams();
- HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
- HttpProtocolParams.setContentCharset(params, "utf-8");
- params.setBooleanParameter("http.protocol.expect-continue", false);
- //registers schemes for both http and https
- SchemeRegistry registry = new SchemeRegistry();
- registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
- registry.register(new Scheme("https", newSslSocketFactory(), 443));
- ClientConnectionManager manager = new ThreadSafeClientConnManager(params, registry);
- mHttpClient = new DefaultHttpClient(manager, params);
- }
- return mHttpClient;
- }
- private static SSLSocketFactory newSslSocketFactory() {
- try {
- KeyStore trusted = KeyStore.getInstance("BKS");
- InputStream in = LoginLayout.getAppContext().getResources().openRawResource(R.raw.keystore);
- try {
- // Keystore password comes in place of 222222
- trusted.load(in, "222222".toCharArray());
- } finally {
- in.close();
- }
- /*
- * If you use STRICT_HOSTNAME_VERIFIER, the the host name in the URL should match with
- * the host name in the server certificate. In this application it is 192.168.1.3
- *
- * If you do not want to check the host name and simply want to connect to the URL, then use ALLOW_ALL_HOSTNAME_VERIFIER
- *
- */
- //HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
- HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.STRICT_HOSTNAME_VERIFIER;
- SSLSocketFactory socketFactory = new SSLSocketFactory(trusted);
- socketFactory.setHostnameVerifier((X509HostnameVerifier) hostnameVerifier);
- return socketFactory;
- } catch (Exception e) {
- throw new AssertionError(e);
- }
- }
- /**
- * Performs an HTTP Post request to the specified url with the
- * specified parameters.
- *
- * @param url The web address to post the request to
- * @param postParameters The parameters to send via the request
- * @return The result of the request
- * @throws Exception
- */
- public static String executeHttpPost(String url, ArrayList<NameValuePair> postParameters) throws Exception {
- BufferedReader in = null;
- try {
- HttpClient client = getHttpClient();
- HttpPost request = new HttpPost(url);
- UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(postParameters);
- request.setEntity(formEntity);
- HttpResponse response = client.execute(request);
- in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- String result = sb.toString();
- return result;
- }
- finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * Performs an HTTP GET request to the specified url.
- *
- * @param url The web address to post the request to
- * @return The result of the request
- * @throws Exception
- */
- public static String executeHttpGet(String url) throws Exception {
- BufferedReader in = null;
- try {
- HttpClient client = getHttpClient();
- HttpGet request = new HttpGet();
- request.setURI(new URI(url));
- HttpResponse response = client.execute(request);
- in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
- StringBuffer sb = new StringBuffer("");
- String line = "";
- String NL = System.getProperty("line.separator");
- while ((line = in.readLine()) != null) {
- sb.append(line + NL);
- }
- in.close();
- String result = sb.toString();
- return result;
- }
- finally {
- if (in != null) {
- try {
- in.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement