Showing posts with label service. Show all posts
Showing posts with label service. Show all posts

Get ISO country code for the given latitude longitude using GeoNames Web Service using HttpURLConnection

| 0 comments |
The GeoNames geographical database covers all countries and contains over eight million placenames that are available for download free of charge. We can get the iso country code for any given latitude/longitude, using GeoNames webservices; api.geonames.org/countryCode?



CountryCode / reverse geocoding

The iso country code of any given point.
Webservice Type : REST 
Url : api.geonames.org/countryCode?
Parameters : lat,lng, type, lang, radius (buffer in km for closest country in coastal areas, a positive buffer expands the positiv area whereas a negative buffer reduces it);
Result : returns the iso country code for the given latitude/longitude
With the parameter type=xml this service returns an xml document with iso country code and country name. The optional parameter lang can be used to specify the language the country name should be in. JSON output is produced with type=JSON
Example http://api.geonames.org/countryCode?lat=47.03&lng=10.2&username=demo 

Important:

  • Do not use the demo account for your app or your tests. It is only meant for the sample links on the documentation pages. Create your own account instead.
  • The parameter username needs to be passed with each request. The username for your application can be registered here. You will then receive an email with a confirmation link and after you have confirmed the email you can enable your account for the webservice on your account page
  • Dont forget to url encode string parameters containing special characters or spaces. (Faq entry on url encoding)
  • ...
read details: http://www.geonames.org/export/web-services.html


Its a example to get the ISO country code of user entered latitude/longitude.


MainActivity.java
package com.blogspot.android_er.androidgeonames;

import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class MainActivity extends AppCompatActivity {

EditText latText;
EditText lonText;
Button btnFind;
TextView textResult;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latText = (EditText)findViewById(R.id.latText);
lonText = (EditText)findViewById(R.id.lonText);
btnFind = (Button)findViewById(R.id.find);
textResult = (TextView)findViewById(R.id.result);

btnFind.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
String strLat = latText.getText().toString();
String strLon = lonText.getText().toString();

new GeoNamesTask(textResult).execute(strLat, strLon);
}
});
}

private class GeoNamesTask extends AsyncTask<String, Void, String> {
TextView tResult;

public GeoNamesTask(TextView vResult){
tResult = vResult;
tResult.setText("");
}

@Override
protected String doInBackground(String... params) {

/*
Do not use the demo account for your app or your tests.
It is only meant for the sample links on the documentation pages.
Create your own account instead.
*/
String queryString =
"http://api.geonames.org/countryCode?lat=" + params[0]
+ "&lng=" + params[1] + "&username=demo";

String s = "";
try {
s = sendQuery(queryString);
} catch (IOException e) {
e.printStackTrace();
s = e.getMessage();
}
return s;
}

@Override
protected void onPostExecute(String s) {
tResult.setText(s);
}


private String sendQuery(String query) throws IOException {
String result = "";

URL searchURL = new URL(query);

HttpURLConnection httpURLConnection = (HttpURLConnection)searchURL.openConnection();
if(httpURLConnection.getResponseCode() == HttpURLConnection.HTTP_OK){
InputStreamReader inputStreamReader = new InputStreamReader(httpURLConnection.getInputStream());
BufferedReader bufferedReader = new BufferedReader(
inputStreamReader,
8192);

String line = null;
while((line = bufferedReader.readLine()) != null){
result += line;
}

bufferedReader.close();
}

return result;
}
}
}


layout/activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout


android_layout_width="match_parent"
android_layout_height="match_parent"
android_padding="16dp"
android_orientation="vertical"
tools_context=".MainActivity">

<TextView
android_layout_width="wrap_content"
android_layout_height="wrap_content"
android_layout_gravity="center_horizontal"
android_autoLink="web"
android_text="http://android-er.blogspot.com/"
android_textStyle="bold" />

<EditText
android_id="@+id/latText"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_inputType="numberSigned|numberDecimal"
android_hint="Latitude"
android_text="47.03"/>

<EditText
android_id="@+id/lonText"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_inputType="numberSigned|numberDecimal"
android_hint="Longitude"
android_text="10.2"/>

<Button
android_id="@+id/find"
android_layout_width="match_parent"
android_layout_height="wrap_content"
android_text="find"/>

<TextView
android_id="@+id/result"
android_layout_width="match_parent"
android_layout_height="wrap_content" />
</LinearLayout>


