zoukankan      html  css  js  c++  java
  • 接口返回数据是一条数据和一个数组的区别

    例1:

    [我:me] ---------->api url stock/me 
    with token:WudYqKDKzijeMcrmYcP.qFiGgIavFs0u response:{"desc":"","name":"1460630091572","balance":100000000,"sex":"","nickName":"1460630091572","logo":"http://115.28.189.219/player_icon/19.png","watched":0,"phone":""} 

    public static void getMe() {

    try{
    String getMe_URL = "http://115.28.189.219:9898/stock/me?access_token="+access_token;
    //创建连接
    URL url = new URL(getMe_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestMethod("GET");
    connection.setUseCaches(false);
    connection.setInstanceFollowRedirects(true);

    //connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");

    connection.connect();



    //读取响应
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    connection.getInputStream()));
    String lines;
    StringBuffer sb = new StringBuffer("");
    while ((lines = reader.readLine()) != null) {
    lines = new String(lines.getBytes(), "utf-8");
    sb.append(lines);
    }
    System.out.println(sb);


    ////////解析返回数据

    String retString = sb.toString();

    JSONObject retObject = JSONObject.fromObject(retString);

    System.out.println(retObject.getString("balance"));


    reader.close();
    // 断开连接
    connection.disconnect();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }

    }

    例2:

    public static void myInvestment() {

    try{
    String myInvestment_URL = "http://115.28.189.219:9898/stock/products?access_token="+access_token;
    //创建连接
    URL url = new URL(myInvestment_URL);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setDoOutput(true);
    connection.setDoInput(true);
    connection.setRequestMethod("GET");
    connection.setUseCaches(false);
    connection.setInstanceFollowRedirects(true);

    //connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
    connection.setRequestProperty("Content-Type","application/json; charset=UTF-8");

    connection.connect();
    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");


    //读取响应
    BufferedReader reader = new BufferedReader(new InputStreamReader(
    connection.getInputStream()));
    String lines;
    StringBuffer sb = new StringBuffer("");
    while ((lines = reader.readLine()) != null) {
    lines = new String(lines.getBytes(), "utf-8");
    sb.append(lines);
    }
    System.out.println(sb);


    ////////解析返回数据

    String retString = sb.toString();

    // JSONObject retObject = JSONObject.(retString);

    JSONArray authorJsonArray = JSONArray.fromObject(retString);

    for(int i = 0; i < authorJsonArray.size();i++)
    {
    JSONObject retObject0 = authorJsonArray.getJSONObject(i);

    System.out.println(retObject0.getString("desc"));
    }


    System.out.println("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");


    reader.close();
    // 断开连接
    connection.disconnect();
    } catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }

    解析返回数据部分是区别,返回数组时,应按照例2写

  • 相关阅读:
    可以支持jQuery1.10.1 的 fancybox 1.3.4, 並現在type為Ajax時,也可以定義窗口的大小。
    用tensorlayer导入Slim模型迁移学习
    用tensorflow迁移学习猫狗分类
    Serenity框架官方文档翻译3.2(多租户)
    Serenity框架官方文档翻译3.1(教程)
    Serenity框架官方文档翻译前言(什么是Serenity平台)
    Serenity框架官方文档翻译(1-2开始、安装和界面)
    GIN+GORILLA=A GOLANG WEBSOCKET SERVER
    【Advanced Windows Phone Programming】在windows phone 8中解码mp3 和编码pcm
    LIstView 滚动 异步 加载更多 mono for android ScrollStateChanged ScrollState.Idle; Fling;TouchScroll
  • 原文地址:https://www.cnblogs.com/fenr9/p/5394214.html
Copyright © 2011-2022 走看看