---
title: "java 读取http url joson 格式"
author: "nolan022"
date: "2012-02-28T08:39:33Z"
tags: []
categories: ["编程语言"]
slug: "java-http-url-joson"
draft: false
lastmod: "2012-02-28T08:39:33Z"
id: 1428159
url: "https://nolan022.iteye.com/blog/1428159"
---

```java


URL url = new URL("http://ditu.google.com/maps/api/geocode/json?latlng=31.1899209667,121.3918055000&sensor=false&&language=zh-CN");
	        URLConnection conn = url.openConnection();
	        HttpURLConnection http = (HttpURLConnection)conn;
	        http.setRequestMethod("GET");
	        http.setDoInput(true);
	        http.setDoOutput(true);
	        http.setRequestProperty("Accept-Charset", "UTF-8");
	        http.connect();
//	        System.out.println(http.getContentType());
	        InputStream in = http.getInputStream();
	        BufferedReader br = new BufferedReader(new InputStreamReader(in,"UTF-8"));
	        String s = null;
	        String json = "";
	        while((s = br.readLine()) != null) {
	        	json+=s;
	        }
	        org.json.JSONObject jo = new org.json.JSONObject(json);
	        org.json.JSONArray ja = jo.getJSONArray("results");
	        org.json.JSONObject jo1 = ja.getJSONObject(0);
	        System.out.println(jo1.getString("formatted_address"));
	        System.out.println(json);

```