您的位置:首页 > 其它

Get请求-Test版

2016-12-15 14:59 323 查看
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();
}
}

}

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: