1、仿网易新闻客户端

<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:orientation="vertical"

tools:context=".MainActivity">

<TextView

android:layout_width="fill_parent"

android:layout_height="54dip"

android:background="#ff0000"

android:gravity="center_vertical"

android:text="网易新闻"

android:textColor="#ffffff"

android:textSize="24sp"/>

<FrameLayout

android:layout_width="fill_parent"

android:layout_height="fill_parent">

<LinearLayout

android:id="@+id/ll_loading"

android:visibility="invisible"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:orientation="vertical">

<ProgressBar

android:layout_width="wrap_content"

android:layout_height="wrap_content"/>

<TextView

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="正在加载数据..."/>

</LinearLayout>

<ListView

android:id="@+id/lv_news"

android:layout_width="fill_parent"

android:layout_height="fill_parent">

</ListView>

</FrameLayout>

</LinearLayout>

<?xmlversion="1.0"encoding="utf-8"?>

<RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"

android:layout_width="match_parent"

android:layout_height="85dip">

<com.loopj.android.image.SmartImageView

android:id="@+id/iv_icon"

android:layout_width="72dip"

android:layout_height="53dp"

android:layout_marginLeft="5dip"

android:layout_marginTop="15dip"

android:src="@drawable/a"/>

<TextView

android:singleLine="true"

android:id="@+id/tv_title"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_marginLeft="8dip"

android:layout_marginTop="10dip"

android:layout_toRightOf="@id/iv_icon"

android:text="我是标题,阿打发打发的发生打发打发打发撒发生大法"

android:textColor="#000000"

android:textSize="18sp"/>

<TextView

android:id="@+id/tv_description"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@id/tv_title"

android:layout_marginLeft="8dip"

android:layout_marginTop="1dip"

android:layout_toRightOf="@id/iv_icon"

android:lines="2"

android:text="我是描述哈哈哈哈哈哈哈,嘎嘎嘎嘎嘎嘎,大开发商电缆附件啊赛罗克就分啦kdj阿飞爱的发放时大法师打法大发生地"

android:textColor="#AA000000"

android:textSize="12sp"/>

<TextView

android:id="@+id/tv_type"

android:layout_below="@id/tv_description"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="评论:333个"

android:textColor="#000000"

android:layout_alignParentRight="true"

android:layout_alignParentBottom="true"

/>

</RelativeLayout>

packagecom.itheima.news;

importjava.io.InputStream;

importjava.net.HttpURLConnection;

importjava.net.URL;

importjava.util.List;

importandroid.app.Activity;

importandroid.graphics.Color;

importandroid.os.Bundle;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.view.View;

importandroid.view.ViewGroup;

importandroid.view.Window;

importandroid.widget.BaseAdapter;

importandroid.widget.LinearLayout;

importandroid.widget.ListView;

importandroid.widget.TextView;

importandroid.widget.Toast;

importcom.itheima.news.domain.NewsInfo;

importcom.itheima.news.service.NewsInfoParser;

importcom.loopj.android.image.SmartImageView;

publicclassMainActivityextendsActivity{

protectedstaticfinalintLOAD_ERROR=1;

protectedstaticfinalintSET_ADAPTER=2;

privateListViewlv_news;

privateList<NewsInfo>newsInfos;

privateLinearLayoutll_loading;

privateHandlerhandler=newHandler(){

publicvoidhandleMessage(android.os.Messagemsg){

//加载数据后设置界面不可见

ll_loading.setVisibility(View.INVISIBLE);

switch(msg.what){

caseLOAD_ERROR:

Toast.makeText(getApplicationContext(),"加载数据失败",0).show();

break;

caseSET_ADAPTER:

lv_news.setAdapter(newNewsAdapter());

break;

}

};

};

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

requestWindowFeature(Window.FEATURE_NO_TITLE);

setContentView(R.layout.activity_main);

lv_news=(ListView)findViewById(R.id.lv_news);

ll_loading=(LinearLayout)findViewById(R.id.ll_loading);

getData();

//lv_news.setAdapter(adapter);

}

