Google 在 Android 中对 HTTP 请求进行地理编码

Google Geocode HTTP request in Android

我正在制作地图应用程序(Android),我需要在信息中制作带有街道地址的点击标记。经过几次错误的尝试后,我发现 Geocoder /get from location 不再起作用(returns 一个带有空指针的空数组)。 我在这里发现 LINK 我可以向 google 发出 HTTP/S 请求,然后我会返回 JSON/XML。 我的问题是:当 HTTP 客户端被取消时如何发出 HTTP 请求(示例)?你能给我一个代码示例来发送请求并在 JSON 中计算出响应(或者至少只发送)吗?

编辑:我得到 this:android.os.NetworkOnMainThreadException

 try {

        URL url = new URL(baseUri+builder.toString());
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setReadTimeout(10000);
        conn.setConnectTimeout(15000);
        conn.setRequestMethod("GET");
        conn.setDoInput(true);
        conn.connect(); \exception originated here
        is = conn.getInputStream();
        reader = new BufferedReader(new InputStreamReader(is));
        for (String line = null; (line = reader.readLine()) != null;) {
            builder.append(line).append("\n");
        }
        jsonresponse = new JSONArray(builder.toString());



    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (ProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (JSONException e) {
        e.printStackTrace();
    }

弃用的只是 Apache HTTP 客户端。但是您始终可以对 HTTP 请求使用 HttpURLConnection
有一个带有代码示例的培训 material。 http://developer.android.com/training/basics/network-ops/connecting.html