我正在使用Kairos API,注册请求成功,我在Kairos Dashboard上看到它。不幸的是我无法捕获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 "";
}
}
答案 0 :(得分:1)
使用此类它将返回字符串响应。
Connection con = new Connection();
String resp=con.connect("url");
这是连接类。
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;
}
}
答案 1 :(得分:0)
您可以使用JsonObject
来接收和解析JSON
数据。
它没有经过测试
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"));
}