//步骤,注册清单里存入网络请求,已经ImageLoder注册 创建MyApp
package com.example.cookbook;
import android.app.Application;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;
/**
* Created by 刘阳洲 on 2017/12/2.
*/
public class MyApp extends Application {
@Override
public void onCreate() {
super.onCreate();
ImageLoaderConfiguration configuration = ImageLoaderConfiguration.createDefault(this);
ImageLoader.getInstance().init(configuration);
}
}
package com.example.cookbook;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import android.widget.Toast;
import com.google.gson.Gson;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private String path = "http://apis.juhe.cn/cook/query?key=7ec6a52ef771701be99da0f65c624310&menu=%E8%A5%BF%E7%BA%A2%E6%9F%BF&rn=10&pn=3";
private ListView lv;
private String data = "";
private List<SuperClass.ResultBean.DataBean> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
new AsyncTask<String, Integer, String>() {
@Override
protected String doInBackground(String... params) {
try {
URL url = new URL(path);
HttpURLConnection urlconn = (HttpURLConnection) url.openConnection();
urlconn.setConnectTimeout(5000);
urlconn.setReadTimeout(5000);
int responseCode = urlconn.getResponseCode();
if (responseCode==200){
InputStream is = urlconn.getInputStream();
int len = 0;
byte[] b = new byte[1024];
while((len = is.read(b))!=-1){
String s = new String(b,0,len);
data +=s;
}
}
} catch (Exception e) {
e.printStackTrace();
}
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Gson gson = new Gson();
SuperClass superClass = gson.fromJson(s,SuperClass.class);
list = superClass.getResult().getData();
// Toast.makeText(MainActivity.this,,Toast.LENGTH_SHORT).show();
lv.setAdapter(new MyAdapter(list,MainActivity.this));
}
}.execute(path);
}
private void initView() {
lv = (ListView) findViewById(R.id.lv);
}
}
package com.example.cookbook;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import com.google.gson.Gson;
import com.nostra13.universalimageloader.core.ImageLoader;
import java.util.List;
/**
* Created by 刘阳洲 on 2017/12/2.
*/
public class MyAdapter extends BaseAdapter{
List<SuperClass.ResultBean.DataBean> list ;
Context context;
private ViewHolder holder;
public MyAdapter(List<SuperClass.ResultBean.DataBean> list, Context context) {
this.list = list;
this.context = context;
}
@Override
public int getCount() {
return list.size();
}
@Override
public Object getItem(int position) {
return list.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView==null){
convertView= View.inflate(context,R.layout.item,null);
holder = new ViewHolder();
holder.set_text = convertView.findViewById(R.id.set_text);
holder.img = convertView.findViewById(R.id.img);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
SuperClass.ResultBean.DataBean g= list.get(0);
holder.set_text.setText(list.get(position).getTitle());
ImageLoader.getInstance().displayImage(g.getSteps().get(position).getImg(),holder.img);
return convertView;
}
class ViewHolder {
ImageView img;
TextView set_text;
}
}