Copy codeThe Code is as follows: import java. io. BufferedReader;
Import java. io. InputStreamReader;
Import org. apache. http. HttpResponse;
Import org. apache. http. HttpStatus;
Import org. apache. http. client. methods. HttpPost;
Import org. apache. http. entity. StringEntity;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. json. JSONArray;
Import org. json. JSONObject;
Import android. app. Activity;
Import android. content. Context;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. telephony. TelephonyManager;
Import android. telephony. gsm. GsmCellLocation;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. LinearLayout;
Import android. widget. TextView;
Public class Test extends Activity {
Context context = this;
LinearLayout mainView = null;
Button button = null;
TextView TV = null;
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
This. setTitle ("base station + networking + google database positioning ");
MainView = new LinearLayout (this );
MainView. setOrientation (LinearLayout. VERTICAL );
Button = new Button (this );
Button. setText ("positioning test ");
Button. setOnClickListener (new OnClickListener (){
@ Override
Public void onClick (View v ){
(New HttpThread (context). start ();
}
});
MainView. addView (button, new LinearLayout. LayoutParams (-2,-2 ));
TV = new TextView (this );
TV. setText ("Hello! \ N ");
MainView. addView (TV );
SetContentView (mainView );
}
Class HttpThread extends Thread {
TelephonyManager tm = null;
GsmCellLocation gcl = null;
Int cid = 0;
Int lac = 0;
Int mcc = 0;
Int mnc = 0;
StringBuffer sb = null;
Handler handler = new Handler (){
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case 1:
TV. append (sb. toString ());
Break;
}
Super. handleMessage (msg );
}
};
HttpThread (Context context ){
Tm = (TelephonyManager) context. getSystemService (Context. TELEPHONY_SERVICE );
Gcl = (GsmCellLocation) tm. getCellLocation ();
Cid = gcl. getCid ();
Lac = gcl. getLac ();
Mcc = Integer. valueOf (tm. getNetworkOperator (). substring (0, 3 ));
Mnc = Integer. valueOf (tm. getNetworkOperator (). substring (3,5 ));
Sb = new StringBuffer ();
Sb. append ("cid:" + cid + "\ n ");
Sb. append ("lac:" + lac + "\ n ");
Sb. append ("mcc:" + mcc + "\ n ");
Sb. append ("mnc:" + mnc + "\ n ");
}
Public void run (){
Try {
JSONObject jObject = new JSONObject ();
JObject. put ("version", "1.1.0 ");
JObject. put ("host", "maps.google.com ");
JObject. put ("request_address", true );
If (mcc = 460 ){
JObject. put ("address_language", "zh_CN ");
} Else {
JObject. put ("address_language", "en_US ");
}
JSONArray jArray = new JSONArray ();
JSONObject jData = new JSONObject ();
JData. put ("cell_id", cid );
JData. put ("location_area_code", lac );
JData. put ("mobile_country_code", mcc );
JData. put ("mobile_network_code", mnc );
JArray. put (jData );
JObject. put ("cell_towers", jArray );
DefaultHttpClient client = new DefaultHttpClient ();
HttpPost post = new HttpPost ("http://www.google.com/loc/json ");
StringEntity se = new StringEntity (jObject. toString ());
Post. setEntity (se );
HttpResponse resp = client.exe cute (post );
BufferedReader br = null;
If (resp. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK ){
Sb. append ("Networking successful \ n ");
Br = new BufferedReader (new InputStreamReader (resp. getEntity (). getContent ()));
} Else {
Sb. append ("failed to get data online! \ N ");
}
String result = br. readLine ();
While (result! = Null ){
Sb. append (result );
Result = br. readLine ();
}
} Catch (Exception ex ){
Sb. append (ex. getMessage ());
}
Message msg = new Message ();
Msg. what = 1;
Handler. sendMessage (msg );
}
}
}
Required PermissionsCopy codeThe Code is as follows: <uses-permission android: name = "android. permission. INTERNET"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_COARSE_LOCATION"> </uses-permission>