Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class GetTiempoGoogle extends AsyncTask<Void, Void, Void> {
- private Context context;
- String dateStr;
- @Override
- protected Void doInBackground(Void... voids) {
- try{
- HttpClient httpclient = new DefaultHttpClient();
- HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
- StatusLine statusLine = response.getStatusLine();
- if(statusLine.getStatusCode() == HttpStatus.SC_OK){
- DateFormat df = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss z");
- dateStr = response.getFirstHeader("Date").getValue();
- Date startDate = df.parse(dateStr);
- dateStr = String.valueOf(startDate.getTime()/1000);
- //Here I do something with the Date String
- } else{
- //Closes the connection.
- response.getEntity().getContent().close();
- throw new IOException(statusLine.getReasonPhrase());
- }
- }catch (ClientProtocolException e) {
- Log.d("Response", e.getMessage());
- }catch (IOException e) {
- Log.d("Response", e.getMessage());
- } catch (ParseException e) {
- e.printStackTrace();
- }
- return null;
- }
- // can use UI thread here
- protected void onPostExecute(String dateStr) {
- }
- public String getDateStr() {
- return dateStr;
- }
- }
- GetTiempoGoogle Uconexion = new GetTiempoGoogle();
- Uconexion.execute();
- String Uconexionapp = Uconexion.getDateStr();
- Log.e("UconexionApp",""+Uconexionapp);
Add Comment
Please, Sign In to add comment