Untitled

 avatar
unknown
plain_text
4 years ago
2.0 kB
8
Indexable
package com.example.geektrust;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;

import javax.net.ssl.HttpsURLConnection;

import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.type.TypeReference;

import org.json.JSONException;
import org.json.JSONObject;

public class JsonHandler {
    
    public  String getJSON(String url) {
        HttpsURLConnection con = null;
        try {
            URL u = new URL(url);
            con = (HttpsURLConnection) u.openConnection();

            con.connect();


            BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream()));
            StringBuilder sb = new StringBuilder();
            String line;
            while ((line = br.readLine()) != null) {
                sb.append(line + "");
            }
            br.close();
            return sb.toString();


        } catch (MalformedURLException ex) {
            ex.printStackTrace();
        } catch (IOException ex) {
            ex.printStackTrace();
        } finally {
            if (con != null) {
                try {
                    con.disconnect();
                } catch (Exception ex) {
                    ex.printStackTrace();
                }
            }
        }
        return null;
    }

    public List<Fund> StringToListConversion(String allFundsJson) throws JSONException, JsonParseException, JsonMappingException, IOException{
        JSONObject jsonObject = new JSONObject(allFundsJson);
        ObjectMapper mapper = new ObjectMapper();
        List<Fund> allFunds = mapper.readValue(jsonObject.getString("funds"), new TypeReference<List<Fund>>(){});
        return allFunds;
        
    }
}   
Editor is loading...