2

I'm trying to parse the json from the api http://badiyajobs.com/apis/v1/roles
but response.isSuccess is returning false
my modal class is,

package arpit.retrodemo;


import java.util.List;
import java.util.ArrayList;

public class Modal {           

    private List<RolesEntity> roles = new ArrayList<>();

    public void setRoles(List<RolesEntity> roles) {
        this.roles = roles;
    }

    public List<RolesEntity> getRoles() {
        return roles;
    }

    public static class RolesEntity {
        private String id;
        private String role;
        private String description;
        private String icon_url;
        private String created_at;
        private String updated_at;

        public void setId(String id) {
            this.id = id;
        }

        public void setRole(String role) {
            this.role = role;
        }

        public void setDescription(String description) {
            this.description = description;
        }

        public void setIcon_url(String icon_url) {
            this.icon_url = icon_url;
        }

        public void setCreated_at(String created_at) {
            this.created_at = created_at;
        }

        public void setUpdated_at(String updated_at) {
            this.updated_at = updated_at;
        }

        public String getId() {
            return id;
        }

        public String getRole() {
            return role;
        }

        public String getDescription() {
            return description;
        }

        public String getIcon_url() {
            return icon_url;
        }

        public String getCreated_at() {
            return created_at;
        }

        public String getUpdated_at() {
            return updated_at;
        }
    }
}


Sample json is,

{"roles":[
    {
      "id":"1",
      "role":"Retail Sales Executive",
      "description":"As a sales assistant....",
      "icon_url":"",
      "created_at":"2015-10-02 12:03:03",
      "updated_at":null
    }
  ] 
}


APIService.java is,

package arpit.retrodemo;

import retrofit.Call;
import retrofit.http.GET;

public interface APIService {
    @GET("/roles")
    Call<Modal> getDetails();
}


MainActivity.java is,

package arpit.retrodemo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;


import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import retrofit.Call;
import retrofit.Callback;
import retrofit.GsonConverterFactory;
import retrofit.Response;
import retrofit.Retrofit;

public class MainActivity extends AppCompatActivity {

    private static final String ENDPOINT = "http://badiyajobs.com/apis/v1";
    private APIService service;
    private Modal mod;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(ENDPOINT)
                .build();
        service = retrofit.create(APIService.class);
        Call<Modal> userList = service.getDetails();
        userList.enqueue(new Callback<Modal>() {
            @Override
            public void onResponse(Response<Modal> response) {
                if(response.isSuccess()){
                    Log.d("findRes", response.body().toString());
                }else{
                    Log.d("find", "Something is wrong! " + response.errorBody().toString());
                }
            }

            @Override
            public void onFailure(Throwable t) {
                Log.d("findError", t.getMessage());
            }
        });
    }
}


Here i'm getting the following on logcat,

D/find: Something is wrong! com.squareup.okhttp.ResponseBody$1@52715c98


instead of the string representation of response.body()

1
  • Can you simply debug and check the error by inspecting response argument in your onResponse callback? Commented Jan 16, 2016 at 21:27

1 Answer 1

2

You need to do the following:

public interface APIService {
    @GET("roles") /*removed the / here */
    Call<Modal> getDetails();
}

private static final String ENDPOINT = "http://badiyajobs.com/apis/v1/"; /* added the / here */
Sign up to request clarification or add additional context in comments.

2 Comments

Hey it worked, thanks, and can you give me just a little explanation of why did changing those work?
Check out the part at Setup - Retrofit Type (31:24) in realm.io/news/droidcon-jake-wharton-simple-http-retrofit-2 and New URL resolving concept. The same way as <a href> in inthecheesefactory.com/blog/retrofit-2.0/en essentially the URI resolving changed and now instead of concatenating the two together, it does some weird magic that's supposedly somehow better. Although I personally think it's just a really easy way to make a non-intuitive mistake, and I don't see the benefit. Essentially, you have to put the / in the BaseURL's end, and not on the service method's lead.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.