2017-08-02 22 views
0

我有一個問題,將數據推到我的索引。我的代碼如下:如何使用將數據推送到我的索引algolia的Android

Client client = new Client("APP_ID", "SEARCH_ID"); 

      Index myIndex = client.initIndex("test"); 
      JSONObject jsonObject = null; 
      try { 
       jsonObject = new JSONObject() 
         .put("name", "Jimmie") 
         .put("username", "Barninger") 
      } catch (JSONException e) { 
       e.printStackTrace(); 
      } 
      myIndex.addObjectAsync(jsonObject, null); 

但是沒有任何反應,在儀表板中沒有任何數據可見。我究竟做錯了什麼?

+0

你好,請出示你得到這樣我們可以更好地幫助你的錯誤? –

+0

嗨,沒有任何錯誤,它只是不推數據對象 –

+0

爲了調試,就可以更換第二個參數'null'用[CompletionHandler(https://github.com/algolia/algoliasearch-客戶端的Android/BLOB/1f63db6ebdd3077caa91e74c6553d94e1bcb31d4/algoliasearch/src目錄/主/ JAVA/COM/algolia /搜索/薩斯/ CompletionHandler.java)和結帳,如果你得到一個錯誤?如果是這樣,請複製粘貼到你的問題的錯誤 –

回答

1

爲了瞭解更多,請像這樣的東西更換addobjectAsync行:

index.addObjectAsync(object, new CompletionHandler() { 
    @Override 
    public void requestCompleted(JSONObject content, AlgoliaException error) { 
     if (error != null) { 
      // Check what is the error Here 
     } 
    } 
} 
1

我不得不改變而此前被SEARCH_ID的API密鑰。我把它改成我ADMIN_API_KEY

Client client = new Client("APP_ID", "ADMIN_API_KEY"); 

這是因爲只有搜索API密鑰只能用於搜索,而管理API密鑰可以更改,刪除或添加的對象。

完整的代碼現在是:

Client client = new Client("APP_ID", "ADMIN_API_KEY"); 

    Index myIndex = client.initIndex("test"); 

    JSONObject object = null; 
    try { 
     object = new JSONObject() 
       .put("username", "Jimmie") 
       .put("name", "Barninger"); 
    } catch (JSONException e) { 
     e.printStackTrace(); 
    } 
    // myIndex.addObjectAsync(object, "myID", null); 
    myIndex.addObjectAsync(object, "id", null); 
+0

聽起來不錯,很高興聽到你找到了解決辦法:) –

相關問題