Get current date and time from Google in Android - android

I have some experience in Android application development. Now we developed an Android application where we need the exact date and time from Google or the internet. Already I test some code from Stack Overflow and from some other sites, but it did not work correctly. The app crashed. Can anyone help me?

Try this:
private long getTime() throws Exception {
String url = "https://time.is/Unix_time_now";
Document doc = Jsoup.parse(new URL(url).openStream(), "UTF-8", url);
String[] tags = new String[] {
"div[id=time_section]",
"div[id=clock0_bg]"
};
Elements elements= doc.select(tags[0]);
for (int i = 0; i <tags.length; i++) {
elements = elements.select(tags[i]);
}
return Long.parseLong(elements.text() + "000");
}
Gradle:
compile 'org.jsoup:jsoup:1.10.2'

This is enough to get what you wanted:
Using the HttpGet, Client and Response, I manage to get a server's current time from the response Date Header. I can call this all the times I want and will get confident responses (Google is almost 100% available and I can trust on getting correct Date and Time)
try{
HttpClient httpclient = new DefaultHttpClient();
HttpResponse response = httpclient.execute(new HttpGet("https://google.com/"));
StatusLine statusLine = response.getStatusLine();
if(statusLine.getStatusCode() == HttpStatus.SC_OK){
String dateStr = response.getFirstHeader("Date").getValue();
//Here I do something with the Date String
System.out.println(dateStr);
} 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());
}

