2016-04-10 1 views
0

J'utilise Kairos API, la demande d'inscription est réussie, je le vois sur Kairos Dashboard. Malheureusement, je ne peux pas saisir la requête formatée JSON, que j'essaie de stocker dans une chaîne. La réponse devrait ressembler à ceci:Réponse Catch Json dans l'application Android

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%" 
      } 
     } 
    }] 
} 

J'essaie d'utiliser ce code. Le résultat est une chaîne vide.

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 ""; 
    } 
} 
+1

post convertinputStreamToString() fonction –

+0

@Abhi: Ajoutée. Merci! – plaidshirt

+0

pourquoi n'utilisez-vous pas la bibliothèque de volley? –

Répondre

1

Utilisez cette classe, il retournera vous chaîne Reponse.

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

Voici la connexion de classe.

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; 
    } 
} 
+0

Il ne peut pas l'attraper, parce que j'obtiens 'NullPointerException' pour la chaîne. – plaidshirt

+0

problème avec vous webservice –

+0

Que pensez-vous, quel est le problème avec le service? – plaidshirt

0

Vous pouvez utiliser JsonObject pour recevoir et analyser les données JSON. Il n'a pas été testé

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")); 
       } 
+0

Comment cela peut-il faire l'authentification? J'utiliserais une fonction similaire pour l'analyse 'JSON', mais la chaîne source est vide. – plaidshirt

+0

Si l'API permet de créer une requête avec des paramètres, vous pouvez passer des informations d'authentification dans l'URL ou vous devez créer une requête 'HttpPost' et recevoir la réponse. C'est aux exigences de l'API –