标签:example content tle tput override rom tag summary save
package com.example.bwie.weekone;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import com.example.bwie.weekone.Bean.beans;
import com.google.gson.Gson;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
ListView lv;
MyAdapter adapter;
beans be;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv= (ListView) findViewById(R.id.lv);
getData();
adapter=new MyAdapter();
lv.setAdapter(adapter);
}
class MyAdapter extends BaseAdapter{
@Override
public int getCount() {
if(be!=null){
return be.getData().size();
}
return 0;
}
@Override
public Object getItem(int i) {
return null;
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public View getView(final int i, View view, ViewGroup viewGroup) {
final ViewHolder holder;
if(view==null){
view=View.inflate(MainActivity.this,R.layout.list_item,null);
holder=new ViewHolder();
holder.im1=(ImageView)view.findViewById(R.id.im1);
holder.tx1=(TextView) view.findViewById(R.id.tx1);
holder.tx2=(TextView) view.findViewById(R.id.tx2);
view.setTag(holder);
}else{
holder=(ViewHolder) view.getTag();
}
holder.tx1.setText(be.getData().get(i).getNews_title());
holder.tx2.setText(be.getData().get(i).getNews_summary());
new Thread(new Runnable() {
@Override
public void run() {
final Bitmap img=getImg(be.getData().get(i).getPic_url());
runOnUiThread(new Runnable() {
@Override
public void run() {
holder.im1.setImageBitmap(img);
}
});
}
}).start();
return view;
}
}
class ViewHolder{
TextView tx1,tx2;
ImageView im1;
}
protected Bitmap getImg(String path){
Bitmap bit=null;
try {
URL url=new URL(path);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
int code=conn.getResponseCode();
if(code==200){
InputStream is=conn.getInputStream();
bit= BitmapFactory.decodeStream(is);
}
} catch (Exception e) {
e.printStackTrace();
}
return bit;
}
private void getData() {
new AsyncTask<String,Integer,String>(){
@Override
protected String doInBackground(String... strings) {
String result=geteConn();
return result;
}
@Override
protected void onPostExecute(String result) {
Gson gson=new Gson();
be=gson.fromJson(result,beans.class);
adapter.notifyDataSetChanged();
super.onPostExecute(result);
}
}.execute();
}
//获取网络数据
public String geteConn() {
String result="";
try {
String path="http://api.expoon.com/AppNews/getNewsList/type/1/p/1";
URL url=new URL(path);
HttpURLConnection conn= (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
conn.setConnectTimeout(5000);
conn.setReadTimeout(5000);
int code=conn.getResponseCode();
if(code==200){
InputStream is=conn.getInputStream();
ByteArrayOutputStream bos=new ByteArrayOutputStream();
int lent=-1;
byte[] buff=new byte[1024];
while((lent=is.read(buff))!=-1){
bos.write(buff,0,lent);
}
result=new String(bos.toByteArray());
}
} catch (Exception e) {
e.printStackTrace();
}
return result;
}
}
标签:example content tle tput override rom tag summary save
原文地址:http://www.cnblogs.com/yun1128/p/7471020.html