/**

*连接服务器获取服务器上新闻数据

*/

privatevoidgetData(){

//加载数据前设置界面可见

ll_loading.setVisibility(View.VISIBLE);

newThread(){

publicvoidrun(){

try{

URLurl=newURL(getString(R.string.serverurl));

HttpURLConnectionconn=(HttpURLConnection)url.openConnection();

conn.setRequestMethod("GET");

conn.setConnectTimeout(5000);

intcode=conn.getResponseCode();

if(code==200){

//xml文件的流

InputStreamis=conn.getInputStream();

//模拟一个很慢的网络..睡眠一段时间

Thread.sleep(3000);

newsInfos=NewsInfoParser.getNewsInfos(is);

//设置数据适配器了.

Messagemsg=newMessage();

msg.what=SET_ADAPTER;

handler.sendMessage(msg);

}else{

Messagemsg=newMessage();

msg.what=LOAD_ERROR;

handler.sendMessage(msg);

}

}catch(Exceptione){

e.printStackTrace();

Messagemsg=newMessage();

msg.what=LOAD_ERROR;

handler.sendMessage(msg);

}

};

}.start();

}

privateclassNewsAdapterextendsBaseAdapter{

@Override

publicintgetCount(){

returnnewsInfos.size();

}

@Override

publicObjectgetItem(intposition){

returnnull;

}

@Override

publiclonggetItemId(intposition){

return0;

}

@Override

publicViewgetView(intposition,ViewconvertView,ViewGroupparent){

Viewview=View.inflate(getApplicationContext(),R.layout.list_item,null);

TextViewtv_title=(TextView)view.findViewById(R.id.tv_title);

TextViewtv_description=(TextView)view.findViewById(R.id.tv_description);

TextViewtv_type=(TextView)view.findViewById(R.id.tv_type);

NewsInfoinfo=newsInfos.get(position);

tv_title.setText(info.getTitle());

tv_description.setText(info.getDescription());

SmartImageViewsiv=(SmartImageView)view.findViewById(R.id.iv_icon);

siv.setImageUrl(info.getImage(),R.drawable.ic_launcher,R.drawable.ic_launcher);

inttype=info.getType();

switch(type){

case1://一般的新闻有评论个数

tv_type.setText("评论:"+info.getComment());

break;

case2:

tv_type.setText("专题");

tv_type.setBackgroundColor(Color.RED);

break;

case3:

tv_type.setText("LIVE");

tv_type.setBackgroundColor(Color.BLUE);

break;

}

returnview;

}

}

}

packagecom.itheima.news.domain;

publicclassNewsInfo{

privateStringtitle;

privateStringdescription;

privateStringimage;

privateinttype;

privateintcomment;

publicStringgetTitle(){

returntitle;

}

publicvoidsetTitle(Stringtitle){

this.title=title;

}

publicStringgetDescription(){

returndescription;

}

publicvoidsetDescription(Stringdescription){

this.description=description;

}

publicStringgetImage(){

returnimage;

}

publicvoidsetImage(Stringimage){

this.image=image;

}

publicintgetType(){

returntype;

}

publicvoidsetType(inttype){

this.type=type;

}

publicintgetComment(){

returncomment;

}

publicvoidsetComment(intcomment){

this.comment=comment;

}

@Override

publicStringtoString(){

return"NewsInfo[title="+title+",description="+description

+",image="+image+",type="+type+",comment="

+comment+"]";

}

}

packagecom.itheima.news.service;

importjava.io.InputStream;

importjava.util.ArrayList;

importjava.util.List;

importorg.xmlpull.v1.XmlPullParser;

importorg.xmlpull.v1.XmlPullParserException;

importandroid.util.Xml;

importcom.itheima.news.domain.NewsInfo;

