package baway.com.my; import android.os.AsyncTask; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import com.google.gson.Gson; import com.limxing.xlistview.view.XListView; import java.io.InputStream; import java.io.UnsupportedEncodingException; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLEncoder; public class MainActivity extends AppCompatActivity implements XListView.IXListViewListener { private XListView xlv; private int pageIndex = 1; private Mydapter dapter; private boolean isplay=true; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); xlv = (XListView) findViewById(R.id.xlv); xlv.setPullLoadEnable(true); xlv.setRefreshTime("12:23:14"); xlv.setXListViewListener(this); try { getHuoqu("http://apis.juhe.cn/cook/query.php?" + "key=a7a42220c5c1c5268be7ba25af764f6c&menu=" + URLEncoder.encode("秘制红烧肉", "utf-8") + "&pn" + pageIndex + "&rn=" + 10); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } private void getHuoqu(String s) { new AsyncTask<String, Void, String>() { @Override protected void onPostExecute(String s) { super.onPostExecute(s); Gson gson = new Gson(); Bean bean = gson.fromJson(s, Bean.class); if (bean==null){ return; } if (dapter==null){ dapter=new Mydapter(MainActivity.this,bean.getResult().getData()); }else { dapter.addMore(bean.getResult().getData(),isplay); dapter.notifyDataSetChanged(); } xlv.setAdapter(dapter); } @Override protected String doInBackground(String... params) { try { String str = params[0]; URL url = new URL(str); HttpURLConnection connection = (HttpURLConnection) url.openConnection(); connection.setRequestMethod("GET"); connection.setConnectTimeout(5000); connection.setReadTimeout(5000); int code = connection.getResponseCode(); if (code == 200) { InputStream inputStream = connection.getInputStream(); String upec = Utils.Upec(inputStream); System.out.println("haha" + upec); return upec; } } catch (Exception e) { e.printStackTrace(); } return null; } }.execute(s); } @Override public void onRefresh() { try { isplay=true; ++pageIndex; getHuoqu("http://apis.juhe.cn/cook/query.php?" + "key=a7a42220c5c1c5268be7ba25af764f6c&menu=" + URLEncoder.encode("秘制红烧肉", "utf-8") + "&pn" + pageIndex + "&rn=" + 10); xlv.stopRefresh(true); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } @Override public void onLoadMore() { try { isplay=false; ++pageIndex; getHuoqu("http://apis.juhe.cn/cook/query.php?" + "key=a7a42220c5c1c5268be7ba25af764f6c&menu=" + URLEncoder.encode("秘制红烧肉", "utf-8") + "&pn" + pageIndex + "&rn=" + 10); xlv.stopLoadMore(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } } }
xListView--MylistViewDemo--1
最新推荐文章于 2018-11-12 10:55:06 发布