Android: linkage between provincial, municipal, and county levels (based on json and spring)
1. See ":
Ii. program code:
1. MainActivity. java
Package com. loveplusplus. loader. demo. ui;
Import org. json. JSONArray;
Import org. json. JSONException;
Import org. json. JSONObject;
Import android. app. Activity;
Import android. OS. Bundle;
Import android. OS. Handler;
Import android. OS. Message;
Import android. view. View;
Import android. widget. AdapterView;
Import android. widget. AdapterView. OnItemSelectedListener;
Import android. widget. ArrayAdapter;
Import android. widget. Spinner;
Import android. widget. Toast;
Import com. loveplusplus. loader. demo. R;
Import com. loveplusplus. loader. demo. util. HttpService;
/**
* Provincial, municipal, and county-level linkages
*
* October 30, 2014 15:22:04
*
* @ Author YJBo
*
*/
Public class MainActivity extends Activity {
Private static final String HOST_URL = "http: // 218.241.233.121/yqw ";
Private static final String PROVINCE_URL = "http: // 218.241.233.121/yqw/w/c/post? App = join & mdl = web & act = provinces & type = json ";
Private static final String CITY_URI = HOST_URL
+ "/W/c/post? App = join & mdl = web & type = json & act = citys & col. province = ";
Private static final String COUNTY_URL = HOST_URL
+ "/W/c/post? App = join & mdl = web & type = json & act = areas & col. city = ";
// Col. province
Private Spinner provinceSpinner;
Private Spinner citySpinner;
Private Spinner countySpinner;
// Province, city, and county Arrays
Private String [] Province;
Private String [] ProvinceCode;
Private String json;
// City ---
Private String json2;
Private String [] City;
Private String [] CityCode;
// County
Private String json3;
Private String [] Country;
Private String [] CountryCode;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
// Obtain the province name
LoadProvices ();
}
Private void loadProvices (){
New Thread (new Runnable (){
@ Override
Public void run (){
Try {
// Enter the page to obtain all provinces
Json = HttpService. postParamAndFileOB (PROVINCE_URL );
MHandler. sendEmptyMessage (1 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}). Start ();
}
// City --- New
Private void loadCitys2 (final String provinceCode ){
If (null = provinceCode)
Return;
New Thread (new Runnable (){
@ Override
Public void run (){
Try {
// Obtain all cities in a province
Json2 = HttpService. postParamAndFileOB (CITY_URI
+ ProvinceCode );
MHandler. sendEmptyMessage (2 );
} Catch (Exception e ){
E. printStackTrace ();
}
}
}). Start ();
}
// County
Private void loadCitys3 (final String CountryCode ){
If (null = CityCode)
Return;
New Thread (new Runnable (){
@ Override
Public void run (){
Try {
// Obtain county data
Json3 = HttpService. postParamAndFileOB (COUNTY_URL
+ CountryCode );
MHandler. sendEmptyMessage (3 );
System. out. println ("------ sheng ---- what's the problem -- 33 --");
} Catch (Exception e ){
E. printStackTrace ();
}
}
}). Start ();
}
Handler mHandler = new Handler (){
@ Override
Public void handleMessage (Message msg ){
Switch (msg. what ){
Case 1:
Try {
JSONArray array = new JSONArray (json );
Province = new String [array. length ()];
ProvinceCode = new String [array. length ()];
For (int I = 0; I <array. length (); I ++ ){
JSONObject obj = (JSONObject) array. get (I );
Province [I] = obj. getString ("name ");
ProvinceCode [I] = obj. getString ("code ");
}
ProvinceSpinner = (Spinner) findViewById (R. id. spinner_province );
// Initialize the data adapter in the drop-down list
ArrayAdapter Adapter = new ArrayAdapter (
MainActivity. this,
Android. R. layout. simple_spinner_item, Province );
// Set the style of the drop-down list, and set it as a simple style
Adapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
// Load data into the drop-down list
ProvinceSpinner. setAdapter (adapter );
// Add event Spinner event listening. This event is triggered when you click an option in the drop-down list.
ProvinceSpinner
. SetOnItemSelectedListener (new SpinnerSelectedListener ());
// Set the default value
ProvinceSpinner. setVisibility (View. VISIBLE );
} Catch (Exception e ){
E. printStackTrace ();
}
Break;
// City
Case 2:
Try {
JSONArray array = new JSONArray (json2 );
City = new String [array. length ()];
CityCode = new String [array. length ()];
For (int I = 0; I <array. length (); I ++ ){
JSONObject obj = (JSONObject) array. get (I );
City [I] = obj. getString ("name ");
CityCode [I] = obj. getString ("code ");
}
CitySpinner = (Spinner) findViewById (R. id. spinner_city );
ArrayAdapter Adapter = new ArrayAdapter (
MainActivity. this,
Android. R. layout. simple_spinner_item, City );
Adapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
CitySpinner. setAdapter (adapter );
CitySpinner. setOnItemSelectedListener (new SpinLisSHi ());
CitySpinner. setVisibility (View. VISIBLE );
} Catch (JSONException e ){
E. printStackTrace ();
}
Break;
// County
Case 3:
Try {
JSONArray array = new JSONArray (json3 );
Country = new String [array. length ()];
CountryCode = new String [array. length ()];
For (int I = 0; I <array. length (); I ++ ){
JSONObject obj = (JSONObject) array. get (I );
Country [I] = obj. getString ("name ");
CountryCode [I] = obj. getString ("code ");
}
CountySpinner = (Spinner) findViewById (R. id. spinner_county );
ArrayAdapter Adapter = new ArrayAdapter (
MainActivity. this,
Android. R. layout. simple_spinner_item, Country );
Adapter. setDropDownViewResource (android. R. layout. simple_spinner_dropdown_item );
CountySpinner. setAdapter (adapter );
CountySpinner. setOnItemSelectedListener (new SpinLisXian ());
CountySpinner. setVisibility (View. VISIBLE );
} Catch (JSONException e ){
E. printStackTrace ();
}
Break;
Default:
Break;
}
Super. handleMessage (msg );
}
};
Class SpinnerSelectedListener implements OnItemSelectedListener {
Public void onItemSelected (AdapterView Arg0, View arg1, int arg2,
Long arg3 ){
Toast. makeText (MainActivity. this, Province [arg2], 3000). show ();
// Download the name of the city
LoadCitys2 (ProvinceCode [arg2]);
}
Public void onNothingSelected (AdapterView Arg0 ){
}
}
// City
Class SpinLisSHi implements OnItemSelectedListener {
Public void onItemSelected (AdapterView Arg0, View arg1, int arg2,
Long arg3 ){
Toast. makeText (MainActivity. this, City [arg2], 3000). show ();
// Download the county name
LoadCitys3 (CityCode [arg2]);
}
Public void onNothingSelected (AdapterView Arg0 ){
}
}
// County
Class SpinLisXian implements OnItemSelectedListener {
Public void onItemSelected (AdapterView Arg0, View arg1, int arg2,
Long arg3 ){
Toast. makeText (MainActivity. this,
Country [arg2] + CountryCode [arg2], 3000). show ();
}
Public void onNothingSelected (AdapterView Arg0 ){
}
}
}
2. httpService. java
Package com. loveplusplus. loader. demo. util;
Import java. util. ArrayList;
Import java. util. List;
Import org. apache. http. HttpEntity;
Import org. apache. http. HttpResponse;
Import org. apache. http. HttpStatus;
Import org. apache. http. NameValuePair;
Import org. apache. http. client. HttpClient;
Import org. apache. http. client. entity. UrlEncodedFormEntity;
Import org. apache. http. client. methods. HttpGet;
Import org. apache. http. client. methods. HttpPost;
Import org. apache. http. impl. client. DefaultHttpClient;
Import org. apache. http. util. EntityUtils;
/**
* The url is resolved at 16:26:40 on January 1, July 20, 2014;
*
* @ Author YJBo
*/
Public class HttpService {
Public static void main (String [] args ){
}
Public static String postParamAndFileOB (String url ){
// Http address
String httpUrl = url;
System. out. println ("the OB address httpUrl ++ =" + httpUrl );
System. out
. Println ("-------------- the database is being linked ····----------------------------");
Try {
// HttpGet connection object
HttpPost httpRequest = new HttpPost (httpUrl );
System. out. println ("= wo, 20 = ");
System. out. println ("= wo here 21 = ");
// Use NameValuePair to save the Post parameter to be passed
List Params = new ArrayList ();
// Set the character set
HttpEntity httpentity = new UrlEncodedFormEntity (params, "UTF-8 ");
// Request httpRequest
HttpRequest. setEntity (httpentity );
// Get the HttpClient object
HttpClient httpclient = new DefaultHttpClient ();
// Request HttpClient to obtain HttpResponse
HttpResponse httpResponse = httpclient.exe cute (httpRequest );
System. out. println ("= wo here 23 = ");
// The request is successful.
If (httpResponse. getStatusLine (). getStatusCode () = HttpStatus. SC _ OK ){
// Obtain the returned string
System. out. println ("= wo here 24 = ");
String strResultOB = EntityUtils. toString (httpResponse
. GetEntity ());
//. GetEntity ();
System. out. println ("= wo here 22 =" + strResultOB );
Return strResultOB;
} Else {
System. out. println ("data request failed = ");
}
} Catch (Exception e ){
// TODO Auto-generated catch block
E. printStackTrace ();
}
Return null;
}
}
3. activity_main.xml
Xmlns: tools = "http://schemas.android.com/tools"
Android: layout_width = "match_parent"
Android: layout_height = "match_parent"
Android: orientation = "vertical"
Tools: context = ". ui. MainActivity">
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "10dp"
Android: orientation = "vertical">
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_margin = "5dp"
Android: text = "@ string/select_province"
Android: textSize = "20sp"/>
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/separator_line"
Android: contentDescription = "@ string/app_name"/>
Android: id = "@ + id/spinner_province"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "5dp"/>
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "10dp"
Android: orientation = "vertical">
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_margin = "5dp"
Android: text = "@ string/select_city"
Android: textSize = "20sp"/>
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/separator_line"
Android: contentDescription = "@ string/app_name"/>
Android: id = "@ + id/spinner_city"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "5dp"/>
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "10dp"
Android: orientation = "vertical">
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center"
Android: layout_margin = "5dp"
Android: text = "@ string/select_county"
Android: textSize = "20sp"/>
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: background = "@ drawable/separator_line"
Android: contentDescription = "@ string/app_name"/>
Android: id = "@ + id/spinner_county"
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_margin = "5dp"/>