[160824][Android]Android中JSON字符串的获取与解析

package com.xiaotustudio.test2016082401;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;

import org.json.JSONArray;
import org.json.JSONObject;

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

public class MainActivity extends AppCompatActivity
{
    String urlString = "http://api.map.baidu.com/geoconv/v1/?coords=114.21892734521,29.575429778924;114.21892734521,29.575429778924&from=1&to=5&ak=你的密钥";
    String json;
    String jsonParse;

    TextView textView;

    Handler handler = new Handler()
    {
        @Override
        public void handleMessage(Message msg)
        {
            if (msg.what == 0)
            {
                textView.setText(jsonParse);
            }
            super.handleMessage(msg);
        }
    };

    Thread thread = new Thread()
    {
        @Override
        public void run()
        {
            try
            {
                json = urlToJSON(urlString);
                jsonParse = parseJSON(json);

                Message message = new Message();
                message.what = 0;
                handler.sendMessage(message);
            } catch (Exception e)
            {

            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        thread.start();
    }

    public String streamToString(InputStream inputStream)
    {
        try
        {
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
            StringBuilder stringBuilder = new StringBuilder();
            String line;
            while ((line = reader.readLine()) != null)
            {
                stringBuilder.append(line);
            }
            return stringBuilder.toString();
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public String urlToJSON(String urlString)
    {
        try
        {
            URL url = new URL(urlString);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream inputStream = connection.getInputStream();
            return streamToString(inputStream);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }

    public String parseJSON(String json)
    {
        try
        {
            JSONObject jsonObject = new JSONObject(json);
            JSONArray jsonArray = jsonObject.getJSONArray("result");
            JSONObject jsonObject0 = (JSONObject) jsonArray.opt(0);
            JSONObject jsonObject1 = (JSONObject) jsonArray.opt(1);

            String result = "";
            result = jsonObject0.getDouble("x") + "," + jsonObject0.getDouble("y") + "\n"
                    + jsonObject1.getDouble("x") + "," + jsonObject1.getDouble("y");

            return result;
        } catch (Exception e)
        {
            e.printStackTrace();
        }
        return null;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值