publicclassNewsInfoParser{

/**

*解析xml文件的流获取全部的新闻信息

*

*@paramis

*@return新闻信息的集合null代表解析失败

*/

publicstaticList<NewsInfo>getNewsInfos(InputStreamis){

try{

List<NewsInfo>newsInfos=null;

NewsInfonewsInfo=null;

XmlPullParserparser=Xml.newPullParser();

parser.setInput(is,"UTF-8");

intenventType=parser.getEventType();

while(enventType!=XmlPullParser.END_DOCUMENT){

switch(enventType){

caseXmlPullParser.START_TAG:

if("channel".equals(parser.getName())){

newsInfos=newArrayList<NewsInfo>();

}elseif("item".equals(parser.getName())){

newsInfo=newNewsInfo();

}elseif("title".equals(parser.getName())){

Stringtitle=parser.nextText();

newsInfo.setTitle(title);

}elseif("description".equals(parser.getName())){

Stringdescription=parser.nextText();

newsInfo.setDescription(description);

}elseif("image".equals(parser.getName())){

Stringimage=parser.nextText();

newsInfo.setImage(image);

}elseif("type".equals(parser.getName())){

Stringtype=parser.nextText();

newsInfo.setType(Integer.parseInt(type));

}elseif("comment".equals(parser.getName())){

Stringcomment=parser.nextText();

newsInfo.setComment(Integer.parseInt(comment));

}

break;

caseXmlPullParser.END_TAG:

if("item".equals(parser.getName())){

newsInfos.add(newsInfo);

newsInfo=null;

}

break;

}

enventType=parser.next();//继续解析下一个节点

}

returnnewsInfos;

}catch(Exceptione){

e.printStackTrace();

returnnull;

}

}

}

2、文件的上传

packagecom.itheima.upload;

importjava.io.File;

importjava.io.FileNotFoundException;

importcom.loopj.android.http.AsyncHttpClient;

importcom.loopj.android.http.AsyncHttpResponseHandler;

importcom.loopj.android.http.RequestParams;

importandroid.app.Activity;

importandroid.os.Bundle;

importandroid.text.TextUtils;

importandroid.view.View;

importandroid.widget.EditText;

importandroid.widget.Toast;

publicclassMainActivityextendsActivity{

privateEditTextet_path;

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

et_path=(EditText)findViewById(R.id.et_path);

}

publicvoidupload(Viewview){

Stringpath=et_path.getText().toString().trim();

if(TextUtils.isEmpty(path)){

Toast.makeText(this,"路径不能为空",1).show();

return;

}

Filefile=newFile(path);

if(file.exists()&&file.length()>0){

AsyncHttpClientclient=newAsyncHttpClient();

//指定上传一个文件

RequestParamsparams=newRequestParams();

try{

params.put("filename",file);

}catch(FileNotFoundExceptione){

e.printStackTrace();

}//UploadaFile

client.post("http://192.168.1.100:8080/web/UploadFileServlet",

params,newAsyncHttpResponseHandler(){

@Override

publicvoidonSuccess(Stringcontent){

super.onSuccess(content);

Toast.makeText(getApplicationContext(),"上传成功",0)

.show();

}

@Override

publicvoidonFailure(Throwableerror,Stringcontent){

super.onFailure(error,content);

Toast.makeText(getApplicationContext(),"上传失败",0)

.show();

}

});

}else{

Toast.makeText(this,"文件不存在,或者大小为0",1).show();

return;

}

}

}

3、SmartView工作原理

packagecom.itheima.smartimageview;

importandroid.os.Bundle;

importandroid.app.Activity;

importandroid.view.Menu;

publicclassMainActivityextendsActivity{

@Override

protectedvoidonCreate(BundlesavedInstanceState){

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

SmartImageViewsiv=(SmartImageView)findViewById(R.id.siv);

siv.setImageUrl("http://192.168.1.100:8089/tomcat.png",R.drawable.ic_launcher);

}

}

packagecom.itheima.smartimageview;

importjava.net.HttpURLConnection;

