2016-09-20 91 views
2

Microsoft Academic提供了一個API來獲取來自Microsoft學術機構的一些常規信息。響應類型是Json對象。使用org.Json和下面的代碼,我試圖讀取響應對象,但我失敗了(需要下載these罐+共記錄和常見的編解碼器):找不到Json對象(使用org.json)

URIBuilder builder = new URIBuilder("https://api.projectoxford.ai/academic/v1.0/evaluate?"); 

    builder.setParameter("expr", "Composite(AA.AuN=='jaime teevan')"); 
    builder.setParameter("count", "100"); 
    builder.setParameter("attributes", "Ti,CC"); 

    URI uri = builder.build(); 
    HttpGet request = new HttpGet(uri); 

    request.setHeader("Ocp-Apim-Subscription-Key", "Your-Key"); 


     HttpClient httpclient = HttpClients.createDefault(); 

     HttpResponse response = httpclient.execute(request); 
     HttpEntity entity = response.getEntity(); 


     if (entity != null) { 

      JSONObject obj = new JSONObject(entity); 


      JSONArray arr = obj.getJSONArray("entities"); 
      for (int i = 0; i < arr.length(); i++){ 

      String post_id = arr.getJSONObject(i).getString("Ti"); 
       System.out.println(post_id); 

      }  

      System.out.println(EntityUtils.toString(entity)); 
     } 

它返回以下異常:

Exception in thread "main" org.json.JSONException: JSONObject["entities"] not found. 
at org.json.JSONObject.get(JSONObject.java:471) 
at org.json.JSONObject.getJSONArray(JSONObject.java:618) 

如何解決這個問題?

編輯 雖然很容易看到我在我的問題(微軟學術)年初提供的鏈接響應的例子,但爲了方便讀者的我展現它在這裏:

{ 
    "expr": "Composite(AA.AuN=='jaime teevan')", 
    "entities": 
    [ 
    { 
     "logprob": -15.08, 
     "Ti": "personalizing search via automated analysis of interests and activities", 

     "CC": 372, 
    }, 
    { 
     "logprob": -15.389, 
     "Ti": "the perfect search engine is not enough a study of orienteering behavior in directed search", 
     "CC": 237, 


    } 
    ] 
} 
+0

答案如何?你有可能收到錯誤信息嗎?從錯誤消息中可以看到返回的對象中沒有'entities'屬性。 – Henry

+0

請點擊我的問題中的第一個單詞(Microsoft Academic)以查看回復的示例。當然,當我使用'System.out.println(EntityUtils.toString(實體));'我可以看到整個響應。 – user3049183

回答

1

好像對我來說,你是不是你的反應轉化爲string,你需要將它傳遞給JSONObject

HttpEntity entity = response.getEntity(); 
    ByteArrayOutputStream os = new ByteArrayOutputStream(); 
    try { 
     entity.writeTo(os); 
    } catch (IOException e1) { 
    } 
    String contentString = new String(os.toByteArray()); 
之前,你的反應轉化爲 string問題

或另一種方式是

InputStream instream = entity.getContent(); 
BufferedReader reader = new BufferedReader(new InputStreamReader(instream)); 
    StringBuilder sb = new StringBuilder(); 

    String line = null; 
    try { 
     while ((line = reader.readLine()) != null) { 
      sb.append(line + "\n"); 
     } 
    } catch (IOException e) { 
     e.printStackTrace(); 
    } finally { 
     try { 
      is.close(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 
String contentString = sb.toString(); // you can pass sb.toString() directly to jsonobject as well 

,現在通過contentStringJSONObject

JSONObject obj = new JSONObject(contentString); 
JSONArray arr = obj.getJSONArray("entities"); 

更新:您還可以使用this也由@建議奧馬爾法迪勒USTA 但我會強烈建議使用HttpURLConnection爲安全和性能

+0

HttpResponse,同樣你已經有 –

+0

把你的更新子句放在你的問題中帶有錯誤的新代碼,我希望你沒有這樣做兩次'entity.getContent();'在你的代碼中 –

+1

我發現了我的錯誤,這就是爲什麼我刪除了我的評論。愚蠢的錯誤。 – user3049183

1

嘗試將字符串JsonData傳遞給JSONObject:

if (entity != null) { 
    String jsonData = EntityUtils.toString(entity); 
    JSONObject obj = new JSONObject(jsonData); 
    ........ 
    ..... 
} 
+0

它只適用於android而不是java –

+0

@PavneetSingh你確定嗎?因爲我嘗試過我的原生Java平臺(沒有安裝任何android sdks)和EntityUtils org.apache.http.util.EntityUtils –

+0

@PavneetSingh它的工作原理以及你的回答 – user3049183