2016-04-10 3 views
0

私はKairos APIを使用しています。登録リクエストが成功しました。カイロスダッシュボードで表示されます。残念ながら私はJSONのフォーマットされたリクエストをキャッチできません。これは文字列に格納しようとします。応答は次のようになります。AndroidアプリのCatch Jsonレスポンス

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

このコードを使用しようとしています。 結果は空の文字列です。

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

投稿convertinputStreamToString()関数 –

+0

@Abhi:追加されました。ありがとう! – plaidshirt

+0

なぜあなたはvolleyライブラリを使用しないのですか? –

答えて

1

使用をテストされていません。

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

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

文字列に 'NullPointerException'があるので、キャッチできません。 – plaidshirt

+0

問題はあなたのウェブサービス –

+0

あなたはどう思いますか?サービスの問題は何ですか? – plaidshirt

0

あなたはJSONデータを受信して​​解析するJsonObjectを使用することができます。 それは、このクラスは、それはあなたの文字列REPONSEを返します

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

これはどのように認証を行うことができますか? 'JSON'の解析にも同様の関数を使用しますが、ソース文字列は空です。 – plaidshirt

+0

APIがパラメータを使用してリクエストを作成できる場合は、URLに認証情報を渡すか、HttpPostリクエストを作成してからレスポンスを受け取る必要があります。そのAPIの要件まで –