注册 登录  
 加关注
   显示下一条  |  关闭
温馨提示!由于新浪微博认证机制调整,您的新浪微博帐号绑定已过期,请重新绑定!立即重新绑定新浪微博》  |  关闭

告别迷茫

梦想与现实的差距,就是我们生活的意义。因为我们有差距,我们才会一直积累,在努力。

 
 
 

日志

 
 

简单的网络连接ListView的新闻客户端  

2015-05-02 16:42:23|  分类: 疯狂Android 讲义 |  标签: |举报 |字号 订阅

  下载LOFTER 我的照片书  |
1.服务器端从数据中获取数据,以JSON的方式返回给客户端
2.客户端杜宇JSON数据的解析
3.编写网络连接获取数据的工具类
4.Listview的适配器中填充数据
5.编写类似于JavaBean的数据操作对象,进行每一项数据的扩充。
6.目录的机构
简单的网络连接ListView的新闻客户端 - 983433479 - 告别迷茫

1.网络获取的工具类

public class HttpUtils {

public static void getNewsJSON(final String url, final Handler handler){

new Thread(new Runnable() {
@Override
public void run() {
HttpURLConnection conn;
InputStream is;
try {
conn = (HttpURLConnection) new URL(url).openConnection();
conn.setRequestMethod("GET");
is = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(is));
String line = "";
StringBuilder result = new StringBuilder();
while ( (line = reader.readLine()) != null ){
result.append(line);
}
Message msg = new Message();
msg.obj = result.toString();
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}

public static void setPicBitmap(final ImageView ivPic, final String pic_url){
new Thread(new Runnable() {
@Override
public void run() {
try {
HttpURLConnection conn = (HttpURLConnection) new URL(pic_url).openConnection();
conn.connect();
InputStream is = conn.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(is);
ivPic.setImageBitmap(bitmap);
is.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}).start();
}

}


2.listview的adapter。xml文件

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<ImageView
android:id="@+id/ivPic"
android:layout_width="42dp"
android:layout_height="42dp"
android:src="@drawable/ic_launcher" />

<TextView
android:id="@+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/ivPic"
android:text="title"
android:textSize="18sp" />

<TextView
android:id="@+id/tvDesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvTitle"
android:layout_toRightOf="@id/ivPic"
android:text="desc" />

<TextView
android:id="@+id/tvTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="time"
android:textSize="10sp" />

</RelativeLayout>

简单的网络连接ListView的新闻客户端 - 983433479 - 告别迷茫

 

public class NewsAdapter extends BaseAdapter {

private Context context;
private List<News> newsList;
构造函数传剃数据,Context对象
public NewsAdapter(Context context, List<News> newsList){
this.context = context;
this.newsList = newsList;
}

@Override
public int getCount() {
return newsList.size();
}

@Override
public News getItem(int position) {
return newsList.get(position);
}

@Override

public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.news_item, null);
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
TextView tvDesc = (TextView) convertView.findViewById(R.id.tvDesc);
TextView tvTime = (TextView) convertView.findViewById(R.id.tvTime);
ImageView ivPic = (ImageView) convertView.findViewById(R.id.ivPic);

News news = newsList.get(position);
tvTitle.setText(news.getTitle());
tvDesc.setText(news.getDesc());
tvTime.setText(news.getTime());

String pic_url = news.getPic_url();
HttpUtils.setPicBitmap(ivPic, pic_url);
图片的获取,也是利用的而网络的工具
return convertView;
}



}


javabean 方便操作数据

public class News {

private String title;
private String desc;
private String time;
private String content_url;
private String pic_url;

public News(String title, String desc, String time, String content_url, String pic_url){
setTitle(title);
setDesc(desc);
setTime(time);
setContent_url(content_url);
setPic_url(pic_url);
}

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDesc() {
return desc;
}
public void setDesc(String desc) {
this.desc = desc;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public String getContent_url() {
return content_url;
}
public void setContent_url(String content_url) {
this.content_url = content_url;
}
public String getPic_url() {
return pic_url;
}
public void setPic_url(String pic_url) {
this.pic_url = pic_url;
}

}




MaiActivity操作所有

public class MainActivity extends Activity implements OnItemClickListener{

private ListView lvNews;
private NewsAdapter adapter;
private List<News> newsList;

/*很多所有的数据的获取,这样的方式,在解析的Json的过程中操作特别的容易*/


public static final String GET_NEWS_URL = "http://......../NewsDemo/getNewsJSON.php";

private Handler getNewsHandler = new Handler(){
public void handleMessage(android.os.Message msg) {
String jsonData = (String) msg.obj;
System.out.println(jsonData);
try {
JSONArray jsonArray = new JSONArray(jsonData);
for (int i=0;i<jsonArray.length();i++){
JSONObject object = jsonArray.getJSONObject(i);
String title = object.getString("title");
String desc = object.getString("desc");
String time = object.getString("time");
String content_url = object.getString("content_url");
String pic_url = object.getString("pic_url");
newsList.add(new News(title, desc, time, content_url, pic_url));
}
adapter.notifyDataSetChanged();
/**
* Notifies the attached observers that the underlying data has
* been changed and any View reflecting
* the data set should refresh itself.
*/

} catch (Exception e) {
e.printStackTrace();
}
};
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvNews = (ListView) findViewById(R.id.lvNews);
newsList = new ArrayList<News>();
adapter = new NewsAdapter(this, newsList);
lvNews.setAdapter(adapter);
lvNews.setOnItemClickListener(this);
HttpUtils.getNewsJSON(GET_NEWS_URL, getNewsHandler);
}

/**
* 设置我们的adapter的点击处理,直接继承
*/
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
News news = newsList.get(position);
Intent intent = new Intent(this, BrowseNewsActivity.class);
intent.putExtra("content_url", news.getContent_url());

这里是个webview的界面

startActivity(intent);
}

}


跳转的界面

public class BrowseNewsActivity extends Activity {

private WebView webView;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.acitivy_browse_news);

webView = (WebView) findViewById(R.id.webView);
String pic_url = getIntent().getStringExtra("content_url");
webView.loadUrl(pic_url);
webView.getSettings().setJavaScriptEnabled(true);
webView.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);

/缓存/

}
}



 
  评论这张
 
阅读(2)| 评论(0)
推荐 转载

历史上的今天

在LOFTER的更多文章

评论

<#--最新日志,群博日志--> <#--推荐日志--> <#--引用记录--> <#--博主推荐--> <#--随机阅读--> <#--首页推荐--> <#--历史上的今天--> <#--被推荐日志--> <#--上一篇,下一篇--> <#-- 热度 --> <#-- 网易新闻广告 --> <#--右边模块结构--> <#--评论模块结构--> <#--引用模块结构--> <#--博主发起的投票-->
 
 
 
 
 
 
 
 
 
 
 
 
 
 

页脚

网易公司版权所有 ©1997-2017