《第一行代码》11.3.2 使用 HttpURLConnection 代替 HttpClient

  • 由于 HttpClient 已经不被采用,我在学习 11.3.2 节的时候,就尝试用 HttpURLConnection 来代替。

  • 以下是我用 HttpURLConnection 重写了的代码:

package com.hultron.locationtest;

public class MainActivity extends AppCompatActivity {

    public static final int SHOW_LOCATION = 0;
    ......

    private void showLocation(final Location location) {
       new Thread(new Runnable() {
           @Override
           public void run() {
               try {
                   //组装反向地理编码的接口地址
                   //使用 HttpURLConnection 替换 HttpClient
                   URL url = new URL("http://maps.googleapis.com/maps/api/geocode/json?latlng="
                   + location.getLatitude() + "," + location.getLongitude() + "&sensor=false");
                   HttpURLConnection connection = (HttpURLConnection) url.openConnection();
                   connection.setRequestMethod("GET");
                   connection.setConnectTimeout(8000);
                   connection.setReadTimeout(8000);
                   //在请求中指定语言,保证服务器会返回中文数据
                   connection.setRequestProperty("Accept-Language","zh-CN");
                   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);
                   }

                   JSONObject jsonObject = new JSONObject(response.toString());
                   //Log.d("MainActivity",response.toString());
                   //获取 results 节点下的位置信息
                   JSONArray resultArray = jsonObject.getJSONArray("results");
                   if (resultArray.length() > 0) {
                       JSONObject subObject = resultArray.getJSONObject(0);
                       //取出格式化后的位置信息
                       String address = subObject.getString("formatted_address");

                   Message message = new Message();
                   message.what = SHOW_LOCATION;
                   //将服务器返回的结果存放到 message 中
                   message.obj = address;
                   handler.sendMessage(message);
                   }
               } catch (Exception e) {
                   e.printStackTrace();
               }
           }
       }).start();
    }

    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case SHOW_LOCATION:
                    String currentPosition = (String) msg.obj;
                    positionTextView.setText(currentPosition);
                    break;
                default:
                    break;
            }
        }
    };
}

ps.我昨天反复运行了三次这段代码,都成功得到了当前的位置信息,然后刚才为了测试,又运行了一遍,却失败了。反复检查后,发现没毛病。。。我想,可能是墙的问题。。。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值