繁体   English   中英

Android Json解析与改造

[英]Android Json parsing with retrofit

我是第一次使用改造,我想解析一些json数据,但是在MainActivity上启动网络请求时可能出错。 该应用程序不会崩溃,但不会返回任何值。 这是一个在每个项目上都带有OnclickListener的Gridlayout,我只想返回2个值(name和Id)。 该对象当前具有3个项(名称,ID和一个List <>),这是Full API端点“ 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";

}

也许更新Recycler-Adapter可以工作。 我还修改了您的条件。

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

尝试更改此条件:

如果(!response.isSuccessful()){

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

这样就可以了。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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