import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.net.HttpURLConnection;
import java.net.URL;
/**
* GetLocation provide mechanism to get location based on MCC, MNC, LAC, and Cell ID
*
* @author Ali Irawan
* @version 1.0
*/
public class GetLocation {
/**
* Get location based on MCC, MNC, LAC, and Cell ID
*
* @param mcc mobile country code
* @param mnc mobile network code
* @param lac location area code
* @param cellId cell id
* @param requestAddress true if want to get address
* @return JSON String representation of position
* * @throws IOException
*/
@SuppressWarnings("unused")
public static String getLocation(String mcc, String mnc, String lac,
String cellId, boolean requestAddress) throws IOException {
final String data = "{\"cell_towers\": [{\"location_area_code\": \""
+ lac + "\", \"mobile_network_code\": \"" + mnc
+ "\", \"cell_id\": \"" + cellId
+ "\", \"mobile_country_code\": \"" + mcc
+ "\"}], \"version\": \"1.1.0\", \"request_address\": \""
+ requestAddress + "\"}";
final String url = "https://www.google.com/loc/json";
URL anUrl = new URL(url);
HttpURLConnection conn = (HttpURLConnection) anUrl.openConnection();
conn.setRequestMethod("GET");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setUseCaches(false);
conn.setAllowUserInteraction(false);
conn.setRequestProperty("Content-type", "application/jsonrequest");
OutputStream out = conn.getOutputStream();
out.write(data.getBytes());
out.close();
InputStream in = conn.getInputStream();
StringBuilder builder = new StringBuilder();
Reader reader = new InputStreamReader(in);
char[] buf = new char[1024];
int read = 0;
while ((read = reader.read(buf)) >= 0) {
builder.append(String.valueOf(buf, 0, read));
}
in.close();
conn.disconnect();
return builder.toString();
}
}
Example of use:
String location = GetLocation.getLocation("510", "89", "9250", "851", true);
System.out.println(location);
System.out.println(location);
Output:
{
"location":
{
"latitude":-6.2452443,
"longitude":106.7908857,
"address": {
"country":"Indonesia",
"country_code":"ID",
"region":"Jakarta Capital Region",
"city":"Jakarta Capital Region",
"street":"Jalan Gandaria Tengah 4",
"postal_code":"12240"
},
"accuracy":713.0
},
"access_token":"2:C5RroEdSGjSfnq9X:7oAhDHrp_5xzgTeU"
}
1 comment:
This Server is Down.So, can u give any Active Site?
Post a Comment