Permission of "android.permission.INTERNET" is needed in AndroidManifest.xml
 <uses-permission android_name="android.permission.INTERNET"/>

Next:
GeoNames also provide Java Client for GeoNames Webservices to help developers to easily access the geonames web services with java. Next post "Get ISO country code for the given latitude/longitude, using GeoNames Java Client" show how to use it in Android Studio project.

Related:
- Find addresses of given latitude and longitude using android.location.Geocoder

Read More..

Service Oriented Architecture

| 0 comments |
Hi,

I am an Architect in the Software Industry. I know the need for such a role is hotly debated one. But for now, I can do with such a designation.

Service Oriented Architecture is a buzz-word since almost 3-4 years now. I believe that it has not caught up as fast as it promised to. However, there is definitely benefits that can be derived out of its implementation (if done right) across large enterprises.

My opinion on this is that it is almost a sure failure, if it is taken up in a big bang way for implementation. It has to be planned and taken up in phases with calculated but less risky applicaitons initially. Once the technical success of it is ascertained, the applications of business value need to be taken up to move towards this architecture.

Though lot is said about this in various forums, I am yet to hear anyone say confidently that much of their enterprise has taken to SOA.

Anyones thoughts on this?
Do you think Business critical applications should be taken up to prove the business value to the enterprise for a pilot phase or less critical applications should be piloted, to absorb and risk of failure?
Read More..

Connecting to a web service over a Secure Sockets Layer SSL protocol

| 0 comments |
Android default HttpClinet does not support SSL connections, so if you have a secured web service, you need to connect to it via javax.net.ssl.HttpsURLConnection.
if you want to call a SSL SOAP web service:
String CallWebService(String url,
String soapAction,
String envelope) throws IOException {
URL address=new URL(url);
URLConnection connection=address.openConnection();
HttpsURLConnection post=(HttpsURLConnection)connection;
post.setDoInput(true);
post.setDoOutput(true);
post.setRequestMethod("POST");
post.setRequestProperty("SOAPAction", soapAction);
post.setRequestProperty( "Content-type", "text/xml; charset=utf-8" );
post.setRequestProperty( "Content-Length", String.valueOf(envelope.length()));
post.setReadTimeout(4000);

OutputStream outStream=post.getOutputStream();
Writer out=new OutputStreamWriter(outStream);
out.write(envelope);
out.flush();
out.close();


InputStream inStream = post.getInputStream();
BufferedInputStream in = new BufferedInputStream(inStream,4);
StringBuffer buffer=new StringBuffer();
// read 4 bytes a time
byte[] buffArray=new byte[4];
int c=0;
while((c=in.read(buffArray))!=-1){
for(int i=0;i<c;i++)
buffer.append((char)buffArray[i]);
}

return buffer.toString();
}
Read More..

BlackBerry Enterprise Service safer than Samsung Knox BES chief

| 0 comments |

Blackberry, the struggling Canadian smartphone brand, has started a war of words the leading mobile brand, Samsung, regarding the latters Knox security feature.


John Sims, BlackBerrys new Enterprise Service chief, wrote about how a University in Israel was able to discover glitches in Samsung Knox, and added that the Blackberry Enterprise Service or BES is much more secure.


"With Samsung still battle testing its enterprise platform and fixing security bugs, industries that require the most stringent security needs can trust that theres nothing more secure than a BlackBerry device managed by a BlackBerry Enterprise Server - period. And thats why we are the only enterprise mobility management vendor and handset maker that has received the Department of Defense "Authority to Operate" certification," he stated on BlackBerrys Business Blog.


The new BES chief also mentions that BlackBerry currently owns 61 percent of the large enterprise mobile device management market. While Knox security sytsem remains a Samsung-exclusive feature, available only in Samsung handsets, BlackBerrys BES device management services are available for Android and iOS, and is all set to expand its reach and arrive soon on Windows Phones as well.


John Chen, CEO of BlackBerry, recently confirmed the arrival of BlackBerry Enterprise Server (BES) support for the Windows Phone platform. No launch date was provided however.


The BlackBerry Enterprise Service is a multi-platform device management service that enables company administrators to club the employees personal devices to the firms own email system, and to wipe data in case the device gets lost. It can also controls the devices camera to make sure that it stays inactive during business hours. Apart from these features, security and encryption options are also available, along with unified communications, and BlackBerry Balance work/home space separation.



Read More..