package com.zml.pojo.test;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import model.Root;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.junit.Test;
import com.zml.pojo.News;
import com.zml.pojo.NewsJson;
import com.zml.pojo.WhetherJson;
import com.zml.utils.GsonTools;
/**
* @author 郑明亮
* @Time:2016年3月18日 上午10:35:39
* @version 1.0
*/
public class TestGsonAPI {
@Test
public void test() {
try {
URL url = new URL("http://api.avatardata.cn/TechNews/Query?key=5e3bedcfa2714e36a3e46dd2efce00d9&page=1&rows=10");
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// String data = connection.getContentType();
String dataString = new String(GsonTools.IsToByte(connection.getInputStream()),"utf-8");
NewsJson newsJson = GsonTools.getObjectData(dataString, NewsJson.class);
List<News>list = newsJson.getResult();
System.out.println(list.toString());
System.out.println(dataString);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testqueryNews(){
HttpPost post = new HttpPost("http://op.juhe.cn/onebox/news/query?key=8b9a6d58dfae1f5e314b47cb6cec2a36");
@SuppressWarnings({ "resource", "deprecation" })
HttpClient client = new DefaultHttpClient();
try {//&page=1&rows=10
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("q", "四川现最神秘鸟"));
// params.add(new BasicNameValuePair("rows","2"));
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
String jsonString = EntityUtils.toString(response.getEntity());
System.out.println(jsonString);
System.out.println(GsonTools.getObjectData(jsonString, NewsJson.class));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testWords(){
HttpPost post = new HttpPost("http://op.juhe.cn/onebox/news/words?key=8b9a6d58dfae1f5e314b47cb6cec2a36");
@SuppressWarnings({ "resource", "deprecation" })
HttpClient client = new DefaultHttpClient();
try {//&page=1&rows=10
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("q", "四川现最神秘鸟"));
// params.add(new BasicNameValuePair("rows","2"));
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
if (code == 200) {
String jsonString = EntityUtils.toString(response.getEntity());
System.out.println(jsonString);
System.out.println(GsonTools.getObjectData(jsonString, NewsJson.class));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testWhether(){
HttpPost post = new HttpPost("http://op.juhe.cn/onebox/weather/query");
@SuppressWarnings({ "resource", "deprecation" })
HttpClient client = new DefaultHttpClient();
try {//&page=1&rows=10
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("cityname", "石家庄"));
params.add(new BasicNameValuePair("key","0d3e4a9debf18bdbfa947a3f6a53d3ca"));
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
System.out.println(code);
if (code == 200) {
String jsonString = EntityUtils.toString(response.getEntity());
System.out.println(jsonString);
System.out.println(GsonTools.getObjectData(jsonString, WhetherJson.class));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testMedice(){
HttpPost post = new HttpPost("http://apis.baidu.com/qunar/qunar_train_service/stationsearch");
@SuppressWarnings({ "resource", "deprecation" })
HttpClient client = new DefaultHttpClient();
try {//&page=1&rows=10
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("version", "1.0"));
params.add(new BasicNameValuePair("station","北京"));
post.setEntity(new UrlEncodedFormEntity(params,HTTP.UTF_8));
post.setHeader("apikey", "0fc807e45a37ce264f45d169646f4a9e");
HttpResponse response = client.execute(post);
int code = response.getStatusLine().getStatusCode();
System.out.println(code);
if (code == 200) {
String jsonString = EntityUtils.toString(response.getEntity());
System.out.println(jsonString);
System.out.println(GsonTools.getObjectData(jsonString, WhetherJson.class));
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Test
public void testrenwu(){
String httpUrl = "http://apis.baidu.com/baidu_openkg/person_kg/person_kg";
String httpArg = "{query:刘德华的身高,resource:spo_person}";
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("POST");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
// 填入apikey到HTTP header
connection.setRequestProperty("apikey", "0fc807e45a37ce264f45d169646f4a9e");
connection.setDoOutput(true);
connection.getOutputStream().write(httpArg.getBytes("UTF-8"));
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sbf.toString());
}
@Test
public void testjson(){
String httpUrl = "http://122.200.77.206:8899/ishop/ePlatform/mobile/queryAllMerchants.do";
BufferedReader reader = null;
String result = null;
StringBuffer sbf = new StringBuffer();
try {
URL url = new URL(httpUrl);
HttpURLConnection connection = (HttpURLConnection) url
.openConnection();
connection.setRequestMethod("GET");
connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
connection.connect();
InputStream is = connection.getInputStream();
reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
String strRead = null;
while ((strRead = reader.readLine()) != null) {
sbf.append(strRead);
sbf.append("\r\n");
}
reader.close();
result = sbf.toString();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println(sbf.toString());
Root root = G