ou can get time from internet time servers using the below program
import java.io.IOException;
import org.apache.commons.net.time.TimeTCPClient;
public final class GetTime {
public static final void main(String[] args) {
try {
TimeTCPClient client = new TimeTCPClient();
try {
// Set timeout of 60 seconds
client.setDefaultTimeout(60000);
// Connecting to time server
// Other time servers can be found at : http://tf.nist.gov/tf-cgi/servers.cgi#
// Make sure that your program NEVER queries a server more frequently than once every 4 seconds
client.connect("nist.time.nosc.us");
System.out.println(client.getDate());
} finally {
client.disconnect();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
1.You would need Apache Commons Net library for this to work. Download the library and add to your project build path.
(Or you can also use the trimmed Apache Commons Net Library here : https://www.dropbox.com/s/bjxjv7phkb8xfhh/commons-net-3.1.jar. This is enough to get time from internet )
2.Run the program. You will get the time printed on your console.

Related

Android http testing with Robolectric

I have an Android app where the main part of the app is the APIcalls.java class where I make http requests to get data from server an display the data in the app.
I wanted to create unit test for this Java class since it's the most part of the app. Here is the method for getting the data from server:
StringBuilder sb = new StringBuilder();
try {
httpclient = new DefaultHttpClient();
Httpget httpget = new HttpGet(url);
HttpEntity entity = null;
try {
HttpResponse response = httpclient.execute(httpget);
entity = response.getEntity();
} catch (Exception e) {
Log.d("Exception", e);
}
if (entity != null) {
InputStream is = null;
is = entity.getContent();
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
reader.close();
} catch (IOException e) {
throw e;
} catch (RuntimeException e) {
httpget.abort();
throw e;
} finally {
is.close();
}
httpclient.getConnectionManager().shutdown();
}
} catch (Exception e) {
Log.d("Exception", e);
}
String result = sb.toString().trim();
return result;
I thought I can make simple API calls from the tests like this:
api.get("www.example.com")
But every time I make some http calls from the tests, I get an error:
Unexpected HTTP call GET
I know I am doing something wrong here, but can anyone tell me how can I properly test this class in Android?
Thank you for all your answers but I found what I was looking for.
I wanted to test real HTTP calls.
By adding Robolectric.getFakeHttpLayer().interceptHttpRequests(false);
you tell Robolectric not to intercept these requests and it allows you to make real HTTP calls
Robolectric provides some helper methods to mock http response for DefaultHttpClient. If you use DefaultHttpClient without using those methods, you would get a warning message.
Here is an example of how to mock http response:
#RunWith(RobolectricTestRunner.class)
public class ApiTest {
#Test
public void test() {
Api api = new Api();
Robolectric.addPendingHttpResponse(200, "dummy");
String responseBody = api.get("www.example.com");
assertThat(responseBody, is("dummy"));
}
}
You can find more examples by looking at Robolectric's test codes.
I answered another version of this same question, but...
What you have here is not using anything from Android, so Robolectric is basically irrelevant. This is all standard Java and the Apache HTTP library. You simply need a mocking framework and dependency injection to simulate the HttpClient (see my other answer for links). It doesn't have network access while unit testing, and so it fails.
When testing classes that use parts of the Android framework, you can use Robolectric (or similar) to mock or simulate Android.jar since your unit testing framework isn't going to have access to that either.

Symfony2 firewall issue Android

Let me explain my situation, I developed a complete backend for an Android application in Symfony2.1 that works perfectly, now I'm trying to create the Android app part, for that I created a firewall with http_basic authentication that ensures that my users are correctly authenticated and authorized, I actually can use my app and be logged, but if I try to retrieve any page behind the firewall a get a 404 error.
I don't want to use any external bundle, I just want to send my user/pass on every request since my app makes just three httpclient calls but I don't know hoy to get access granted on every request.
Here is part of my code, feel free to ask :)
Thanks in advance!
My Android http call:
#Override protected String doInBackground(Void... arg0) {
HttpClient httpClient = new DefaultHttpClient();
// construir peticion get
HttpGet httpGet = new HttpGet("http://www.somewebsite.com/api/login/");
httpGet.addHeader(BasicScheme.authenticate(
new UsernamePasswordCredentials(loguser, passw), "UTF-8",
false));
try {
response = httpClient.execute(httpGet);
reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
builder = new StringBuilder();
for(String line = null; (line = reader.readLine()) != null;){
builder.append(line).append("\n");
}
} catch (Exception e) {
e.printStackTrace();
alert("Error de protocolo", "Lo sentimos, ha ocurrido un error");
}
return builder.toString();
}
My firewall
api:
pattern: ^/api/.*
provider: app_user
http_basic:
realm: "API"
access_control:
- { path: ^/api-registro/, role: IS_AUTHENTICATED_ANONYMOUSLY }
- { path: ^/api/.*, role: ROLE_API }
providers:
app_user:
entity: { class: Alood\BackBundle\Entity\Usuario, property: user }
encoders:
Alood\BackBundle\Entity\Usuario: plaintext
My Controller
public function apiLoginAction()
{
$peticion = $this->getRequest();
$sesion = $peticion->getSession();
$usuario = $this->get('security.context')->getToken()->getUser();
$error = $peticion->attributes->get(SecurityContext::AUTHENTICATION_ERROR,
$sesion->get(SecurityContext::AUTHENTICATION_ERROR));
$securityContext = $this->container->get('security.context');
if( $securityContext->isGranted('IS_AUTHENTICATED_FULLY')){
$texto["user"] = $usuario->getUser();
return new JsonResponse($texto);
}
}
Note: If I repeat the same steps in a different function of my controller I get a problem in my android app and I don't know how to solve this.
It happened to be a typo issue in my routing.yml so if its useful for somebody I must say this works properly.

Geocoder - Service not available on Android 2.3

I know this question has been asked several times, but still I'm getting this error.
Tried both on emulator and actual device, triend chahging emulator's target to Google API and also changed the target build of the project to Google API's
Need help on these :( Thanks!
I hope you solved it by now. As you said, there are many thread to it. After studying all the threads the answer I got was that Geocoder doesn't always return a value. You can try to send a request 3 times in a for loop. I might be able to return atleast once. If not then, their might be a connection issue or can be other issues like server does not reply to your request.
I had a while loop as well but I used to try it maximum for 10 times. Sometimes, it never returned anything even if it was connected to internet. Then, I used this much more reliable way to get the address everytime:
I used to get the latitude and longitude and then request google servers, to reply with a JSON object containing various information about the location co-ordinates. This way of getting address string does not require Geocoder. Here is the function:
public JSONObject getLocationInfo() {
HttpGet httpGet = new HttpGet("http://maps.google.com/maps/api/geocode/json?latlng="+lat+","+lng+"&sensor=true");
HttpClient client = new DefaultHttpClient();
HttpResponse response;
StringBuilder stringBuilder = new StringBuilder();
try {
response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream stream = entity.getContent();
int b;
while ((b = stream.read()) != -1) {
stringBuilder.append((char) b);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
JSONObject jsonObject = new JSONObject();
try {
jsonObject = new JSONObject(stringBuilder.toString());
} catch (JSONException e) {
e.printStackTrace();
}
return jsonObject;
}
I called it as follows:
JSONObject ret = getLocationInfo();
JSONObject location;
String location_string;
try {
location = ret.getJSONArray("results").getJSONObject(0);
location_string = location.getString("formatted_address");
Log.d("test", "formattted address:" + location_string);
} catch (JSONException e1) {
e1.printStackTrace();
}
Hope this helps. I was also tired of relying on Geocoder. This worked for me. Though it might be just a little slower than geocoder. For testing its functionality, you can just place in the URL with the lat and longitude coordinates you are having. Try to see the returned JSON object in a web browser. You'll see how you can extract the address string. Try and read these threads as well:
Geocoder doesn't always return a value and geocoder.getFromLocationName returns only null

How does RequestFactory know what Android user is logged in

I have an Android app that successfully uses RequestFactory to manipulate Entities in AppEngine Datastore.
In my AppEngine service, I want to use
UserService userService = UserServiceFactory.getUserService();
User user = userService.getCurrentUser();
to get information about the current Android user, but getCurrentUser() returns null.
How does my Android app let RequestFactory know who is logged in?
This question applies to dev mode and production mode.
Thanks.
There is a not-so-obvious answer found in the old AppEngine Connected Android Eclipse wizard code, which is no longer available in the GPE (see Util.java of this wizard generated code). It includes the following:
T requestFactory = RequestFactorySource.create(factoryClass);
requestFactory.initialize(new SimpleEventBus(),
new AndroidRequestTransport(uri, authCookie));
with
public AndroidRequestTransport(URI uri, String cookie) {
this.uri = uri;
this.cookie = cookie;
}
public void send(String payload, TransportReceiver receiver) {
HttpClient client = new DefaultHttpClient();
HttpPost post = new HttpPost();
post.setHeader("Content-Type", "application/json;charset=UTF-8");
post.setHeader("Cookie", cookie);
post.setURI(uri);
Throwable ex;
try {
post.setEntity(new StringEntity(payload, "UTF-8"));
HttpResponse response = client.execute(post);
if (200 == response.getStatusLine().getStatusCode()) {
String contents = readStreamAsString(response.getEntity().getContent());
receiver.onTransportSuccess(contents);
} else {
receiver.onTransportFailure(new ServerFailure(response.getStatusLine()
.getReasonPhrase()));
}
return;
} catch (UnsupportedEncodingException e) {
ex = e;
} catch (ClientProtocolException e) {
ex = e;
} catch (IOException e) {
ex = e;
}
receiver.onTransportFailure(new ServerFailure(ex.getMessage()));
}
Using that code from the GPE wizard did the trick for me.

How to use webview or open URL in AndEngine - Android

I'm utilizing a game engine called AndEngine (which I'm completely new to) in my Android app. I need to load a different URL from the application based on what position an onscreen joystick is in (uploading to a .cgi server). The dilemma is that I cannot open a URL connection! This may seem simple, but I've looked everywhere, tried multiple solutions and nothing's worked. In basic Android, I've always used a WebView (loadUrl() method), and it worked well. However, I have no idea to how to create a webview while also using AndEngine. My preference is that the connection did not show (loaded underneath the AndEngine scene?) because I will need the screen for other things. I've also tried other solutions. I just tried this code, but when I checked the server, nothing was opened:
#Override
public void onLoadResources() {
//methods n/a to this question
try {
URL url = new URL(setUrl);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
readStream(con.getInputStream());
} catch (Exception e) {
e.printStackTrace();
}
return scene; // AndEngine return statement
}
private void readStream(InputStream in) {
BufferedReader reader = null;
try {
reader = new BufferedReader(new InputStreamReader(in));
String line = "";
while ((line = reader.readLine()) != null) {
System.out.println(line);
}
} catch (IOException e) {
e.printStackTrace();
} finally {
if (reader != null) {
try {
reader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
**I've tried using the HTTPConnection class before (without AndEngine) to open up a URL, but to no avail. So it may be that I was just doing something wrong here. Using AndEngine GLES2. If more info is needed, let me know (this is my first question on SO).
Also tried setting up my .xml layout on AndEngine using
#Override
protected int getLayoutID() {
return R.layout.main;
}
but it says: "The method getLayoutID() of type Control must override or implement a supertype method"
Edit in response to Nicolas Gramlich: Internet permissions were set and compiler was originally at 1.6. Still don't know what the issue is.
xml
<manifest xlmns:android...>
...
<uses-permission android:name="android.permission.INTERNET"/>
</manifest>
Set java compiler compliance to 1.6
I solved my issue. I had to run all network operations on a thread separate from the main one (else it will throw a NetworkOnMainThread exception). I don't know why nothing else worked, but this did the trick! Here I'm creating a new thread with the action I want to perform, and then starting it after exceptions are taken care of. I found my answer here
new Thread(new Runnable() {
public void run() {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpGet httpGet = new HttpGet("your_url");
try {
HttpResponse response = httpClient.execute(httpGet, localContext);
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}).start();

Categories

Resources