[英]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. 我是第一次使用改造,我想解析一些json数据,但是在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 ).
这是一个在每个项目上都带有OnclickListener的Gridlayout,我只想返回2个值(name和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 "
该对象当前具有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";
}
Maybe update Recycler-Adapter can work. 也许更新Recycler-Adapter可以工作。 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()) {
如果(!response.isSuccessful()){
To something like t: 对于像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. 这样就可以了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.