Package com.junar.api.bbva

Source Code of com.junar.api.bbva.DataStream2

package com.junar.api.bbva;

import java.io.IOException;
import java.util.List;
import java.util.Map;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

public class DataStream2 {
    private String app_key="908c94d730215a89913db11cb288163c82cf8db5";
    private static String guid="BBVA-REGIO-CHILE-SUCUR-REGIO";
    //private static String guid="CHILE-INDIC-UF";
    private String base_uri="http://apisandbox.junar.com";
   
    public static void main(String args[]) {
        try {
          //CHILE-INDIC-UF
            DataStream2 ds = new DataStream2(guid);
            String arrayS[] = {""};
//            System.out.println("Info :" +ds.info());
            String jsonEjemplo = "["+ds.invoke(arrayS)+"]";
            //String jsonEjemplo = '[{"id": "CHILE-INDIC-UF", "title": "Chile", "subtitle": "UF", "description": "Daily UF", "result": [{"null":22322.04},{"timestamp":0}], "source": "http://www.bcentral.cl/", "link": "http://www.junar.com/datastreams/694/chile-uf/"}]';
            System.out.println("DS INFO : " + jsonEjemplo);
            //String jsonEjemplo = ""+ds.invoke(arrayS);
            //String[] column = gson.fromJson(test, Object[][].class);
          //  Object[][] column = new Gson().fromJson(jsonEjemplo, Object[][].class);
            Gson gson = new Gson();
                   
                   
                    List<Category> table = gson.fromJson(jsonEjemplo, new TypeToken<List<Category>>(){}.getType());
                String salida;
                    for (Category tab : table) {
                      System.out.println("#########");
                      System.out.println("ID:  " + tab.getId());
                      System.out.println("Subtitle:  " + tab.getSubtitle());
                      System.out.println("Title:  " + tab.getTitle());
                      System.out.println("Descripci�n:  " + tab.getDescription());
                        System.out.println("#########");
                        int length = tab.getResult().getfArray().length;
                        int contadorSucursal = 0;
                        for(int i = 0; i <length;i=i+4){
                         
                          salida  = tab.getResult().getfArrayPosicion(i).getfStr()+"/"+tab.getResult().getfArrayPosicion(i+1).getfStr()
                              +"/"+tab.getResult().getfArrayPosicion(i+2).getfStr() +"/"+ tab.getResult().getfArrayPosicion(i+3).getfStr();
                          System.out.println(salida);
//                        System.out.println(""+i+" Fstr  " + tab.getResult().getfArrayPosicion(i).getfStr());
//                        System.out.println(""+i+"Ftype:  " + tab.getResult().getfArrayPosicion(i).getfType());
//                        System.out.println(""+i+" Fstr  " + tab.getResult().getfArrayPosicion(i+1).getfStr());
//                        System.out.println(""+i+"Ftype:  " + tab.getResult().getfArrayPosicion(i+1).getfType());
//                        System.out.println(""+i+" Fstr  " + tab.getResult().getfArrayPosicion(i+2).getfStr());
//                        System.out.println(""+i+"Ftype:  " + tab.getResult().getfArrayPosicion(i+2).getfType());
//                        System.out.println(""+i+" Fstr  " + tab.getResult().getfArrayPosicion(i+3).getfStr());
//                        System.out.println(""+i+"Ftype:  " + tab.getResult().getfArrayPosicion(i+3).getfType());
                          contadorSucursal++;
                        }
                        System.out.println("#########");
                        System.out.println("Sucursal "+contadorSucursal);
                        salida = null;
                    }
//           // String jsonString = ds.info();
//            System.out.println("##");
//            Map<String, String> map2 = new Gson().fromJson(jsonEjemplo, new TypeToken<Map<String, String>>() {}.getType());
//            System.out.println("ACA"+map2);
           
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
   
    public DataStream2(String p_guid) {
        this.setGUID(p_guid);
    }
   
    public String getGUID() {
        return guid;
    }
   
    public String getAuthKey() {
        return this.app_key;
    }
    public void setGUID(String p_guid) {
        guid = p_guid;
    }
   
    public String invoke(String[] p_params) throws ClientProtocolException, IOException {
        String url = "/datastreams/invoke/" + getGUID() + "?";
        url += "auth_key=" + app_key;
       
        for (int i=0; i < p_params.length; i++) {
            url += "&pArgument" + i + "=" + p_params[i];
        }
        //url += "&output=prettyjson";
        return callURI(url);
    }
   
    public String info() throws ClientProtocolException, IOException {
        String url = "/datastreams/" + getGUID() + "?auth_key=" + getAuthKey();
        return callURI(url);
    }
   
    private String callURI(String p_url) throws ClientProtocolException, IOException {
        HttpClient http_client = new DefaultHttpClient();
        String response_body = "";
        String final_url = this.base_uri + p_url;
       
        System.out.println("URL: " + final_url);
        try {
            HttpGet httpget = new HttpGet(final_url);

            ResponseHandler<String> responseHandler = new BasicResponseHandler();
            response_body = http_client.execute(httpget, responseHandler);
        } finally {
            http_client.getConnectionManager().shutdown();
        }
       
        return response_body;
    }
}
TOP

Related Classes of com.junar.api.bbva.DataStream2

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.