0

Hi i'm trying to learn android and currently implementing retrofit and tried to solve this using related post here sadly nothing Helped me pls hear me out.

I have a json data that i need to parse here it is

{
"-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"
}}

Then some tutorials told me to use a hashmap so i implemented mine here is my ModelClass1.class

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

ModeClass2.class - For handling the objects

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;
}

}

My API is here UPDATED

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

ANd lastly i'm calling my handler here UPDATED

       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) {

                }
            });

And everytime i TOast the outcome i got a null value did i implemented it wrong? Im certain i used my RootUrl correctly if this is not the problem what are the other methods i can use? please help me.

here is my Logcat UPDATED

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)
Sufian
  • 6,405
  • 16
  • 66
  • 120
user3262438
  • 73
  • 1
  • 8
  • you should definitely move to Retrofit2. Which version of Gson are you using ? – Blackbelt Aug 02 '16 at 08:33
  • @cricket_007 - just found it after updating my answer but now fixed and accepted a legit answer for my previous problem pls don't kill the post because it might help somebody in the future will revert it back to previous error – user3262438 Aug 03 '16 at 02:07
  • @user3262438 please don't change your question if someone has answered your original issue. Post a new question instead, but make sure it isn't a duplicate. – Sufian Aug 03 '16 at 05:17
  • @Sufian - ow so that's why thank you for pointing it out i'm a a bit new here so i'm not well informed thanks mate cheers! – user3262438 Aug 04 '16 at 02:46

2 Answers2

2

Update api definition to:

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

And api call to:

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

Update:

To get all accountNames add this code to your callback:

...

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

Check out this article: Getting Starter with Retrofit 2

Use Retrofit2 Call with set up converter factory to enqueue your data

Your problem is in serialization: There is no marksamples field in respond. You don't need special MarkSamples class. Look how you can modify your code:

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();
}

and request:

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) {

    }
});
AnoDest
  • 333
  • 1
  • 3
  • 13