HttpGet 请求
import java.net.HttpURLConnection;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils; import com.google.gson.Gson;
import com.google.gson.JsonElement;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken; public class Test { /**
* @param args
*/
public static void main(String[] args) { HttpClient httpClient = new DefaultHttpClient();
String strResult = "";
try {
Date date = new Date();
Calendar cal = Calendar.getInstance();
cal.setTime(date);
cal.add(Calendar.DATE, -1);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String dateStr = sdf.format(cal.getTime());
String url = "http://192.168.0.1:1234/test/getData?uid=12&date="
+ dateStr;
/* 建立HttpGet对象 */
HttpGet httpget = new HttpGet(url);
httpget.addHeader("request-key", "test-007"); // 请求访问key /* 发送请求并等待响应 */
HttpResponse httpResponse = httpClient.execute(httpget);
if (httpResponse.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
/* 读返回数据 */
strResult = EntityUtils.toString(httpResponse.getEntity()); /* json字符串转 list<T> */
JsonParser jsonparer = new JsonParser();
JsonElement je = null;
je = jsonparer.parse(strResult);
Object ob = je.getAsJsonObject().get("data");
Gson gson = new Gson();
List<TestPojo> picInfoList = gson.fromJson(ob.toString(),
new TypeToken<List<TestPojo>>() {
}.getType()); for (TestPojo temp : picInfoList) {
System.out.println(temp.getTime());
} System.out.println("--strResult:" + strResult);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 关闭连接 ,释放资源
httpClient.getConnectionManager().shutdown();
}
}
}
HttpGet 请求的更多相关文章
- 路由传值及获取参数,路由跳转,路由检测,this.$route.query和this.$route.params接收参数,HttpGet请求拼接url参数
配置动态路由参数id: routes: [ // 动态路径参数 以冒号开头 { path: '/user/:id', component: User } ] html路由跳转: <router- ...
- HttpGet请求传递数组(集合)
在HttpGet请求是传递数组(集合)的方法: 1.使用Ajax方法传递 eg: ajax.({ url:/test, data:["], type:"get" }); ...
- HttpGet 请求(带参数)
package com.example.util; import java.io.BufferedReader;import java.io.IOException;import java.io.In ...
- httpget请求测试用Java代码的实现方法
原文:http://www.cnblogs.com/johnson-yuan/p/6637906.html 1.首先要在eclipse中导入HttpClient的jar包. 2.新建类并写入一下代码: ...
- WebApi 4.0 默认方式不支持HttpGet 请求
如果Controller方法中没有指定请求方式,在RC版本中默认是HttpPost ,Beta版本中支持所有方法GET, PUT, POST and Delete,而在RC版本后做了改变只支持Http ...
- C#实现的HttpGet请求
话不多说,代码贴上: /// <summary> /// HTTP Get请求 /// </summary> /// <param name="url" ...
- 向PHP发送HTTP-Get请求
1.get.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...
- 使用HttpGet请求json数据
- java最简单的方式实现httpget和httppost请求
java实现httpget和httppost请求的方式多种多样,个人总结了一种最简单的方式,仅仅需几行代码,就能够完美的实现. 此处须要用到两个jar包,httpclient-4.3.1.jar.ht ...
随机推荐
- effective c++ 条款7 declare virtual destructor for polymophyc base class
这似乎很明显. 如果base class的destructor不是virtual,当其derived class作为基类使用,析构的时候derived class的数据成员将不会被销毁. 举个例子 我 ...
- WPF 绑定
WPF里分三种Binding:Binding, PriorityBinding, MultiBinding,这三种Binding的基类都是BindingBase,而BindingBase又继承于Mar ...
- python遗产
1. python类方法的定义: class Animal(): def __init__(self,name): self.name=name; def show(self): print s ...
- 自定义DB连接池实现
实现一个简单的数据库连接池 1,连接池接口 package dbsource; import java.sql.Connection; import java.sql.SQLException; /* ...
- uboot的relocation原理具体分析
近期在一直在做uboot的移植工作,uboot中有非常多值得学习的东西.之前总结过uboot的启动流程,但uboot一个非常核心的功能没有细致研究.就是uboot的relocation功能. 这几天研 ...
- redis(Remote Dictionary Server)
redis的简介和使用 简介 redis(Remote Dictionary Server)是一种Nosql技术,它是一个开源的高级kv存储和数据结构存储系统,它经常被拿来和Memcached相比 ...
- Redis于windows在安装
下载的windows版本号是redis-2.0.2,解压到D盘下: D:\redis-2.0.2 进到该文件夹下,有下列文件: redis-server.exe:服务程序 redis-check-du ...
- Webots入门(二)-build up a controller
A simple controller 控制器程序读取传感器的值,然后改动行走速度来避开障碍物. 以下是控制器源码mybot_simple.c: #include<webots/robot.h& ...
- iOS如何添加照片模拟器(附带诉讼)
刚開始做图片选择时,使用了最笨的办法给iphone模拟器添加照片. 方法一:首先打开safari.然后找到图片.点击图片,保存到本地(iphone): 方法二:拖动本地计算机的随意一张照片到iphon ...
- Android应用公布的准备——生成渠道包
我们须要使用一个变量标明该app的渠道.通常我们能够在manifest中的application节点下声明.例如以下. <meta-data android:name="CHANNEL ...