java中如何java调用淘宝api物流信息的api

java调用淘宝api联网查询ip归属地
转载 &发布时间:日 09:17:19 & 作者:
java联网查询IP归属地,原理是根据淘宝提供的service查询IP的归属地并且解析http请求返回的json串
淘宝返回的数据为:{"code":0,"data":{"country":"\u4e2d\u56fd","country_id":"CN","area":"\u534e\u4e1c","area_id":"300000","region":"\u5c71\u4e1c\u7701","region_id":"370000","city":"\u4e1c\u","city_id":"370500","county":"","county_id":"-1","isp":"\ua","isp_id":"100026","ip":"60.214.183.158"}}
用法: 代码如下:String arr1 = GetAddressByIp.GetAddressByIp("120.192.182.1");System.out.println(arr1);详细请看代码
代码如下:package getAddressByIp;
import java.io.ByteArrayOutputSimport java.io.IOEimport java.io.InputSimport java.net.HttpURLCimport java.net.MalformedURLEimport java.net.URL;
import net.sf.json.JSONO
public class GetAddressByIp{&&/**& * & * @param IP& * @return& */&public static String GetAddressByIp(String IP){&&String resout = "";&&try{&& String str = getJsonContent("http://ip.taobao.com/service/getIpInfo.php?ip="+IP);&& System.out.println(str);&& JSONObject obj = JSONObject.fromObject(str);&& JSONObject obj2 =& (JSONObject) obj.get("data");&& String code = (String) obj.get("code");&& if(code.equals("0")){&&& resout =& obj2.get("country")+"--" +obj2.get("area")+"--" +obj2.get("city")+"--" +obj2.get("isp");&& }else{&&& resout =& "IP地址有误";&& }&&}catch(Exception e){&&&e.printStackTrace();&&& resout = "获取IP地址异常:"+e.getMessage();&&}&&&}&&& public static String getJsonContent(String urlStr)&&& {&&&&&&& try&&&&&&& {// 获取HttpURLConnection连接对象&&&&&&&&&&& URL url = new URL(urlStr);&&&&&&&&&&& HttpURLConnection httpConn = (HttpURLConnection) url&&&&&&&&&&&&&&&&&&& .openConnection();&&&&&&&&&&& // 设置连接属性&&&&&&&&&&& httpConn.setConnectTimeout(3000);&&&&&&&&&&& httpConn.setDoInput(true);&&&&&&&&&&& httpConn.setRequestMethod("GET");&&&&&&&&&&& // 获取相应码&&&&&&&&&&& int respCode = httpConn.getResponseCode();&&&&&&&&&&& if (respCode == 200)&&&&&&&&&&& {&&&&&&&&&&&&&&& return ConvertStream2Json(httpConn.getInputStream());&&&&&&&&&&& }&&&&&&& }&&&&&&& catch (MalformedURLException e)&&&&&&& {&&&&&&&&&&& e.printStackTrace();&&&&&&& }&&&&&&& catch (IOException e)&&&&&&& {&&&&&&&&&&& e.printStackTrace();&&&&&&& }&&&&&&& return "";&&& }&&& private static String ConvertStream2Json(InputStream inputStream)&&& {&&&&&&& String jsonStr = "";&&&&&&& // ByteArrayOutputStream相当于内存输出流&&&&&&& ByteArrayOutputStream out = new ByteArrayOutputStream();&&&&&&& byte[] buffer = new byte[1024];&&&&&&& int len = 0;&&&&&&& // 将输入流转移到内存输出流中&&&&&&& try&&&&&&& {&&&&&&&&&&& while ((len = inputStream.read(buffer, 0, buffer.length)) != -1)&&&&&&&&&&& {&&&&&&&&&&&&&&& out.write(buffer, 0, len);&&&&&&&&&&& }&&&&&&&&&&& // 将内存流转换为字符串&&&&&&&&&&& jsonStr = new String(out.toByteArray());&&&&&&& }&&&&&&& catch (IOException e)&&&&&&& {&&&&&&&&&&& // TODO Auto-generated catch block&&&&&&&&&&& e.printStackTrace();&&&&&&& }&&&&&&& return jsonS&&& }}
您可能感兴趣的文章:
大家感兴趣的内容
12345678910
最近更新的内容
常用在线小工具博客分类:
有兴趣开发阿里软件的朋友来看了,这是一个官方demo:基于java语言的,用淘宝API客户端1.0版本,调用接口taobao.items.onsale.get,获取卖家出售中的商品列表
代码如下:
package com.taobao.api.demo.
import java.io.IOE
import java.net.URLE
import javax.servlet.ServletE
import javax.servlet.http.HttpS
import javax.servlet.http.HttpServletR
import javax.servlet.http.HttpServletR
import org.apache.commons.logging.L
import org.apache.commons.logging.LogF
import com.taobao.api.TaobaoApiE
import com.taobao.api.TaobaoJsonRestC
import com.taobao.api.TaobaoRestC
import com.taobao.api.model.ItemsGetR
import com.taobao.api.model.ItemsOnSaleGetR
* @version
* @author &a href="mailto:"&zixue&/a&
public class MainServlet extends HttpServlet {
private static final transient Log log = LogFactory
.getLog(MainServlet.class);
private static final long serialVersionUID = 9766274L;
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String appkey = req.getParameter("appkey");
String secret = req.getParameter("secret");
if (log.isDebugEnabled()) {
log.debug(appkey + ":" + secret);
if (appkey != null && secret != null) {
//生成客户端实例
TaobaoRestClient client = new TaobaoJsonRestClient(appkey,secret);
//淘宝卖家的出售的商品列表
ItemsOnSaleGetRequest tbReq = new ItemsOnSaleGetRequest()
.withFields("iid,title,price,list_time,num")
.withPageNo(1)
.withPageSize(20);
// use appkey as session_id
ItemsGetResponse tbRsp = client.itemsOnSaleGet(tbReq, appkey);//搜索单个商品信息的Request请求
if (log.isDebugEnabled()) {
log.debug(tbRsp.getBody());
//如果接口调用成功
if (!tbRsp.isSuccess()) {
if (tbRsp.isRedirect()) {
req.setAttribute("url",
tbRsp.getRedirectUrl(URLEncoder
(req.getRequestURL()
.append('?')
.append("appkey=")
.append(appkey)
.append("&secret=")
.append(secret)
.toString(),"utf-8")
req.setAttribute("msg", tbRsp.getMsg());
req.setAttribute("items", tbRsp.getItems());
} catch (TaobaoApiException e) {
throw new ServletException(e);
req.getRequestDispatcher("main.jsp").forward(req, resp);
protected void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
doPost(req, resp);
浏览 12822
han2000lei
浏览: 230413 次
来自: 济南
问题已解决,见下面代码
public void validat ...
你好,我想请教你一个问题,我们打算开发一个BS架构的卖家服务应 ...
properties文件直接放到src路径下,
Propert ...
朋友,你这个支持群发吗?为什么我按照你的写法来了改了一下,做成 ...
终于找到了
(window.slotbydup=window.slotbydup || []).push({
id: '4773203',
container: s,
size: '200,200',
display: 'inlay-fix'使用淘宝IP库获取用户ip地理位置_php实例
淘宝公布了他们的IP库http://ip.taobao.com/,还有REST API接口,不过每个用户的访问频率需小于10qps,访问方
式:http://ip.taobao.com/service/getIpInfo.php?ip=[ip地址字串],返回内容以json格式的。具有IP查询,IP统计等功能。各大运营商拥有的IP数等信息。接下来介绍一下获取ip的实例:
复制代码 代码如下:
&?php /**   * 通过淘宝IP接口获取IP地理位置   * @param string $ip   * @return: string   **/   function getCity($ip)
  $url="http://ip.taobao.com/service/getIpInfo.php?ip=".$
  $ipinfo=json_decode(file_get_contents($url));
  if($ipinfo-&code=='1'){
  $city = $ipinfo-&data-&region.$ipinfo-&data-&
  return $
  header("Content-Type:text/;charset=utf-8");
  var_dump(getCity("112.234.69.189"));怎么用java调用淘宝开发平台获得淘宝的所有的类目,要详细的接口_百度知道
怎么用java调用淘宝开发平台获得淘宝的所有的类目,要详细的接口
想做一个导购网站
采集淘宝的类目
需要调用开发平台的那个接口
我有更好的答案
淘宝的API有些是开放的,我查了一下所有类目的API是收费的,需要申请APIKey,然后调用对应查询接口,就能返回XML或JSON数据进行自己的处理了。如果真是开发需要,就可以买淘宝的服务啊。调用很简单,比如taobao.itemcats.get 接口(获取后台供卖家发布商品的标准商品类目),如下Java代码就返回结果了(前提是提供APIKey,需要申请审核)TaobaoClient&client=new&DefaultTaobaoClient(url,&appkey,&secret);ItemcatsGetRequest&req=new&ItemcatsGetRequest();req.setFields(&cid,parent_cid,name,is_parent&);req.setParentCid(L);req.setCids(&,&);ItemcatsGetResponse&response&=&client.execute(req);另外,变通的方法是自己开发爬虫,在淘宝页面上抓取,但问题是得不到淘宝技术支持,比如淘宝改版后也得跟着改动爬虫代码。
采纳率:85%
来自团队:
淘宝的API有些是开放的,我查了一下所有类目的API是收费的,需要申请APIKey,然后调用对应查询接口,就能返回XML或JSON数据进行自己的处理了。如果真是开发需要,就可以买淘宝的服务啊。调用很简单,比如taobao.itemcats.get 接口(获取后台供卖家发布商品的标准商品类目),如下Java代码就返回结果了(前提是提供APIKey,需要申请审核)123456
TaobaoClient client=new DefaultTaobaoClient(url, appkey, secret);ItemcatsGetRequest req=new ItemcatsGetRequest();req.setFields(&cid,parent_cid,name,is_parent&);req.setParentCid(L);req.setCids(&,&);ItemcatsGetResponse response = client.execute(req);另外,变通的方法是自己开发爬虫,在淘宝页面上抓取,但问题是得不到淘宝技术支持,比如淘宝改版后也得跟着改动爬虫代码。
为您推荐:
其他类似问题
开发平台的相关知识
换一换
回答问题,赢新手礼包
个人、企业类
违法有害信息,请在下方选择后提交
色情、暴力
我们会通过消息、邮箱等方式尽快将举报结果通知您。

我要回帖

更多关于 java调用windows api 的文章

 

随机推荐