2016-08-02 70 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