Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2016
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 15.69 KB | None | 0 0
  1. package com.example.javi.myapplication2;
  2.  
  3. import android.util.Log;
  4. import org.apache.http.HttpEntity;
  5. import org.apache.http.HttpResponse;
  6. import org.apache.http.client.ClientProtocolException;
  7. import org.apache.http.client.entity.UrlEncodedFormEntity;
  8. import org.apache.http.client.methods.HttpGet;
  9. import org.apache.http.client.methods.HttpPost;
  10. import org.apache.http.client.utils.URLEncodedUtils;
  11. import org.apache.http.impl.client.DefaultHttpClient;
  12. import org.json.JSONException;
  13. import org.json.JSONObject;
  14.  
  15. import java.io.BufferedReader;
  16. import java.io.IOException;
  17. import java.io.InputStream;
  18. import java.io.InputStreamReader;
  19. import java.io.UnsupportedEncodingException;
  20. import java.util.List;
  21.  
  22. public class JSONParser {
  23.  
  24. static InputStream is = null;
  25. static JSONObject jObj = null;
  26. static String json = "";
  27.  
  28. // constructor
  29. public JSONParser() {
  30.  
  31. }
  32.  
  33.  
  34. public JSONObject getJSONFromUrl(final String url) {
  35.  
  36. // Making HTTP request
  37. try {
  38. // Construct the client and the HTTP request.
  39. DefaultHttpClient httpClient = new DefaultHttpClient();
  40. HttpPost httpPost = new HttpPost(url);
  41.  
  42. // Execute the POST request and store the response locally.
  43. HttpResponse httpResponse = httpClient.execute(httpPost);
  44. // Extract data from the response.
  45. HttpEntity httpEntity = httpResponse.getEntity();
  46. // Open an inputStream with the data content.
  47. is = httpEntity.getContent();
  48.  
  49. } catch (UnsupportedEncodingException e) {
  50. e.printStackTrace();
  51. } catch (ClientProtocolException e) {
  52. e.printStackTrace();
  53. } catch (IOException e) {
  54. e.printStackTrace();
  55. }
  56.  
  57. try {
  58. // Create a BufferedReader to parse through the inputStream.
  59. BufferedReader reader = new BufferedReader(new InputStreamReader(
  60. is, "iso-8859-1"), 8);
  61. // Declare a string builder to help with the parsing.
  62. StringBuilder sb = new StringBuilder();
  63. // Declare a string to store the JSON object data in string form.
  64. String line = null;
  65.  
  66. package com.example.javi.myapplication2;
  67.  
  68. import android.util.Log;
  69. import org.apache.http.HttpEntity;
  70. import org.apache.http.HttpResponse;
  71. import org.apache.http.client.ClientProtocolException;
  72. import org.apache.http.client.entity.UrlEncodedFormEntity;
  73. import org.apache.http.client.methods.HttpGet;
  74. import org.apache.http.client.methods.HttpPost;
  75. import org.apache.http.client.utils.URLEncodedUtils;
  76. import org.apache.http.impl.client.DefaultHttpClient;
  77. import org.json.JSONException;
  78. import org.json.JSONObject;
  79.  
  80. import java.io.BufferedReader;
  81. import java.io.IOException;
  82. import java.io.InputStream;
  83. import java.io.InputStreamReader;
  84. import java.io.UnsupportedEncodingException;
  85. import java.util.List;
  86.  
  87. public class JSONParser {
  88.  
  89. static InputStream is = null;
  90. static JSONObject jObj = null;
  91. static String json = "";
  92.  
  93. // constructor
  94. public JSONParser() {
  95.  
  96. }
  97.  
  98.  
  99. public JSONObject getJSONFromUrl(final String url) {
  100.  
  101. // Making HTTP request
  102. try {
  103. // Construct the client and the HTTP request.
  104. DefaultHttpClient httpClient = new DefaultHttpClient();
  105. HttpPost httpPost = new HttpPost(url);
  106.  
  107. // Execute the POST request and store the response locally.
  108. HttpResponse httpResponse = httpClient.execute(httpPost);
  109. // Extract data from the response.
  110. HttpEntity httpEntity = httpResponse.getEntity();
  111. // Open an inputStream with the data content.
  112. is = httpEntity.getContent();
  113.  
  114. } catch (UnsupportedEncodingException e) {
  115. e.printStackTrace();
  116. } catch (ClientProtocolException e) {
  117. e.printStackTrace();
  118. } catch (IOException e) {
  119. e.printStackTrace();
  120. }
  121.  
  122. try {
  123. // Create a BufferedReader to parse through the inputStream.
  124. BufferedReader reader = new BufferedReader(new InputStreamReader(
  125. is, "iso-8859-1"), 8);
  126. // Declare a string builder to help with the parsing.
  127. StringBuilder sb = new StringBuilder();
  128. // Declare a string to store the JSON object data in string form.
  129. String line = null;
  130.  
  131. package com.example.javi.myapplication2;
  132.  
  133. import java.util.ArrayList;
  134. import java.util.List;
  135.  
  136. import org.apache.http.NameValuePair;
  137. import org.apache.http.message.BasicNameValuePair;
  138. import org.json.JSONException;
  139. import org.json.JSONObject;
  140.  
  141. import android.app.Activity;
  142. import android.app.ProgressDialog;
  143. import android.content.Intent;
  144. import android.content.SharedPreferences;
  145. import android.content.SharedPreferences.Editor;
  146. import android.os.AsyncTask;
  147. import android.os.Bundle;
  148. import android.preference.PreferenceManager;
  149. import android.util.Log;
  150. import android.view.View;
  151. import android.view.View.OnClickListener;
  152. import android.widget.Button;
  153. import android.widget.CheckBox;
  154. import android.widget.EditText;
  155. import android.widget.Toast;
  156.  
  157. public class Login extends Activity implements OnClickListener {
  158.  
  159. private EditText user, pass;
  160. private Button mSubmit, mRegister;
  161. private CheckBox check;
  162.  
  163.  
  164. private ProgressDialog pDialog;
  165.  
  166. // Clase JSONParser
  167. JSONParser jsonParser = new JSONParser();
  168.  
  169.  
  170. // si trabajan de manera local "localhost" :
  171. // En windows tienen que ir, run CMD > ipconfig
  172. // buscar su IP
  173. // y poner de la siguiente manera
  174. // "http://xxx.xxx.x.x:1234/cas/login.php";
  175.  
  176. private static final String LOGIN_URL = "http://saveds.esy.es/cas/login.php";
  177.  
  178. // La respuesta del JSON es
  179. private static final String TAG_SUCCESS = "success";
  180. private static final String TAG_MESSAGE = "message";
  181.  
  182. @Override
  183. protected void onCreate(Bundle savedInstanceState) {
  184. // TODO Auto-generated method stub
  185. super.onCreate(savedInstanceState);
  186. setContentView(R.layout.login);
  187.  
  188. // setup input fields
  189. user = (EditText) findViewById(R.id.username);
  190. pass = (EditText) findViewById(R.id.password);
  191. check = (CheckBox) findViewById(R.id.rememberme);
  192.  
  193. // setup buttons
  194. mSubmit = (Button) findViewById(R.id.login);
  195. mRegister = (Button) findViewById(R.id.register);
  196.  
  197. // register listeners
  198. mSubmit.setOnClickListener(this);
  199. mRegister.setOnClickListener(this);
  200.  
  201. }
  202.  
  203. @Override
  204. public void onClick(View v) {
  205. // TODO Auto-generated method stub
  206. switch (v.getId()) {
  207. case R.id.login:
  208. new AttemptLogin().execute();
  209. break;
  210. case R.id.register:
  211. Intent i = new Intent(this, Register.class);
  212. startActivity(i);
  213. break;
  214.  
  215. default:
  216. break;
  217. }
  218. }
  219.  
  220. class AttemptLogin extends AsyncTask<String, String, String> {
  221.  
  222. @Override
  223. protected void onPreExecute() {
  224. super.onPreExecute();
  225. pDialog = new ProgressDialog(Login.this);
  226. pDialog.setMessage("Attempting login...");
  227. pDialog.setIndeterminate(false);
  228. pDialog.setCancelable(true);
  229. pDialog.show();
  230. }
  231.  
  232. @Override
  233. protected String doInBackground(String... args) {
  234. int success;
  235. String username = user.getText().toString();
  236. String password = pass.getText().toString();
  237. String rememberme = check.getText().toString();
  238.  
  239. try {
  240. // Building Parameters
  241. List params = new ArrayList();
  242. params.add(new BasicNameValuePair("username", username));
  243. params.add(new BasicNameValuePair("password", password));
  244. params.add(new BasicNameValuePair("rememberme", rememberme));
  245.  
  246.  
  247. Log.d("request!", "starting");
  248. // getting product details by making HTTP request
  249. JSONObject json = jsonParser.makeHttpRequest(LOGIN_URL, "POST",
  250. params);
  251.  
  252. // check your log for json response
  253. Log.d("Login attempt", json.toString());
  254.  
  255. // json success tag
  256. success = json.getInt(TAG_SUCCESS);
  257. if (success == 1) {
  258. Log.d("Login Successful!", json.toString());
  259. // save user data
  260. SharedPreferences sp = PreferenceManager
  261. .getDefaultSharedPreferences(Login.this);
  262. Editor edit = sp.edit();
  263. edit.putString("username", username);
  264. edit.commit();
  265.  
  266. Intent i = new Intent(Login.this, ReadComments.class);
  267. finish();
  268. startActivity(i);
  269. return json.getString(TAG_MESSAGE);
  270. } else {
  271. Log.d("Login Failure!", json.getString(TAG_MESSAGE));
  272. return json.getString(TAG_MESSAGE);
  273. }
  274. } catch (JSONException e) {
  275. e.printStackTrace();
  276. }
  277.  
  278. return null;
  279.  
  280. }
  281.  
  282. protected void onPostExecute(String file_url) {
  283. // dismiss the dialog once product deleted
  284. pDialog.dismiss();
  285. if (file_url != null) {
  286. Toast.makeText(Login.this, file_url, Toast.LENGTH_LONG).show();
  287. }
  288. }
  289. }
  290. }
  291.  
  292. package com.example.javi.myapplication2;
  293.  
  294. import java.util.ArrayList;
  295. import java.util.List;
  296.  
  297. import org.apache.http.message.BasicNameValuePair;
  298. import org.json.JSONException;
  299. import org.json.JSONObject;
  300. import android.app.Activity;
  301. import android.app.ProgressDialog;
  302. import android.os.AsyncTask;
  303. import android.os.Bundle;
  304. import android.util.Log;
  305. import android.view.View;
  306. import android.view.View.OnClickListener;
  307. import android.widget.Button;
  308. import android.widget.EditText;
  309. import android.widget.Toast;
  310.  
  311. public class ReadComments extends Activity implements OnClickListener{
  312.  
  313. private Button logout;
  314.  
  315. // Progress Dialog
  316. private ProgressDialog pDialog;
  317.  
  318. // JSON parser class
  319. JSONParser jsonParser = new JSONParser();
  320.  
  321. //si lo trabajan de manera local en xxx.xxx.x.x va su ip local
  322. // private static final String REGISTER_URL = "http://xxx.xxx.x.x:1234/cas/register.php";
  323.  
  324. //testing on Emulator:
  325. private static final String WELCOME_URL = "http://saveds.esy.es/cas/welcome.php";
  326.  
  327. //ids
  328. private static final String TAG_SUCCESS = "success";
  329. private static final String TAG_MESSAGE = "message";
  330.  
  331. @Override
  332. protected void onCreate(Bundle savedInstanceState) {
  333. // TODO Auto-generated method stub
  334. super.onCreate(savedInstanceState);
  335. setContentView(R.layout.read_comments);
  336.  
  337. logout=(Button)findViewById(R.id.logout);
  338. logout.setOnClickListener(this);
  339.  
  340. }
  341.  
  342. @Override
  343. public void onClick(View v) {
  344. // TODO Auto-generated method stub
  345.  
  346. new Logoutclass().execute();
  347.  
  348. }
  349.  
  350. class Logoutclass extends AsyncTask<String, String, String> {
  351.  
  352. @Override
  353. protected void onPreExecute() {
  354. super.onPreExecute();
  355. pDialog = new ProgressDialog(ReadComments.this);
  356. pDialog.setMessage("Cerrando sesi贸n...");
  357. pDialog.setIndeterminate(false);
  358. pDialog.setCancelable(true);
  359. pDialog.show();
  360. }
  361.  
  362. @Override
  363. protected String doInBackground(String... args) {
  364. // TODO Auto-generated method stub
  365. // Check for success tag
  366. int success;
  367. String out=logout.getText().toString();
  368.  
  369. try {
  370. // Building Parameters
  371. List params = new ArrayList();
  372. params.add(new BasicNameValuePair("username", out));
  373.  
  374. Log.d("request!", "starting");
  375.  
  376. //Posting user data to script
  377. JSONObject json = jsonParser.makeHttpRequest(
  378. WELCOME_URL, "POST", params);
  379.  
  380. // full json response
  381. Log.d("Cerrando sesi贸n", json.toString());
  382.  
  383. // json success element
  384. success = json.getInt(TAG_SUCCESS);
  385. if (success == 1) {
  386. Log.d("Sesi贸n cerrada", json.toString());
  387. finish();
  388. return json.getString(TAG_MESSAGE);
  389. }else{
  390. Log.d("Fallo de cerrar sesi贸n", json.getString(TAG_MESSAGE));
  391. return json.getString(TAG_MESSAGE);
  392.  
  393. }
  394. } catch (JSONException e) {
  395. e.printStackTrace();
  396. }
  397.  
  398. return null;
  399.  
  400. }
  401.  
  402. protected void onPostExecute(String file_url) {
  403. // dismiss the dialog once product deleted
  404. pDialog.dismiss();
  405. if (file_url != null){
  406. Toast.makeText(ReadComments.this, file_url, Toast.LENGTH_LONG).show();
  407. }
  408. }
  409. }
  410. }
  411.  
  412. package com.example.javi.myapplication2;
  413.  
  414. import java.util.ArrayList;
  415. import java.util.List;
  416.  
  417. import org.apache.http.message.BasicNameValuePair;
  418. import org.json.JSONException;
  419. import org.json.JSONObject;
  420. import android.app.Activity;
  421. import android.app.ProgressDialog;
  422. import android.os.AsyncTask;
  423. import android.os.Bundle;
  424. import android.util.Log;
  425. import android.view.View;
  426. import android.view.View.OnClickListener;
  427. import android.widget.Button;
  428. import android.widget.EditText;
  429. import android.widget.Toast;
  430.  
  431. public class Register extends Activity implements OnClickListener{
  432. private EditText user, pass, nam, mail ;
  433. private Button mRegister;
  434.  
  435. // Progress Dialog
  436. private ProgressDialog pDialog;
  437.  
  438. // JSON parser class
  439. JSONParser jsonParser = new JSONParser();
  440.  
  441. //si lo trabajan de manera local en xxx.xxx.x.x va su ip local
  442. // private static final String REGISTER_URL = "http://xxx.xxx.x.x:1234/cas/register.php";
  443.  
  444. //testing on Emulator:
  445. private static final String REGISTER_URL = "http://saveds.esy.es/cas/register.php";
  446.  
  447. //ids
  448. private static final String TAG_SUCCESS = "success";
  449. private static final String TAG_MESSAGE = "message";
  450.  
  451. @Override
  452. protected void onCreate(Bundle savedInstanceState) {
  453. // TODO Auto-generated method stub
  454. super.onCreate(savedInstanceState);
  455. setContentView(R.layout.register);
  456.  
  457. user = (EditText)findViewById(R.id.username);
  458. pass = (EditText)findViewById(R.id.password);
  459. nam = (EditText)findViewById(R.id.name);
  460. mail = (EditText)findViewById(R.id.email);
  461.  
  462.  
  463. mRegister = (Button)findViewById(R.id.register);
  464. mRegister.setOnClickListener(this);
  465.  
  466. }
  467.  
  468. @Override
  469. public void onClick(View v) {
  470. // TODO Auto-generated method stub
  471.  
  472. new CreateUser().execute();
  473.  
  474. }
  475.  
  476. class CreateUser extends AsyncTask<String, String, String> {
  477.  
  478. @Override
  479. protected void onPreExecute() {
  480. super.onPreExecute();
  481. pDialog = new ProgressDialog(Register.this);
  482. pDialog.setMessage("Creating User...");
  483. pDialog.setIndeterminate(false);
  484. pDialog.setCancelable(true);
  485. pDialog.show();
  486. }
  487.  
  488. @Override
  489. protected String doInBackground(String... args) {
  490. // TODO Auto-generated method stub
  491. // Check for success tag
  492. int success;
  493. String email=mail.getText().toString();
  494. String name = nam.getText().toString();
  495. String username = user.getText().toString();
  496. String password = pass.getText().toString();
  497.  
  498. try {
  499. // Building Parameters
  500. List params = new ArrayList();
  501. params.add(new BasicNameValuePair("username", username));
  502. params.add(new BasicNameValuePair("password", password));
  503. params.add(new BasicNameValuePair("name", name));
  504. params.add(new BasicNameValuePair("email", email));
  505.  
  506. Log.d("request!", "starting");
  507.  
  508. //Posting user data to script
  509. JSONObject json = jsonParser.makeHttpRequest(
  510. REGISTER_URL, "POST", params);
  511.  
  512. // full json response
  513. Log.d("Registering attempt", json.toString());
  514.  
  515. // json success element
  516. success = json.getInt(TAG_SUCCESS);
  517. if (success == 1) {
  518. Log.d("User Created!", json.toString());
  519. finish();
  520. return json.getString(TAG_MESSAGE);
  521. }else{
  522. Log.d("Registering Failure!", json.getString(TAG_MESSAGE));
  523. return json.getString(TAG_MESSAGE);
  524.  
  525. }
  526. } catch (JSONException e) {
  527. e.printStackTrace();
  528. }
  529.  
  530. return null;
  531.  
  532. }
  533.  
  534. protected void onPostExecute(String file_url) {
  535. // dismiss the dialog once product deleted
  536. pDialog.dismiss();
  537. if (file_url != null){
  538. Toast.makeText(Register.this, file_url, Toast.LENGTH_LONG).show();
  539. }
  540. }
  541. }
  542. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement