private String mUrl = "http://v.juhe.cn/toutiao/index?type=top&key=097060266650f67b2cebd2a06aded587"; private RecyclerView mRecy; private ListmList = new ArrayList<>();
private Handler mHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
String jsonStr = (String) msg.obj;
Gson gson = new Gson();
MyData myData = gson.fromJson(jsonStr, MyData.class);
mList = myData.getResult().getData();
mAdapter = new MyAdapter(mList, MainActivity.this);
mRecy.setAdapter(mAdapter);
mAdapter.setOnItemListener(new MyAdapter.OnItemClick() {
@Override
public void setOnItem(View v, int position) {
Toast.makeText(MainActivity.this, mList.get(position).getTitle(), Toast.LENGTH_SHORT).show();
}
});
}
};
private MyAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
getInfo();
}
private void initView() {
mRecy = (RecyclerView) findViewById(R.id.Recycler);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(MainActivity.this);
mRecy.setLayoutManager(linearLayoutManager);
}
private void getInfo() {
OkHttpClient okHttpClient = new OkHttpClient();
Request request = new Request.Builder().url(mUrl).build();
okHttpClient.newCall(request).enqueue(new Callback() {
@Override
public void onFailure(Call call, IOException e) {
}
@Override
public void onResponse(Call call, Response response) throws IOException {
Message msg = new Message();
msg.obj = response.body().string();
mHandler.sendMessage(msg);
}
});
}
网友评论