importjava.net.MalformedURLException;

importjava.net.URL;

importandroid.graphics.Bitmap;

importandroid.graphics.BitmapFactory;

publicclassImageUtils{

/**

*获取一个路径的bitmap

*

*@paramiconpath

*@return图片获取失败返回null

*/

publicstaticBitmapgetUrlBitmap(Stringiconpath){

try{

URLurl=newURL(iconpath);

HttpURLConnectionconn=(HttpURLConnection)url.openConnection();

conn.setRequestMethod("GET");

conn.setConnectTimeout(5000);

intcode=conn.getResponseCode();

if(code==200){

returnBitmapFactory.decodeStream(conn.getInputStream());

}else{

returnnull;

}

}catch(Exceptione){

e.printStackTrace();

returnnull;

}

}

}

packagecom.itheima.smartimageview;

importandroid.content.Context;

importandroid.graphics.Bitmap;

importandroid.os.Handler;

importandroid.os.Message;

importandroid.util.AttributeSet;

importandroid.widget.ImageView;

publicclassSmartImageViewextendsImageView{

protectedstaticfinalintLOAD_SUCCESS=1;

protectedstaticfinalintLOAD_ERROR=2;

privateHandlerhandler=newHandler(){

publicvoidhandleMessage(android.os.Messagemsg){

switch(msg.what){

caseLOAD_SUCCESS:

Bitmapbitmap=(Bitmap)msg.obj;

setImageBitmap(bitmap);

break;

caseLOAD_ERROR:

intresid=(Integer)msg.obj;

setImageResource(resid);

break;

}

};

};

publicSmartImageView(Contextcontext,AttributeSetattrs,intdefStyle){

super(context,attrs,defStyle);

}

publicSmartImageView(Contextcontext,AttributeSetattrs){

super(context,attrs);

}

publicSmartImageView(Contextcontext){

super(context);

}

/**

*设置一个要加载的图片的url路径

*@paramurl

*/

publicvoidsetImageUrl(finalStringurl){

newThread(){

publicvoidrun(){

Bitmapbitmap=ImageUtils.getUrlBitmap(url);

if(bitmap!=null){

//获取图片成功了.

Messagemsg=newMessage();

msg.obj=bitmap;

msg.what=LOAD_SUCCESS;

handler.sendMessage(msg);

}

};

}.start();

}

/**

*设置一个要加载的图片的url路径

*@paramurl

*/

publicvoidsetImageUrl(finalStringurl,finalintfallbackres){

newThread(){

publicvoidrun(){

Bitmapbitmap=ImageUtils.getUrlBitmap(url);

if(bitmap!=null){

//获取图片成功了.

Messagemsg=newMessage();

msg.obj=bitmap;

msg.what=LOAD_SUCCESS;

handler.sendMessage(msg);

}else{

Messagemsg=newMessage();

msg.obj=fallbackres;

msg.what=LOAD_ERROR;

handler.sendMessage(msg);

}

};

}.start();

}

}

更多相关文章

  1. android intent.setType指定浏览本地多种类型的文件
  2. 修改文件夹权限
  3. Android——文件操作
  4. Android下使用Properties文件保存程序设置
  5. android解析xml文件 Android DOM解析XML之全球实时地震信息列表
  6. android 删除文件,打开指定的文件类型
  7. 在android里保存数据的三种形式(Saving Data)
  8. Android Studio2.0引入so文件(亲测)
  9. 引用自定义资源需注意数据类型

随机推荐

  1. Android屏蔽ListView的Item点击事件
  2. android 焦点事件
  3. Mac编译Android 6.0源码
  4. Android:EditText 软键盘退出监听解决方法
  5. Double click 转成power之后的上报
  6. [转]Android开发者指南-Manifest.xml-act
  7. android中用xml文件设置button的各个状态
  8. Android中下拉通知栏,Activity会走哪些生
  9. Android G726语音编解码库+除燥音算法
  10. android之获取手机安装包里面的信息、获