-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
tony
committed
May 14, 2016
1 parent
927a022
commit a545946
Showing
8 changed files
with
370 additions
and
9 deletions.
There are no files selected for viewing
199 changes: 199 additions & 0 deletions
199
app/src/androidTest/java/com/app/myweather/activity/ChooseAreaActivity.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
package com.app.myweather.activity; | ||
|
||
import android.app.Activity; | ||
import android.app.ProgressDialog; | ||
import android.os.Bundle; | ||
import android.text.TextUtils; | ||
import android.view.View; | ||
import android.view.Window; | ||
import android.widget.AdapterView; | ||
import android.widget.ArrayAdapter; | ||
import android.widget.ListView; | ||
import android.widget.TextView; | ||
import android.widget.Toast; | ||
|
||
import com.app.myweather.R; | ||
import com.app.myweather.model.City; | ||
import com.app.myweather.model.County; | ||
import com.app.myweather.model.MyWeatherDB; | ||
import com.app.myweather.model.Province; | ||
import com.app.myweather.util.HttpCallbackListener; | ||
import com.app.myweather.util.HttpUtil; | ||
import com.app.myweather.util.Utility; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* Created by Administrator on 2016/5/14. | ||
*/ | ||
public class ChooseAreaActivity extends Activity { | ||
public static final int LEVEL_PROVINCE = 0; | ||
public static final int LEVEL_CITY = 1; | ||
public static final int LEVEL_COUNTY = 2; | ||
private ProgressDialog progressDialog; | ||
private TextView titleText; | ||
private ListView listView; | ||
private ArrayAdapter<String> adapter; | ||
private MyWeatherDB myWeatherDB; | ||
private List<String> dataList = new ArrayList<String>(); | ||
private List<Province> provinceList; | ||
private List<City> cityList; | ||
private List<County> countyList; | ||
private Province selectedProvince; | ||
private City selectedCity; | ||
private int currentLevel; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
requestWindowFeature(Window.FEATURE_NO_TITLE); | ||
setContentView(R.layout.choose_area); | ||
listView = (ListView) findViewById(R.id.list_view); | ||
titleText = (TextView) findViewById(R.id.title_text); | ||
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, dataList); | ||
listView.setAdapter(adapter); | ||
myWeatherDB = MyWeatherDB.getInstance(this); | ||
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { | ||
@Override | ||
public void onItemClick(AdapterView<?> adapterView, View view, int index, long arg3) { | ||
|
||
if (currentLevel == LEVEL_PROVINCE) { | ||
selectedProvince = provinceList.get(index); | ||
queryCities(); | ||
} else if (currentLevel == LEVEL_CITY) { | ||
selectedCity = cityList.get(index); | ||
queryCounties(); | ||
} | ||
} | ||
}); | ||
queryProvinces(); | ||
} | ||
|
||
private void queryProvinces() { | ||
provinceList = myWeatherDB.loadProvinces(); | ||
if (provinceList.size() > 0) { | ||
dataList.clear(); | ||
for (Province province : provinceList) { | ||
dataList.add(province.getProvinceName()); | ||
} | ||
adapter.notifyDataSetChanged(); | ||
listView.setSelection(0); | ||
titleText.setText("Chinese"); | ||
currentLevel = LEVEL_PROVINCE; | ||
} else { | ||
queryFromServer(null, "provicne"); | ||
} | ||
} | ||
|
||
private void queryCities() { | ||
cityList = myWeatherDB.loadCities(selectedProvince.getId()); | ||
if (cityList.size() > 0) { | ||
dataList.clear(); | ||
for (City city : cityList) { | ||
dataList.add(city.getCityName()); | ||
} | ||
adapter.notifyDataSetChanged(); | ||
listView.setSelection(0); | ||
titleText.setText(selectedProvince.getProvinceName()); | ||
currentLevel = LEVEL_CITY; | ||
} else { | ||
queryFromServer(selectedProvince.getProvinceCode(), "city"); | ||
} | ||
} | ||
|
||
private void queryCounties() { | ||
countyList = myWeatherDB.loadCounties(selectedCity.getId()); | ||
if (countyList.size() > 0) { | ||
dataList.clear(); | ||
for (County county : countyList) { | ||
dataList.add(county.getCountyName());} | ||
adapter.notifyDataSetChanged(); | ||
listView.setSelection(0); | ||
titleText.setText(selectedCity.getCityName()); | ||
currentLevel = LEVEL_COUNTY; | ||
}else{ | ||
queryFromServer(selectedCity.getCityCode(), "county"); | ||
} | ||
} | ||
|
||
private void queryFromServer(final String code, final String type) { | ||
String address; | ||
if (!TextUtils.isEmpty(code)) { | ||
address = "http.weather.com.cn/data/list3/city" + code + ".xml"; | ||
} else { | ||
address = "http://www.weather.com.cn/data/list3/city.xml"; | ||
|
||
} | ||
showProgressDialog(); | ||
HttpUtil.sendHttpRequest(address, new HttpCallbackListener() { | ||
@Override | ||
public void onFinish(String response) { | ||
boolean result = false; | ||
if ("province".equals(type)) { | ||
result = Utility.handleProvincesResponse(myWeatherDB, response); | ||
} else if ("city".equals(type)) { | ||
result = Utility.handleCitiesResponse(myWeatherDB, response, selectedProvince.getId()); | ||
} else if ("county".equals(type)) { | ||
result = Utility.handleCountiesResponse(myWeatherDB, response, selectedCity.getId()); | ||
} | ||
if (result) { | ||
runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
closeProgressDialog(); | ||
if ("province".equals(type)) { | ||
queryProvinces(); | ||
} else if ("city".equals(type)) { | ||
queryCities(); | ||
} else if ("county".equals((type))) { | ||
queryCounties(); | ||
} | ||
} | ||
}); | ||
} | ||
} | ||
|
||
@Override | ||
public void onError(Exception e) { | ||
runOnUiThread(new Runnable() { | ||
@Override | ||
public void run() { | ||
closeProgressDialog(); | ||
Toast.makeText(ChooseAreaActivity.this, "加载失败", Toast.LENGTH_SHORT).show(); | ||
} | ||
}); | ||
|
||
} | ||
}); | ||
} | ||
|
||
private void showProgressDialog() { | ||
if (progressDialog == null) { | ||
progressDialog = new ProgressDialog(this); | ||
progressDialog.setMessage("正在加载..."); | ||
progressDialog.setCanceledOnTouchOutside(false); | ||
} | ||
progressDialog.show(); | ||
} | ||
|
||
private void closeProgressDialog() { | ||
if (progressDialog != null) { | ||
progressDialog.dismiss(); | ||
} | ||
} | ||
|
||
@Override | ||
public void onBackPressed() { | ||
if (currentLevel == LEVEL_COUNTY) { | ||
queryCities(); | ||
} else if (currentLevel == LEVEL_CITY) { | ||
queryProvinces(); | ||
} else { | ||
finish(); | ||
} | ||
} | ||
|
||
} | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 changes: 9 additions & 0 deletions
9
app/src/androidTest/java/com/app/myweather/util/HttpCallbackListener.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.app.myweather.util; | ||
|
||
/** | ||
* Created by Administrator on 2016/5/14. | ||
*/ | ||
public interface HttpCallbackListener { | ||
void onFinish(String response); | ||
void onError(Exception e); | ||
} |
47 changes: 47 additions & 0 deletions
47
app/src/androidTest/java/com/app/myweather/util/HttpUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.app.myweather.util; | ||
|
||
import java.io.BufferedReader; | ||
import java.io.InputStream; | ||
import java.io.InputStreamReader; | ||
import java.net.HttpURLConnection; | ||
import java.net.URI; | ||
import java.net.URL; | ||
|
||
/** | ||
* Created by Administrator on 2016/5/14. | ||
*/ | ||
public class HttpUtil { | ||
public static void sendHttpRequest(final String address,final HttpCallbackListener listener){ | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
HttpURLConnection connection=null; | ||
try{ | ||
URL url=new URL(address); | ||
connection=(HttpURLConnection) url.openConnection(); | ||
connection.setRequestMethod("GET"); | ||
connection.setConnectTimeout(8000); | ||
connection.setReadTimeout(8000); | ||
InputStream in=connection.getInputStream(); | ||
BufferedReader reader=new BufferedReader(new InputStreamReader(in)); | ||
StringBuilder response=new StringBuilder(); | ||
String line; | ||
while ((line=reader.readLine()) !=null){ | ||
response.append(line); | ||
} | ||
if (listener !=null){ | ||
listener.onFinish(response.toString()); | ||
} | ||
}catch(Exception e){ | ||
if (listener !=null){ | ||
listener.onError(e); | ||
} | ||
}finally { | ||
if (connection !=null){ | ||
connection.disconnect(); | ||
} | ||
} | ||
} | ||
}).start(); | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
app/src/androidTest/java/com/app/myweather/util/Utility.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
package com.app.myweather.util; | ||
|
||
import android.text.TextUtils; | ||
|
||
import com.app.myweather.model.City; | ||
import com.app.myweather.model.County; | ||
import com.app.myweather.model.MyWeatherDB; | ||
import com.app.myweather.model.Province; | ||
|
||
/** | ||
* Created by Administrator on 2016/5/14. | ||
*/ | ||
public class Utility { | ||
public synchronized static boolean handleProvincesResponse(MyWeatherDB myWeatherDB,String response){ | ||
if (!TextUtils.isEmpty(response)){ | ||
String[] allProvinces =response.split(","); | ||
if (allProvinces !=null && allProvinces.length>0){ | ||
for (String p:allProvinces){ | ||
String[] array=p.split("\\|"); | ||
Province province=new Province(); | ||
province.setProvinceCode(array[0]); | ||
province.setProvinceName(array[1]); | ||
myWeatherDB.saveProvince(province); | ||
} | ||
return true; | ||
|
||
} | ||
} | ||
return false; | ||
} | ||
public static boolean handleCitiesResponse(MyWeatherDB myWeatherDB,String response,int provinceId){ | ||
if (!TextUtils.isEmpty(response)){ | ||
String[] allCities=response.split(","); | ||
if (allCities !=null && allCities.length>0){ | ||
for (String c:allCities){ | ||
String[] array=c.split("\\|"); | ||
City city=new City(); | ||
city.setCityCode(array[0]); | ||
city.setCityName(array[1]); | ||
city.setProvinceId(provinceId); | ||
myWeatherDB.saveCity(city); | ||
} | ||
return true; | ||
} | ||
} | ||
return false; | ||
} | ||
public static boolean handleCountiesResponse(MyWeatherDB myWeatherDB,String response,int cityId){ | ||
if (!TextUtils.isEmpty(response)){ | ||
String[] allCounties =response.split(","); | ||
if (allCounties !=null && allCounties.length >0){ | ||
for (String c:allCounties){ | ||
String[] array=c.split("\\|"); | ||
County county=new County(); | ||
county.setCountyCode(array[0]); | ||
county.setCountyName(array[1]); | ||
county.setCityId(cityId); | ||
myWeatherDB.saveCounty(county); | ||
}return true; | ||
} | ||
}return false; | ||
} | ||
} |
Oops, something went wrong.