0

I am using Kairos API, enroll request is successful, I see it on Kairos Dashboard. Unfortunately I cannot catch JSON formatted request, which I try to store in a string. Response should looks like this:

200
Content-Type: application/json
{
    "images": [
    {
        "time": 3.43817,
        "transaction": {
        "status": "success",
        "face_id": "685ff4b47a3db8579efd2fa6a7d9293b",
        "subject_id": "subtest1",
        "width": 934,
        "height": 934,
        "topLeftX": 300,
        "topLeftY": 526,
        "timestamp": "1417207442",
        "gallery_name": "gallerytest1"
        },
        "attributes": {
            "gender": {
                "type": "F",
                "confidence": "80%"
            }
        }
    }]
}

I try to use this code. Result is an empty string.

public String jsonParsing(){
    String parsedString = "";
    try {
        String urlStr = "https://api.kairos.com/enroll";
        URL url = new URL(urlStr);
        URLConnection conn = url.openConnection();

        HttpURLConnection httpConn = (HttpURLConnection) conn;
        httpConn.setAllowUserInteraction(false);
        httpConn.setInstanceFollowRedirects(true);
        httpConn.setRequestMethod("GET");
        httpConn.connect();

        InputStream is = httpConn.getInputStream();
        parsedString = convertinputStreamToString(is);

    } catch (Exception e) {
        e.printStackTrace();
    }

    return parsedString;
}

convertinputStreamToString() :

public static String convertinputStreamToString(InputStream ists)
        throws IOException {
    if (ists != null) {
        StringBuilder sb = new StringBuilder();
        String line;

        try {
            BufferedReader r1 = new BufferedReader(new InputStreamReader(
                    ists, "UTF-8"));
            while ((line = r1.readLine()) != null) {
                sb.append(line).append("\n");
            }
        } finally {
            ists.close();
        }
        return sb.toString();
    } else {
        return "";
    }
}
plaidshirt
  • 5,189
  • 19
  • 91
  • 181

2 Answers2

1

Use this class it will return you string reponse.

Connection con = new Connection();
String resp=con.connect("url");

here is the class Connection.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Created by Zeeshan on 12/29/2015.
 */
public class Connection {
    URL url1 = null;
    HttpURLConnection httpURLConnection= null;
    BufferedReader reader;
    String json=null;
    public String connect(String url){
        try {
            url1= new URL(url);
            httpURLConnection=(HttpURLConnection)url1.openConnection();
            httpURLConnection.connect();
            InputStream in = httpURLConnection.getInputStream();
            reader=new BufferedReader(new InputStreamReader(in));
            StringBuffer buffer= new StringBuffer();
            String line="";
            while((line=reader.readLine())!=null){
                buffer.append(line);
            }
            json=buffer.toString();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        finally {
            if(httpURLConnection!=null){
                httpURLConnection.disconnect();
            }
            if(reader!=null){
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return json;
    }
}
Zeeshan Shabbir
  • 6,704
  • 4
  • 38
  • 74
0

You can use JsonObject to receive and parse JSON data. It is not tested

java.net.URL json = new java.net.URL("http://https://api.kairos.com/enroll");
                URLConnection jc = json.openConnection();
                BufferedReader reader = new BufferedReader(new InputStreamReader(jc.getInputStream()));

                String line = reader.readLine();

                JSONObject jsonResponse = new JSONObject(line);
                JSONArray jsonArray = jsonResponse.getJSONArray("images");

                for (int i = 0; i < jsonArray.length(); i++) {

                    JSONObject jObject = (JSONObject)jsonArray.get(i);

                    // "FullName" is the property of .NET object spGetPersonsResult,
                    // and also the name of column in SQL Server 2008
                    listItems.add(jObject.getString("time"));
                }
Jibran Khan
  • 3,236
  • 4
  • 37
  • 50
  • How can this do the authentication? I would use a similar function for `JSON` parsing, but source string is empty. – plaidshirt Apr 10 '16 at 13:19
  • If the API allows to create request with parameters, you can pass authentication credentials in the URL or you have to create `HttpPost` request and then receive the response. Its up to the API requirements – Jibran Khan Apr 10 '16 at 13:23