Advertisement
Guest User

Untitled

a guest
Jul 30th, 2015
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.22 KB | None | 0 0
  1. package com.example.arunraj.locationtracker;
  2.  
  3. import android.app.ProgressDialog;
  4. import android.content.Intent;
  5. import android.os.AsyncTask;
  6. import android.support.v7.app.ActionBarActivity;
  7. import android.os.Bundle;
  8. import android.view.Menu;
  9. import android.view.MenuItem;
  10. import android.view.View;
  11. import android.widget.Button;
  12. import android.widget.EditText;
  13. import android.widget.Toast;
  14.  
  15. import org.apache.http.HttpResponse;
  16. import org.apache.http.client.HttpClient;
  17. import org.apache.http.client.methods.HttpPost;
  18. import org.apache.http.entity.StringEntity;
  19. import org.apache.http.impl.client.DefaultHttpClient;
  20. import org.json.JSONArray;
  21. import org.json.JSONException;
  22. import org.json.JSONObject;
  23.  
  24. import java.io.BufferedReader;
  25. import java.io.IOException;
  26. import java.io.InputStream;
  27. import java.io.InputStreamReader;
  28.  
  29.  
  30. public class MainActivity extends ActionBarActivity {
  31. String L_URL= "http://safetracker-threetinker.rhcloud.com/api/login";
  32. EditText e_user,e_pwd;
  33. String c_username,c_password;
  34. @Override
  35. protected void onCreate(Bundle savedInstanceState) {
  36. super.onCreate(savedInstanceState);
  37. setContentView(R.layout.activity_login);
  38. e_user = (EditText) findViewById(R.id.e_user);
  39. e_pwd = (EditText) findViewById(R.id.e_pwd);
  40. Button b_login = (Button) findViewById(R.id.b_login);
  41. Button b_register = (Button) findViewById(R.id.b_register);
  42. b_login.setOnClickListener(new View.OnClickListener() {
  43. @Override
  44. public void onClick(View v) {
  45. c_username=e_user.getText().toString();
  46. c_password=e_pwd.getText().toString();
  47. LoginAsyn loginAsyn=new LoginAsyn();
  48. loginAsyn.execute(L_URL);
  49. //
  50. }
  51. });
  52. b_register.setOnClickListener(new View.OnClickListener() {
  53. @Override
  54. public void onClick(View v) {
  55. Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
  56. startActivity(i);
  57. }
  58. });
  59. }
  60. @Override
  61. public boolean onCreateOptionsMenu(Menu menu) {
  62. // Inflate the menu; this adds items to the action bar if it i present.
  63. getMenuInflater().inflate(R.menu.menu_main, menu);
  64. return true;
  65. }
  66.  
  67. @Override
  68. public boolean onOptionsItemSelected(MenuItem item) {
  69. // Handle action bar item clicks here. The action bar will
  70. // automatically handle clicks on the Home/Up button, so long
  71. // as you specify a parent activity in AndroidManifest.xml.
  72. int id = item.getItemId();
  73.  
  74. //noinspection SimplifiableIfStatement
  75. if (id == R.id.action_settings) {
  76. return true;
  77. }
  78.  
  79. return super.onOptionsItemSelected(item);
  80. }
  81. class LoginAsyn extends AsyncTask<String,Void,String>
  82. {
  83. private ProgressDialog nDialog;
  84.  
  85.  
  86.  
  87.  
  88.  
  89.  
  90. @Override
  91. protected void onPreExecute()
  92. {
  93.  
  94. //progress dialog
  95. nDialog = new ProgressDialog(MainActivity.this);
  96. nDialog.setMessage("Loading..");
  97. nDialog.setTitle("Login....");
  98. nDialog.setIndeterminate(false);
  99. nDialog.setCancelable(true);
  100. nDialog.show();
  101. }
  102.  
  103. @Override
  104. protected String doInBackground(String... params) {
  105. InputStream inputStream = null;
  106. String result = "";
  107. try {
  108. // 1. create HttpClient
  109. HttpClient httpclient = new DefaultHttpClient();
  110. // 2. make POST request to the given URL
  111. HttpPost httpPost = new HttpPost(params[0]);
  112. String json = "";
  113. // 3. build jsonObject
  114. JSONObject jsonObject = new JSONObject();
  115. jsonObject.put("name", c_username);
  116. jsonObject.put("password", c_password);
  117.  
  118.  
  119. // 4. convert JSONObject to JSON to String
  120. json = jsonObject.toString();
  121.  
  122. // 5. set json to StringEntity
  123. StringEntity se = new StringEntity(json);
  124. // 6. set httpPost Entity
  125. httpPost.setEntity(se);
  126. // 7. Set some headers to inform server about the type of the content
  127. httpPost.setHeader("Accept", "application/json");
  128. httpPost.setHeader("Content-type", "application/json");
  129. // 8. Execute POST request to the given URL
  130. HttpResponse httpResponse = httpclient.execute(httpPost);
  131.  
  132.  
  133.  
  134.  
  135.  
  136.  
  137.  
  138. inputStream = httpResponse.getEntity().getContent();
  139.  
  140.  
  141. BufferedReader bufferedReader = new BufferedReader( new InputStreamReader(inputStream));
  142. String line = "";
  143. while((line = bufferedReader.readLine()) != null)
  144. result += line;
  145.  
  146. inputStream.close();
  147.  
  148.  
  149.  
  150. } catch (Exception e) {
  151. }
  152.  
  153. // 11. return result
  154. return result;
  155.  
  156. }
  157.  
  158. @Override
  159. protected void onPostExecute(String s) {
  160. super.onPostExecute(s);
  161. if (nDialog.isShowing()) {
  162. nDialog.dismiss();
  163. }
  164.  
  165.  
  166.  
  167. Toast.makeText(MainActivity.this, s, Toast.LENGTH_LONG).show();
  168.  
  169.  
  170. }
  171.  
  172. }
  173.  
  174.  
  175.  
  176. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement