Get请求-Test版

package com.fanqi.test;

import java.io.DataInputStream;
import java.io.IOException;
import java.io.InputStream;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;

public class TestGet {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        HttpResponse hs = null;
        String url = "http://gc.ditu.aliyun.com/geocoding?a=%E8%8B%8F%E5%B7%9E%E5%B8%82";
        HttpGet hg = new HttpGet(url);
        HttpClient hc = new DefaultHttpClient();
        try {
            hs = hc.execute(hg);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        HttpEntity he = hs.getEntity();
        try {
            InputStream in = he.getContent();            
            DataInputStream d = new DataInputStream(in);
            String str = d.readLine();
            String str1 = str.substring(1, str.length()-1);
            String[] s = str1.split(",");
            for(int i=0;i<s.length;i++){
                String t = s[i];
                String[] tt = t.split(":");
                if(tt.length==2){
                    String key = tt[0].replace("\"", "");
                    String value = tt[1].replace("\"", "");
                    System.out.println(key + ":" + value);
                }else{
                    String key = tt[0].replace("\"", "");
                    System.out.println(key + ":" + "null");
                }
            }
            
        } catch (IllegalStateException | IOException e) {
            e.printStackTrace();
        }
    }

}

 

posted on 2016-12-15 14:59  我要的明天  阅读(453)  评论(0编辑  收藏  举报

导航