Guest User

Untitled

a guest
Nov 23rd, 2017
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.60 KB | None | 0 0
  1. public class GetTiempoGoogle extends AsyncTask<Void, Void, Void> {
  2.  
  3. private Context context;
  4. String dateStr;
  5.  
  6. @Override
  7. protected Void doInBackground(Void... voids) {
  8. try{
  9. HttpClient httpclient = new DefaultHttpClient();
  10. HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
  11. StatusLine statusLine = response.getStatusLine();
  12. if(statusLine.getStatusCode() == HttpStatus.SC_OK){
  13. DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
  14. dateStr = response.getFirstHeader("Date").getValue();
  15. Date startDate = df.parse(dateStr);
  16. dateStr = String.valueOf(startDate.getTime()/1000);
  17. //Here I do something with the Date String
  18.  
  19. } else{
  20. //Closes the connection.
  21. response.getEntity().getContent().close();
  22. throw new IOException(statusLine.getReasonPhrase());
  23. }
  24. }catch (ClientProtocolException e) {
  25. Log.d("Response", e.getMessage());
  26. }catch (IOException e) {
  27. Log.d("Response", e.getMessage());
  28. } catch (ParseException e) {
  29. e.printStackTrace();
  30. }
  31. return null;
  32. }
  33. // can use UI thread here
  34. protected void onPostExecute(String dateStr) {
  35.  
  36.  
  37.  
  38. }
  39.  
  40. public String getDateStr() {
  41. return dateStr;
  42. }
  43. }
  44.  
  45. GetTiempoGoogle Uconexion = new GetTiempoGoogle();
  46. Uconexion.execute();
  47. String Uconexionapp = Uconexion.getDateStr();
  48. Log.e("UconexionApp",""+Uconexionapp);
Add Comment
Please, Sign In to add comment