上一片我们大概了解了一下Geocoder的bug和它的替代方法,Geocoding,直接利用http地址进行解析;下面我们来用一个实例看看如何使用http地址反回来的数据进行解析,我们这里需要用到Google自己开发的一个框架来解析json;json1.7.jar,这个类库的学习,请看:http://code.google.com/p/google-gson/
下面看实例%
1、我们如果要访问网络,首先需要在mainandroidfest.xml文件里添加权限:<uses-permission android:name="android.permission.INTERNET" />
2、修改main.xml文件,添加按钮:点击事件,进行解析;
3、给按钮添加监听器;
4、在这里我们用的是google json进行解析,所以需要两个模型,分别代表json解析出来的两个数据。一个代表一对象,list存储数组;
两具模型为:
- public class Result {
- private String[] types;
- private String formatted_address;
- public String getFormatted_address() {
- return formatted_address;
- }
- public void setFormatted_address(String formattedAddress) {
- formatted_address = formattedAddress;
- }
- public String[] getTypes() {
- return types;
- }
- public void setTypes(String[] types) {
- this.types = types;
- }
- @Override
- public String toString() {
- return "Result [formatted_address=" + formatted_address + ", types="
- + Arrays.toString(types) + "]";
- }
- }
- public class TestResult {
- private String status;
- private List<Result> results;
- public String getStatus() {
- return status;
- }
- public void setStatus(String status) {
- this.status = status;
- }
- public List<Result> getResults() {
- return results;
- }
- public void setResults(List<Result> results) {
- this.results = results;
- }
- @Override
- public String toString() {
- return "TestResult [results=" + results + ", status=" + status + "]";
- }
- }
Activity代码为:
- public class HelloGPS2Activity extends Activity {
- private Button geoButton = null;
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- geoButton = (Button)findViewById(R.id.geoButton);
- geoButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String url = "http://maps.googleapis.com/maps/api/geocode/json?latlng=40.714224,-73.961452&sensor=false";
- //创建一个HttpClient对象
- HttpClient httpClient = new DefaultHttpClient();
- String responseData = "";
- try{
- //向指定的URL发送Http请求
- HttpResponse response = httpClient.execute(new HttpGet(url));
- //取得服务器返回的响应
- HttpEntity entity = response.getEntity();
- BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(entity.getContent()));
- String line = "";
- while((line = bufferedReader.readLine()) != null){
- responseData = responseData + line;
- }
- }
- catch (Exception e) {
- e.printStackTrace();
- }
- Gson gson = new Gson();
- TestResult testResult = gson.fromJson(responseData, TestResult.class);
- System.out.println(testResult);
- }
- });
- }
- }
当运行之后 :点击按钮:就会将接受到的地址JSON数据解析出来:
- 08-15 03:33:23.703: INFO/System.out(281): TestResult [results=[Result [formatted_address=279-281 Bedford Ave, Brooklyn, NY 11211, USA, types=[street_address]], Result [formatted_address=Williamsburg, NY, USA, types=[neighborhood, political]], Result [formatted_address=Brooklyn, NY 11211, USA, types=[postal_code]], Result [formatted_address=Brooklyn, NY, USA, types=[sublocality, political]], Result [formatted_address=Kings, New York, USA, types=[administrative_area_level_2, political]], Result [formatted_address=New York, NY, USA, types=[locality, political]], Result [formatted_address=New York, USA, types=[administrative_area_level_1, political]]], status=OK]
我们可以进一步提取里面的信息