首页 > 技术文章 > Get请求-Test版

fqfanqi 2016-12-15 14:59 原文

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();
        }
    }

}

 

推荐阅读