1、运行效果图
2、实现过程
(1)、布局文件:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns: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"
tools:context=".MainActivity"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="@+id/loading"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:visibility="invisible">
<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="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
ListView的布局:news_item.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="65dp">
<com.loopj.android.image.SmartImageView
android:id="@+id/siv_icon"
android:layout_width="80dp"
android:layout_height="60dp"
android:scaleType="centerCrop"
android:src="@mipmap/ic_launcher"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"></com.loopj.android.image.SmartImageView>
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="@id/siv_icon"
android:ellipsize="end"
android:maxLength="20"
android:singleLine="true"
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="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="@id/siv_icon"
android:ellipsize="end"
android:maxLength="16"
android:maxLines="1"
android:text="我是描述"
android:textColor="#99000000"
android:textSize="14sp" />
<TextView
android:id="@+id/tv_type"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="10dp"
android:text="评论"
android:textColor="#99000000"
android:textSize="12sp" />
</RelativeLayout>
(2)、用户界面交互代码:
MainActivity.xml
package com.edu.bzu.newsclient;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.Toast;
import com.edu.bzu.newsclient.adapter.NewsAdapter;
import com.edu.bzu.newsclient.entity.NewsInfo;
import com.edu.bzu.newsclient.tools.JsonParse;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView lvNews;
private LinearLayout loading;
private List<NewsInfo> newsinfos;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvNews = (ListView) findViewById(R.id.lv_news);
loading = (LinearLayout) findViewById(R.id.loading);
fillData();
}
private void fillData() {
AsyncHttpClient client = new AsyncHttpClient();
client.get("http://10.51.24.149:8080/NewsInfo.json", new AsyncHttpResponseHandler() {
@Override
public void onSuccess(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes) {
try {
String json = new String(bytes, "utf-8");
newsinfos = JsonParse.getNewsInfo(json);
if (newsinfos == null) {
Toast.makeText(MainActivity.this, "解析失败", Toast.LENGTH_LONG).show();
} else {
loading.setVisibility(View.INVISIBLE);
lvNews.setAdapter(new NewsAdapter(MainActivity.this, newsinfos));
}
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(int i, cz.msebera.android.httpclient.Header[] headers, byte[] bytes, Throwable throwable) {
Toast.makeText(MainActivity.this, "失败", Toast.LENGTH_LONG).show();
}
});
}
}
(3)、实体类
NewsInfo.java
package com.edu.bzu.newsclient.entity;
public class NewsInfo {
private String icon;//图片路径
private String title;//新闻标题
private String content;//新闻描述
private int type;//新闻类型
private long comment;//新闻评论数
public String getIcon() {
return icon;
}
public void setIcon(String icon) {
this.icon = icon;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public int getType() {
return type;
}
public void setType(int type) {
this.type = type;
}
public long getComment() {
return comment;
}
public void setComment(long comment) {
this.comment = comment;
}
}
(4)、工具类,用于解析gson
package com.edu.bzu.newsclient.tools;
import com.edu.bzu.newsclient.entity.NewsInfo;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.lang.reflect.Type;
import java.util.List;
public class JsonParse {
public static List<NewsInfo> getNewsInfo(String json) {
Gson gson=new Gson();
Type listType=new TypeToken<List<NewsInfo>>(){
}.getType();
List<NewsInfo> newsinfos=gson.fromJson(json,listType);
return newsinfos;
}
}
(5)、适配器:
NewsAdapter.java
package com.edu.bzu.newsclient.adapter;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import com.edu.bzu.newsclient.R;
import com.edu.bzu.newsclient.entity.NewsInfo;
import com.loopj.android.image.SmartImageView;
import java.util.List;
public class NewsAdapter extends ArrayAdapter<NewsInfo> {
public NewsAdapter(Context context, List<NewsInfo> objects) {
super(context, R.layout.news_item, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
NewsInfo newsInfo = getItem(position);
ViewHoler viewHoler;
View view = null;
if (convertView == null) {
view = View.inflate(getContext(), R.layout.news_item, null);
viewHoler = new ViewHoler();
viewHoler.siv_icon = (SmartImageView) view.findViewById(R.id.siv_icon);
viewHoler.tv_title = (TextView) view.findViewById(R.id.tv_title);
viewHoler.tv_description = (TextView) view.findViewById(R.id.tv_description);
viewHoler.tv_type = (TextView) view.findViewById(R.id.tv_type);
view.setTag(viewHoler);
} else {
view = convertView;
viewHoler = (ViewHoler) view.getTag();
}
return view;
}
class ViewHoler {
SmartImageView siv_icon;
TextView tv_title;
TextView tv_description;
TextView tv_type;
}
}
(6)、开启Tomcat服务器,在Tomcat服务器根目录下找到bin文件夹,找到 startup.bat,点击运行开启服务器。将NewsInfo.xml文件放到ROOT文件夹下。并创建images文件夹,放置所需图片。(记得名字正确)
在线json编辑器:http://www.qqe2.com/json
[
{
"icon": "http://10.51.24.32:8080/images/a.jpg",
"title": "科技温暖世界",
"content": "进入一个更有爱的世界",
"type": "1",
"comment": "69"
},
{
"icon": "http://10.51.24.32:8080/images/b.jpg",
"title": "科技温暖世界2",
"content": "进入一个更有爱的世界2",
"type": "2",
"comment": "69"
},
{
"icon": "http://10.51.24.32:8080/images/c.jpg",
"title": "科技温暖世界3",
"content": "进入一个更有爱的世界3",
"type": "3",
"comment": "69"
}
]
(7)、添加权限,访问网络需要在AndroidMainfest.xml里配置
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.edu.bzu.newsclient">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<!--需要配置的相应的权限-->
<uses-permission android:name="android.permission.INTERNET"></uses-permission>
<!-- -->
</manifest>
3、注意的问题
(1)、运行前需要打开服务器,否则报错。
(2)、json文件有错,显示 Java.lang.IllegalStateException: Expected BEGIN_ARRAY but was BEGIN_OBJEC
(3)、将AsyncHttpClient和SmartImageView的所学的jar包放入libs中,并 add as library。
(4)、点击File->Project Structure->Dependencies->点击 右侧加号->Library dependency->搜索 com.google.code.gson->选第二项。
最后,通过这个案例,会使我们学到AsyncHttpClient和SmartImageView开源框架的使用。