打印城市坐标



http://gc.ditu.aliyun.com/geocoding?a=邢台

这个地址是一个返回城市的坐标的地址。返回的是·Json.

示例返回数据:

{
    "lon": 114.50484,
    "level": 2,
    "address": "",
    "cityName": "",
    "alevel": 4,
    "lat": 37.07058
}

这也是最基础的Json数据格式。


首先写一个从网上调去和数据类

MyTask 这个类继承 AsyncTask 这都懂得 这个类的主要作用是从网上取出这段数据。
传递的参数第一个是传进的参数类型String(网址) 第二个参数一般作为进度参数,这里不用所以传Void ,第三个是传出的参数类型String



需要注意的是,第一个传进去的Url,如果将这个链接直接用电脑打开的话,是没有问题的,但是如果用手机的的浏览器打开的话如果链接中含有中文那么,就会出现错误。所以在通过手机访问的时候需要对链接中的中文字符进行编码
利用java中提供的UrlEncode类进操作这个方法还是比较简单的:
String url = "邢台";
String newUrl = null;
newUrl = java.net.Encode.encode(url,"UTF-8");
System.out.println(newUrl);
这样就可以将中文字符进行转换为网地址识别字符




MyTask.java

package com.example.hejingzhou.studiojson;

<span style="font-size:18px;">import android.os.AsyncTask;
import android.util.Log;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Hejingzhou on 2016/2/5.
 */
public class MyTask extends AsyncTask<String,Void, String> {

    private String TAG = "MyTask";
    InputStream inputStream;
    InputStreamReader inputStreamReader;
    BufferedReader bufferedReader;
    String ReadLine;
    String Final;


    public static String lon;
    public static String level;
    public static String lat;
    public static String alevel;

    public static String getLon() {
        return lon;
    }

    public static String getLevel() {
        return level;
    }

    public static String getLat() {
        return lat;
    }

    public static String getAlevel() {
        return alevel;
    }

    public static String Result;
    public static String Data;

    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            Log.i(TAG, "" + params[0]);
            HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
            httpURLConnection.setRequestMethod("GET");
            httpURLConnection.setDoInput(true);
            httpURLConnection.setDoOutput(true);
            httpURLConnection.setDefaultUseCaches(false);
            Log.i(TAG, "" + httpURLConnection.getResponseCode());
            if (httpURLConnection.getResponseCode() == 200) {
                inputStream = httpURLConnection.getInputStream();
                inputStreamReader = new InputStreamReader(inputStream);
                bufferedReader = new BufferedReader(inputStreamReader);
                while ((ReadLine = bufferedReader.readLine()) != null) {
                    Result += ReadLine;
                    Log.i(TAG, Result);
                }
                if (httpURLConnection != null) httpURLConnection.disconnect();
                if (inputStream != null) inputStream.close();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return Result;
    }

    @Override
    protected void onPostExecute(String result) {
        Data = result;
        Log.i(TAG, Data);
        Final = Data.replace("null", "");

        if (Final != null) {
            try {
                JSONObject jsonObject = new JSONObject(Final);
                lat = jsonObject.getString("lat");
                lon = jsonObject.getString("lon");
                level = jsonObject.getString("level");
                alevel = jsonObject.getString("alevel");

                Log.i(TAG, "     纬度   " + lat);
                Log.i(TAG, "     径度   " + lon);
                Log.i(TAG, "     水平   " + level);
                Log.i(TAG, "A式水准仪   " + alevel);

            } catch (JSONException e) {
                Log.i(TAG, "Json Decode Errors");
            }
        } else Log.i(TAG, "Data数据为空");
    }
}
</span>



MainActivity.java

package com.example.hejingzhou.studiojson;

<span style="font-size:18px;">import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EdgeEffect;
import android.widget.EditText;
import android.widget.TextView;

import java.io.UnsupportedEncodingException;

public class MainActivity extends AppCompatActivity {

    private TextView textViewShow;
    private String url_L = "http://gc.ditu.aliyun.com/geocoding?a=";
    private String url_R;
    private EditText editTextAddr;
    private Button buttonOk;
    private String EncodeResult;
    private String TAG = "MainActivity";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        editTextAddr = (EditText) findViewById(R.id.editTextAddr);
        buttonOk = (Button) findViewById(R.id.buttonOk);
        textViewShow = (TextView) findViewById(R.id.textViewShow);

        buttonOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                url_R = editTextAddr.getText().toString();
                MyTask myTask = new MyTask();
                myTask.execute(url_L + UrlEncode(url_R));
                textViewShow.setText("本地区的位置:\n\n" + "         经度 :" + myTask.getLon() + "\n\n          纬度 : " + myTask.getLat() + "\n\n" + "      水平度 : " + myTask.getLevel() + "\n\nA式水平度 : " + myTask.getAlevel());
            }
        });
    }


</span>
<span style="font-size:18px;">//对输入的中文进行编码的方法
    public String UrlEncode(String url) {
        if (url != null) {
            try {
                EncodeResult = java.net.URLEncoder.encode(url_R, "UTF-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return EncodeResult;
    }
}
</span>




你可能感兴趣的:(json解析,urlencode)