2016-08-02 44 views
0

嗨,我想學習機器人,目前正在實施改造,並試圖解決這個使用相關的帖子在這裏可悲沒事幫助我請我聽到。Retrofit HashMap爲null

我有我需要在這裏解析它是

{ 
"-KNea90tV5nZlkeqxc3Q": { 
    "accountName": "Mark Angelo Noquera", 
    "accountNumber": "12435656443", 
    "accountType": "Peso Savings" 
}, 
"-KNeaPmBoTXV4mQC6cia": { 
    "accountName": "Mark Angelo Noquera", 
    "accountNumber": "12435656444", 
    "accountType": "Peso Checking" 
}, 
"-KNeaWe_ZbtI9Tn6l-oF": { 
    "accountName": "Mark Angelo Noquera", 
    "accountNumber": "12435656445", 
    "accountType": "Personal Loan" 
}} 

一個JSON數據那麼一些教程告訴我使用一個HashMap,所以我實現我的 這裏是我的ModelClass1.class

public class MarkSamples { 
public HashMap<String, MarkSample> marksamples; 
public HashMap<String, MarkSample> getMarksamples() { 
    return marksamples; 
}} 

ModeClass2.class - 用於處理對象

public class MarkSample { 

@SerializedName("accountName") 
public String acntName; 
@SerializedName("accountNumber") 
public String acntNumber; 
@SerializedName("accountType") 
public String acntType; 

public String getName() { 
    return (acntName); 
} 

public void setName(String acntName) { 
    this.acntName = acntName; 
} 

public String getNumber() { 
    return (acntNumber); 
} 

public void setNumber(String acntNumber) { 
    this.acntNumber = acntNumber; 
} 

public String getType() { 
    return (acntType); 
} 

public void setType(String acntType) { 
    this.acntType = acntType; 
} 

}

我的API在這裏修訂

public interface ContactsAPI { 
     @GET("/api/accounts.json") 
public void getSamples(Callback<HashMap<String, MarkSample>> response);} 

,最後我在這裏打電話給我的處理程序已更新

 api.getSamples(new Callback<HashMap<String, MarkSample>>() { 
       @Override 
       public void success(HashMap<String, MarkSample> stringMarkSampleHashMap, Response response) { 
        loading.dismiss(); 
        int mint = stringMarkSampleHashMap.size(); 
        Toast.makeText(mainActivity, mint, Toast.LENGTH_SHORT).show(); 
       } 

       @Override 
       public void failure(RetrofitError error) { 

       } 
      }); 

每次我敬酒結果我得到了一個空值沒有我實施它錯了嗎?林肯定我正確使用我的RootUrl如果這不是問題我可以使用其他方法是什麼?請幫幫我。

,這裏是我的logcat 修訂

FATAL EXCEPTION: main 
                    Process: com.exist.kelvs.retrofit2, PID: 2517 
                    android.content.res.Resources$NotFoundException: String resource ID #0x3 
                     at android.content.res.Resources.getText(Resources.java:312) 
                     at android.widget.Toast.makeText(Toast.java:286) 
                     at com.exist.kelvs.retrofit2.RetrofitHandler$2.success(RetrofitHandler.java:51) 
                     at com.exist.kelvs.retrofit2.RetrofitHandler$2.success(RetrofitHandler.java:46) 
                     at retrofit.CallbackRunnable$1.run(CallbackRunnable.java:45) 
                     at android.os.Handler.handleCallback(Handler.java:739) 
                     at android.os.Handler.dispatchMessage(Handler.java:95) 
                     at android.os.Looper.loop(Looper.java:148) 
                     at android.app.ActivityThread.main(ActivityThread.java:5417) 
                     at java.lang.reflect.Method.invoke(Native Method) 
                     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+0

你一定要移動到Retrofit2。你正在使用哪個版本的Gson? – Blackbelt

+0

@ cricket_007 - 更新我的答案後才發現它,但現在修復並接受了我以前的問題的合法答案請不要殺了帖子,因爲它可能會幫助某人在未來將其恢復到以前的錯誤 – user3262438

+0

@ user3262438請不要如果有人回答了您的原始問題,那麼您的問題就不會改變。改爲發佈新問題,但確保它不是重複的。 – Sufian

回答

1

更新API定義:

@GET("/api/accounts.json") 
public void getSamples(Map<String,MarkSample> response);} 

和API調用:

api.getSamples(new Callback<Map<String,MarkSample>() { 
     @Override 
     public void success(Map<String,MarkSample> samplelist, Response response) { 
      loading.dismiss(); 
      int mint = samplelist.size(); 
... 

更新:

要獲得所有accountNames此代碼添加到您的回調:

... 

loading.dismiss(); 
int mint = 0; 
if (samplelist!=null){ 
    mint = samplelist.size(); 
    for (MarkSample item:samplelist.values()){ 
    Log.d("TEST","value: "+item.getName(); 
    } 
} 
... 
+0

@Alexy Rogovoy - 上面的變化給我一個新的錯誤「android.content.res.Resources $ NotFoundException:字符串資源ID#0x3」 在這裏請參閱已編輯的文章完整的logcat – user3262438

+0

@ user3262438添加更新的「成功」方法和一個新的錯誤日誌貓,請 –

+0

完成更新請檢查謝謝 – user3262438

0

看看這篇文章: Getting Starter with Retrofit 2

使用Retrofit2呼叫與設置變頻器出廠入隊的數據

你的問題正在序列化中: 響應中沒有marksamples字段。你不需要特殊的MarkSamples類。看看你能如何修改代碼:

public interface ContactsAPI { 

    @GET("/api/accounts.json") 
    Call<LinkedHashMap<String, MarkSample>> getSamples(); 

    Retrofit retrofit = new Retrofit.Builder() 
     .baseUrl(" https://pbcom.firebaseio.com") 
     .addConverterFactory(GsonConverterFactory.create()) 
     .build(); 
} 

和要求:

ContactsAPI api = ContactsAPI.retrofit.create(ContactsAPI.class); 
Call<LinkedHashMap<String, MarkSample>> call = api.getSamples(); 
call.enqueue(new Callback<LinkedHashMap<String, MarkSample>>() { 
    @Override 
    public void onResponse(Call<LinkedHashMap<String, MarkSample>> call, Response<LinkedHashMap<String, MarkSample>> response) { 
     LinkedHashMap<String, MarkSample>> samples = response.body(); 
    } 

    @Override 
    public void onFailure(Call<LinkedHashMap<String, MarkSample>> call, Throwable t) { 

    } 
}); 
+0

我可以在改造1.9上使用此功能嗎? – user3262438

+0

不,1.9我不知道支持'Call'。 但重點是你有序列化時不必要的類。 – AnoDest