简体   繁体   中英

Android Json parsing with retrofit

I'm using retrofit for the first time and I'm looking to parse some json data but I may have made a mistake initiating the network request on MainActivity. The App doesn't crush but it's not returning any values. it's a Gridlayout with an OnclickListener on each item and I'm only looking to return 2 values (name and Id ). The object currently has 3 items (name, id, and a List<>) this is the Full API end point " https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json "

public class MainActivity extends AppCompatActivity implements 
CakeAdapter.CakeClickedListener {


 RecyclerView mRecyclerView;
 TextView mCakeName;
 ImageView mCakeImage;
 TextView mCakeId;
 private List<CakesItem> mCakeList = new ArrayList<>();


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


    mRecyclerView = findViewById(R.id.cake_list_recycler_view);


    mRecyclerView.setHasFixedSize(true);
    GridLayoutManager mGridLayoutManager = new GridLayoutManager(MainActivity.this, 2);
    final CakeAdapter mCakeAdapter = new CakeAdapter(this);
    mRecyclerView.setLayoutManager(mGridLayoutManager);
    mRecyclerView.setAdapter(mCakeAdapter);
    mCakeAdapter.getCakeData(mCakeList);

    Retrofit retrofit = new Retrofit.Builder()
            .baseUrl(Constants.BASE_URL)
            .addConverterFactory(GsonConverterFactory.create())
            .build();

    BakingJsonApi bakingJsonApi = retrofit.create(BakingJsonApi.class);

    Call<List<CakesItem>> call = bakingJsonApi.getCakes(Constants.JSON_PATH);

    call.enqueue(new Callback<List<CakesItem>>() {
        @Override
        public void onResponse(Call<List<CakesItem>> call, Response<List<CakesItem>> response) {
            if (!response.isSuccessful()) {

                Toast.makeText(MainActivity.this, "Code: " + response.code(), Toast.LENGTH_SHORT).show();
                return;
            }

            List<CakesItem> cakeItem = response.body();
            mCakeAdapter.getCakeData(cakeItem);
        }

        @Override
        public void onFailure(Call<List<CakesItem>> call, Throwable t) {

            Toast.makeText(MainActivity.this, "Unable to load data" + t.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });


  }


public interface BakingJsonApi {


 @GET("/topher/2017/May/59121517_baking/{json}")
 Call<List<CakesItem>> getCakes(@Path("json") String path);


}

class Constants {

 static final String BAKING_API = "https://d17h27t6h515a5.cloudfront.net/topher/2017/May/59121517_baking/baking.json";
 static final String BASE_URL = "https://d17h27t6h515a5.cloudfront.net/";
 static final String JSON_PATH = "baking.json";

}

Maybe update Recycler-Adapter can work. I also modified your condition.

call.enqueue(new Callback<List<CakesItem>>() {
    @Override
    public void onResponse(Call<List<CakesItem>> call, Response<List<CakesItem>> response) {
        if (response.isSuccessful()) {
          mCakeList = new ArrayList();
          mCakeList.addAll(response.body());
          mCakeAdapter.notifyDataSetChanged();
        }

    }

    @Override
    public void onFailure(Call<List<CakesItem>> call, Throwable t) {

        Toast.makeText(MainActivity.this, "Unable to load data" + t.getMessage(), Toast.LENGTH_SHORT).show();
    }
});

Try change this condition :

if (!response.isSuccessful()) {

To something like t:

if(response.isSuccessful()){
                Modelcake respuesta = response.body();
                listcake.addAll(respuesta.getcake()); //in getcake() you get what are you want in your model
                adapter.notifyDataSetChanged();

            }else{
                Log.e("API","onResponse"+response.errorBody());
            }

With that should be work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM