1. 程式人生 > >okhttp 請求list數據實例

okhttp 請求list數據實例

geb error: catch style base ray 實例 bitmap cte

public class DataBean {


    /**
     * id : 61684
     * movieName : 《猜火車2》先導預告片
     * coverImg : http://img31.mtime.cn/mg/2016/07/26/143142.64770465.jpg
     * movieId : 228230
     * url : http://vfx.mtime.cn/Video/2016/07/26/mp4/160726074707321432_480.mp4
     * hightUrl : http://vfx.mtime.cn/Video/2016/07/26/mp4/160726074707321432.mp4
* videoTitle : 猜火車2 先導預告片 * videoLength : 46 * rating : 0 * type : ["劇情"] * summary : 蘇格蘭四兄弟回來了! */ private List<ItemData> trailers; public void setTrailers(List<ItemData> trailers) { this.trailers = trailers; } public List<ItemData> getTrailers() {
return trailers; } public static class ItemData { private int id; private String movieName; private String coverImg; private int movieId; private String url; private String hightUrl; private String videoTitle; private int videoLength;
private int rating; private String summary; private List<String> type; get set.... } } public class CacheUtils { /** * 保持數據 * @param context * @param key * @param values */ public static void putString(Context context,String key,String values){ SharedPreferences sharedPreferences = context.getSharedPreferences("atguigu",Context.MODE_PRIVATE); sharedPreferences.edit().putString(key,values).commit(); } /** * 得到緩存的數據 * @param context * @param key * @return */ public static String getString(Context context,String key){ SharedPreferences sharedPreferences = context.getSharedPreferences("atguigu",Context.MODE_PRIVATE); return sharedPreferences.getString(key,""); } } public class OKHttpListActivity extends Activity{ private static final String TAG = OKHttpListActivity.class.getSimpleName(); private ListView listView; private ProgressBar progressBar; private TextView tv_nodata; private OKHttpListAdapter adapter; private String url; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initView(); getDataFromNet(); } private void getDataFromNet() { url = "http://api.m.mtime.cn/PageSubArea/TrailerList.api"; //得到緩存的數據 String saveJson = CacheUtils.getString(this,url); if(!TextUtils.isEmpty(saveJson)){ processData(saveJson); } OkHttpUtils .post() .url(url) .id(100) .build() .execute(new MyStringCallback()); } private void initView() { setContentView(R.layout.activity_okhttplist); tv_nodata = (TextView) findViewById(R.id.tv_nodata); listView = (ListView) findViewById(R.id.listview); progressBar = (ProgressBar) findViewById(R.id.progressBar); } public class MyStringCallback extends StringCallback { @Override public void onBefore(Request request, int id) { setTitle("loading..."); } @Override public void onAfter(int id) { setTitle("Sample-okHttp"); } @Override public void onError(Call call, Exception e, int id) { e.printStackTrace(); tv_nodata.setVisibility(View.VISIBLE); } @Override public void onResponse(String response, int id) { Log.e(TAG, "onResponse:complete"); tv_nodata.setVisibility(View.GONE); switch (id) { case 100: Toast.makeText(OKHttpListActivity.this, "http", Toast.LENGTH_SHORT).show(); break; case 101: Toast.makeText(OKHttpListActivity.this, "https", Toast.LENGTH_SHORT).show(); break; } //解析數據和顯示數據 if(response != null){ //緩存數據 CacheUtils.putString(OKHttpListActivity.this,url,response); processData(response); } } @Override public void inProgress(float progress, long total, int id) { Log.e(TAG, "inProgress:" + progress); } } /** * 解析和顯示數據 * @param json */ private void processData(String json) { //解析數據 DataBean dataBean = parsedJson(json); List<DataBean.ItemData> datas = dataBean.getTrailers(); if(datas != null && datas.size() >0){ //有數據 tv_nodata.setVisibility(View.GONE); //顯示適配器 adapter = new OKHttpListAdapter(OKHttpListActivity.this,datas); listView.setAdapter(adapter); }else{ //沒有數據 tv_nodata.setVisibility(View.VISIBLE); } progressBar.setVisibility(View.GONE); } /** * 解析json數據 * * @param response * @return */ private DataBean parsedJson(String response) { DataBean dataBean = new DataBean(); try { JSONObject jsonObject = new JSONObject(response); JSONArray jsonArray = jsonObject.optJSONArray("trailers"); if (jsonArray != null && jsonArray.length() > 0) { List<DataBean.ItemData> trailers = new ArrayList<>(); dataBean.setTrailers(trailers); for (int i = 0; i < jsonArray.length(); i++) { JSONObject jsonObjectItem = (JSONObject) jsonArray.get(i); if (jsonObjectItem != null) { DataBean.ItemData mediaItem = new DataBean.ItemData(); String movieName = jsonObjectItem.optString("movieName");//name mediaItem.setMovieName(movieName); String videoTitle = jsonObjectItem.optString("videoTitle");//desc mediaItem.setVideoTitle(videoTitle); String imageUrl = jsonObjectItem.optString("coverImg");//imageUrl mediaItem.setCoverImg(imageUrl); String hightUrl = jsonObjectItem.optString("hightUrl");//data mediaItem.setHightUrl(hightUrl); //把數據添加到集合 trailers.add(mediaItem); } } } } catch (JSONException e) { e.printStackTrace(); } return dataBean; } } public class OKHttpListAdapter extends BaseAdapter { private final Context context; private final List<DataBean.ItemData> datas; public OKHttpListAdapter(Context context,List<DataBean.ItemData> datas){ this.context = context; this.datas = datas; } @Override public int getCount() { return datas.size(); } @Override public Object getItem(int position) { return null; } @Override public long getItemId(int position) { return 0; } @Override public View getView(int position, View convertView, ViewGroup parent) { final ViewHolder viewHolder; if(convertView ==null){ convertView = View.inflate(context, R.layout.item_okhttp_list_image,null); viewHolder = new ViewHolder(); viewHolder.iv_icon = (ImageView) convertView.findViewById(R.id.iv_icon); viewHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name); viewHolder.tv_desc = (TextView) convertView.findViewById(R.id.tv_desc); convertView.setTag(viewHolder); }else{ viewHolder = (ViewHolder) convertView.getTag(); } //根據位置得到數據 DataBean.ItemData itemData = datas.get(position); viewHolder.tv_name.setText(itemData.getMovieName()); viewHolder.tv_desc.setText(itemData.getVideoTitle()); //在列表中使用okhttp-utils請求圖片 OkHttpUtils .get()// .url(itemData.getCoverImg())// .tag(this)// .build()// .connTimeOut(20000) .readTimeOut(20000) .writeTimeOut(20000) .execute(new BitmapCallback() { @Override public void onError(Call call, Exception e, int id) { // tv_result.setText("onError:" + e.getMessage()); } @Override public void onResponse(Bitmap bitmap, int id) { Log.e("TAG", "onResponse:complete"); viewHolder.iv_icon.setImageBitmap(bitmap); } }); return convertView; } static class ViewHolder{ ImageView iv_icon; TextView tv_name; TextView tv_desc; } }

okhttp 請求list數據實例