webservice小例子

package test;

import java.io.InputStream;
import java.nio.charset.Charset;

import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.utils.HttpClientUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;

public class Test {

	public static void main(String[] args) throws Exception {
		HttpClient client=new DefaultHttpClient();
		HttpUriRequest request=new HttpGet("http://www.weather.com.cn/adat/sk/101181701.html");
		HttpResponse resp=client.execute(request);
		String s=EntityUtils.toString(resp.getEntity(), Charset.forName("UTF-8"));
		
		JSONObject jso=new JSONObject(s);
		System.out.println(jso);
		JSONObject wi=(JSONObject)jso.get("weatherinfo");
		System.out.println(wi.get("city"));
		
	}

}

你可能感兴趣的:(webservice小例子)