android:省市縣三級聯動(基於json和spring)

來源:互聯網
上載者:User

android:省市縣三級聯動(基於json和spring)

一、請看“:

二、程式的代碼:

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;


/**
* 省市縣三級聯動
*
* 2014年10月30日 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;
// 省市縣數組
private String[] Province;
private String[] ProvinceCode;
private String json;
// 市---
private String json2;
private String[] City;
private String[] CityCode;
// 縣
private String json3;
private String[] Country;
private String[] CountryCode;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 擷取省的名稱
loadProvices();
}


private void loadProvices() {
new Thread(new Runnable() {
@Override
public void run() {
try {
// 初步進入頁面,擷取所有省份
json = HttpService.postParamAndFileOB(PROVINCE_URL);
mHandler.sendEmptyMessage(1);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}


// 市---新
private void loadCitys2(final String provinceCode) {
if (null == provinceCode)
return;
new Thread(new Runnable() {
@Override
public void run() {
try {
// 擷取一個省的所有市
json2 = HttpService.postParamAndFileOB(CITY_URI
+ provinceCode);
mHandler.sendEmptyMessage(2);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}


// 縣
private void loadCitys3(final String CountryCode) {
if (null == CityCode)
return;
new Thread(new Runnable() {
@Override
public void run() {
try {
// 擷取縣份資料
json3 = HttpService.postParamAndFileOB(COUNTY_URL
+ CountryCode);
mHandler.sendEmptyMessage(3);
System.out.println("------sheng----麻煩呢--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);
// 初始化下拉式清單載入資料配接器
ArrayAdapter adapter = new ArrayAdapter(
MainActivity.this,
android.R.layout.simple_spinner_item, Province);
// 設定下拉式清單的樣式,下為設定為簡單樣式
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// 將資料載入進下拉式清單當中
provinceSpinner.setAdapter(adapter);
// 添加事件Spinner事件監聽,當點擊下拉式清單中的某一選項之後觸發該事件
provinceSpinner
.setOnItemSelectedListener(new SpinnerSelectedListener());
// 設定預設值
provinceSpinner.setVisibility(View.VISIBLE);
} catch (Exception e) {
e.printStackTrace();
}
break;
// 市
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;
// 縣
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();
// 下載市的名稱
loadCitys2(ProvinceCode[arg2]);
}


public void onNothingSelected(AdapterView arg0) {
}
}


// 市
class SpinLisSHi implements OnItemSelectedListener {


public void onItemSelected(AdapterView arg0, View arg1, int arg2,
long arg3) {
Toast.makeText(MainActivity.this, City[arg2], 3000).show();
// 下載縣的名稱
loadCitys3(CityCode[arg2]);
}


public void onNothingSelected(AdapterView arg0) {
}
}


// 縣
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;


/**
* 2014年7月20日 16:26:40 這裡是解析url的;
*
* @author YJBo
*/
public class HttpService {


public static void main(String[] args) {
}



public static String postParamAndFileOB(String url) {
// http地址
String httpUrl = url;
System.out.println("此時的OB地址httpUrl++==" + httpUrl);
System.out
.println("--------------資料庫正在連結中····----------------------------");
try {
// HttpGet連線物件
HttpPost httpRequest = new HttpPost(httpUrl);
System.out.println("==wo走到這裡了 20===");


System.out.println("==wo走到這裡了 21===");
// 使用NameValuePair來儲存要傳遞的Post參數
List params = new ArrayList();
// 設定字元集
HttpEntity httpentity = new UrlEncodedFormEntity(params, "UTF-8");
// 請求httpRequest
httpRequest.setEntity(httpentity);
// 取得HttpClient對象
HttpClient httpclient = new DefaultHttpClient();
// 請求HttpClient,取得HttpResponse
HttpResponse httpResponse = httpclient.execute(httpRequest);
System.out.println("==wo走到這裡了 23===");
// 請求成功
if (httpResponse.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
// 取得返回的字串
System.out.println("==wo走到這裡了 24===");
String strResultOB = EntityUtils.toString(httpResponse
.getEntity());
// .getEntity();
System.out.println("==wo走到這裡了 22===" + strResultOB);
return strResultOB;
} else {
System.out.println("資料請求失敗====");
}


} 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" />




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.