湘南学院
课程设计报告
课程名称: 嵌入式系统
设计题目: Android 客户端开发
专业班级: 网络工程1班
学生姓名: 欧阳亚平
学生学号: 201114160114
指导老师:
20##年 月 日
目 录
1概述…………………………………………………………………………1
1.1背景…………………………………………………………………………1
1.2设计要求……………………………………………………………………2
1.3开发环境……………………………………………………………………3
1.4关键技术……………………………………………………………………5
2概要设计………………………………………………………………………8
2.1软件结构……………………………………………………………………8
2.2各功能模块设计……………………………………………………………10
3详细设计与实现………………………………………………………………13
3.1界面说明……………………………………………………………………16
3.2程序流程……………………………………………………………………15
3.3关键代码……………………………………………………………………15
4测试分析……………………………………………………………………18
4.1测试方法……………………………………………………………………18
4.2测试用例……………………………………………………………………19
4.3测试结果……………………………………………………………………19
5小结……………………………………………………………………19
参考文献…………………………………………………………………… 24
附录(备选)……………………………………………………………26
通过API接口服务器返回json数据。再将返回来的json数据时行解析,让其显示在手机上。
随着手机和互联网的使用越来越普遍,通过手机浏览网页越来来方便,但是如果用手机浏览电脑版网页就会使得用户体验很差,因为手机手频目太小。这样就需要将电脑版网页转化成手机版网页。
通过API接口访问服务器返回json数据进行解析,得到需要显示手机上的图片或文字。
eclipse插件ADT;
将如下json数据进行解析:
1. 显示网页主界面
2. 登入显示已购买商品
1. 显示网页主界面:
2. 登入显示已购买商品
利用android各种控件来布局界面:如TextView,Button,ListView等;
先调用api接口:URL url=new URL(urls);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
String result="";
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
byte[] data = newbyte[1024];
int len = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((len = is.read(data)) != -1) {
out.write(data, 0, len);
}
返回json数据,再对此数据进行处理:for (int i = offset; i < totalnumber; ++i) {
JSONObject obj = data.getJSONObject(i);
JSONObject partner = obj.getJSONObject("partner");
Map<String, Object> map = new HashMap<String, Object>();
map.put("contact", partner.get("contact").toString());
map.put("title", obj.get("title").toString());
map.put("image", obj.get("image").toString());
map.put("team_price", obj.get("team_price").toString());
map.put("market_price", obj.get("market_price").toString());
map.put("now_number", obj.get("now_number").toString() + "人");
allvalues.add(map);
}
最后再显示在手机的界面:ListView.setAdapter(new Adapter);
1.
public class MainActivity extends Activity {
private ListView list = null;
private TextView footView = null;
private Handler handler = null;
private ProductListAdapter adapter = null;
private List<Map<String, Object>> allvalues = new ArrayList<Map<String,Object>>();
private Spinner catalog = null;
private Button orderby = null;
private ArrayAdapter<CharSequence> adapterCatalog = null;
private File imagecache;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Groable.init(this);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.list);
catalog = (Spinner) findViewById(R.id.catalog);
orderby = (Button) findViewById(R.id.order_by);
adapterCatalog = ArrayAdapter.createFromResource(this, R.array.catalog,
android.R.layout.simple_spinner_item);
adapterCatalog
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
catalog.setAdapter(adapterCatalog);
catalog.setPrompt("请选择类别");
super.registerForContextMenu(this.orderby);
footView = new TextView(this);
footView.setTextSize(20);
footView.setTextColor(Color.WHITE);
footView.setGravity(Gravity.CENTER);
footView.setText("正在查询,请稍候...");
list.addFooterView(footView);
imagecache = new File("/sdcard");
if (!imagecache.exists()) {
imagecache.mkdirs();
}
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
list.setAdapter(adapter);
} else {
}
}
};
final ProgressDialog pro = new ProgressDialog(MainActivity.this);
pro.setTitle("提示");
pro.setMessage("正在加载数据,请稍后.......");
pro.show();
Thread t = new Thread() {
@Override
public void run() {
try {
loadData();
adapter = new ProductListAdapter(allvalues,
MainActivity.this, imagecache);
} catch (Exception e) {
e.printStackTrace();
}
handler.sendEmptyMessage(0);
pro.dismiss();
}
};
t.start();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
menu.setHeaderTitle("排序");
super.getMenuInflater().inflate(R.menu.order_by, menu);
}
public void loadData() throws Exception {
String results = Groable.getUrlResult(Groable.BASE_URL
+ "Tuan/goodsList");
JSONObject root = new JSONObject(results);
JSONObject result = root.getJSONObject("result");
JSONArray data = result.getJSONArray("data");
System.out.println(data.toString());
for (int i = 0; i < data.length(); ++i) {
JSONObject obj = data.getJSONObject(i);
JSONObject partner = obj.getJSONObject("partner");
Map<String, Object> map = new HashMap<String, Object>();
map.put("contact", partner.get("contact").toString());
map.put("title", obj.get("title").toString());
map.put("image", obj.get("image").toString());
map.put("team_price", obj.get("team_price").toString());
map.put("market_price", obj.get("market_price").toString());
map.put("now_number", obj.get("now_number").toString() + "人");
allvalues.add(map);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
menu.add(menu.NONE, menu.FIRST + 1, 1, "团购").setIcon(
android.R.drawable.ic_menu_edit);
menu.add(menu.NONE, menu.FIRST + 2, 2, "周边").setIcon(
android.R.drawable.ic_menu_view);
menu.add(menu.NONE, menu.FIRST + 3, 3, "团购券").setIcon(
android.R.drawable.ic_menu_save);
menu.add(menu.NONE, menu.FIRST + 4, 4, "团购").setIcon(
android.R.drawable.ic_menu_more);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case Menu.FIRST + 3:
Intent it = new Intent(MainActivity.this, Login.class);
MainActivity.this.startActivity(it);
break;
}
return false;
}
@Override
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
return true;
}
@Override
protected void onDestroy() {
for (File file : imagecache.listFiles()) {
file.delete();
}
super.onDestroy();
}
}
public class ProductListAdapter extends BaseAdapter {
private List<Map<String, Object>> allvalues;
private Context ctx;
private LayoutInflater inflater;
private File imagecache;
public ProductListAdapter(List<Map<String, Object>> allvalues,
Context context, File imagecache) {
this.allvalues = allvalues;
this.ctx = context;
this.imagecache = imagecache;
}
@Override
public int getCount() {
return allvalues.size();
}
@Override
public Object getItem(int position) {
return allvalues.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView contact = null;
TextView title = null;
TextView price = null;
TextView marketprice = null;
TextView now_number = null;
ImageView image = null;
if (convertView == null) {
inflater = (LayoutInflater) ctx
.getSystemService(ctx.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.product_list_line, null);
contact = (TextView) convertView.findViewById(R.id.product_contact);
title = (TextView) convertView.findViewById(R.id.product_title);
price = (TextView) convertView.findViewById(R.id.team_price);
marketprice = (TextView) convertView
.findViewById(R.id.market_price);
now_number = (TextView) convertView.findViewById(R.id.now_number);
image = (ImageView) convertView.findViewById(R.id.productimage);
convertView.setTag(new DataWrap(image, title, price, marketprice,
contact, now_number));
} else {
DataWrap datawrap = (DataWrap) convertView.getTag();
contact = datawrap.contact;
title = datawrap.title;
price = datawrap.price;
now_number = datawrap.now_number;
marketprice = datawrap.marketprice;
image = datawrap.image;
}
Map<String, Object> map = allvalues.get(position);
contact.getLayoutParams().height = Groable.SCREEN_HEIGHT / 20;
title.getLayoutParams().height = Groable.SCREEN_HEIGHT / 10;
price.getLayoutParams().height = Groable.SCREEN_HEIGHT / 20;
image.getLayoutParams().height = Groable.SCREEN_HEIGHT / 5;
title.setText(map.get("title").toString());
price.setText("¥" + map.get("team_price").toString());
marketprice.setText("/" + map.get("market_price").toString());
now_number.setText(map.get("now_number").toString());
contact.setText(map.get("contact").toString());
String imageurl = (String) map.get("image");
DownloadImageUtils.setImageUrl(Groable.UPLOAD_URL + imageurl, image,
imageurl, imagecache);
return convertView;
}
private final class DataWrap {
ImageView image;
TextView title;
TextView price;
TextView marketprice;
TextView contact;
TextView now_number;
public DataWrap(ImageView image, TextView title, TextView price,
TextView marketprice, TextView contact, TextView now_number) {
this.contact = contact;
this.image = image;
this.marketprice = marketprice;
this.now_number = now_number;
this.price = price;
this.title = title;
}
}
}
public class DownloadImageUtils {
public static void setImageUrl(final String url, final ImageView img,
final String imgurl, final File imagecache) {
final Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
Uri uri = (Uri) msg.obj;
if (uri != null && img != null) {
img.setImageURI(uri);
System.out.println();
}
}
};
Thread t = new Thread() {
@Override
public void run() {
Message msg = new Message();
String fileName = new MD5Code().getMD5ofStr(imgurl)
+ imgurl.substring(imgurl.lastIndexOf("."));
File f = new File(imagecache, fileName);
InputStream is = null;
OutputStream os = null;
// OutputStream os = null;
try {
if (f.exists()) {
msg.what = 1;
msg.obj = Uri.fromFile(f);
} else {
is = Groable.getUrlInputStream(url);
os = new FileOutputStream(f);
byte data[] = new byte[1024];
int len = 0;
while ((len = is.read(data)) != -1) {
os.write(data, 0, len);
}
Uri uri= Uri.fromFile(f);
msg.what = 1;
msg.obj =uri;
}
handler.sendMessage(msg);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (is != null && os != null) {
is.close();
os.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
t.start();
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#ff1493"
android:gravity="center"
android:height="40px"
android:text="和兴团"
android:textColor="#fffaf0"
android:textSize="25px" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TableRow>
<Spinner
android:id="@+id/catalog"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="40dp"
/>
<Button
android:id="@+id/order_by"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:padding="2px"
android:text="排序"
android:layout_toRightOf="@+id/catalog"/>
</TableRow>
</TableLayout>
</RelativeLayout>
<ListView
android:id="@+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#00000000"
android:divider="#ff1493"
android:dividerHeight="1dp"
android:scrollbars="none" >
</ListView>
</LinearLayout>
public class Groable {
public static String BASE_URL="**********";
public static String WELCOME_URL=BASE_URL+"?s=Tuan/goodsList";
public static int SCREEN_WIDTH;
public static int SCREEN_HEIGHT;
public static String CACHE_IMAGE_DIR;
public static String UPLOAD_URL="***********";
public static File imgcache=null;
public static void init(Activity a){
SCREEN_HEIGHT=a.getWindowManager().getDefaultDisplay().getHeight();
SCREEN_WIDTH=a.getWindowManager().getDefaultDisplay().getWidth();
System.out.println(Environment.getExternalStorageState()+"yaping");
CACHE_IMAGE_DIR=a.getFilesDir().getPath() + "/imagecache";
File imgcache=null;
imgcache=new File(CACHE_IMAGE_DIR);
if(!imgcache.exists()){
imgcache.mkdirs();
Log.v("111",imgcache.toString());
}
}
public static String getUrlResult(String urls)throws Exception{
URL url=new URL(urls);
HttpURLConnection conn=(HttpURLConnection)url.openConnection();
String result="";
int responseCode = conn.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
InputStream is = conn.getInputStream();
byte[] data = new byte[1024];
int len = 0;
ByteArrayOutputStream out = new ByteArrayOutputStream();
while ((len = is.read(data)) != -1) {
out.write(data, 0, len);
}
is.close();
out.close();
result=out.toString();
}
return result;
}
public static InputStream getUrlInputStream(String url)throws Exception{
URL u=new URL(url);
HttpURLConnection conn=(HttpURLConnection)u.openConnection();
return conn.getInputStream();
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:orientation="horizontal" >
<ImageView
android:id="@+id/productimage"
android:layout_width="135.0dip"
android:layout_height="130.0dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="3.0dip"
android:layout_marginRight="15.0dip" />
<TextView
android:id="@+id/product_contact"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@id/productimage"
android:layout_toRightOf="@id/productimage"
android:textColor="#ff1493"
android:textSize="17sp" />
<TextView
android:id="@+id/product_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@id/product_contact"
android:layout_toRightOf="@id/productimage"
android:textColor="#ff1493"
android:textSize="13sp" />
<TextView
android:id="@+id/team_price"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/productimage"
android:layout_toRightOf="@id/productimage"
android:textColor="#ff1493"
android:textSize="13sp" />
<TextView
android:id="@+id/market_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/team_price"
android:layout_alignBottom="@id/productimage"
android:textColor="#ff1493"
android:textSize="10sp" />
<TextView
android:id="@+id/now_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBottom="@id/productimage"
android:textColor="#ff1493"
android:textSize="9sp" />
</RelativeLayout>
直接用Run As addriod application运行。能显示结果说明代码正确,否则会报错。
此项目有两个难点,一是要使用异步加载,新建一个子线程,用子线程去访问页数据,而不能用主线程去访问网页,否则会主线程会阻塞。其二是要将从网页获取的图片放进缓存保存,或在本地或保存在内存,依据数据的大小定,如果缓存里有对应的图片,即下一次访问的时候就直接从缓存中读取,不需要再从网页中加载,这样提高了效率。
1课程设计目的课程设计是船舶设计原理课程重要的实践性教学环节是培养学生掌握船舶设计基本原理和能力的技术基础主尺度论证与总布置设计是…
一设计目的1强化上机动手能力在理论和实践的基础上进一步巩固数据结构课程学习的内容掌握工程化软件设计的基本方法2掌握图的创建和应用3…
中国计量学院信息工程学院课程设计报告课程设计名称系统设计与仿真课程计二级学院信息工程学院专业班级10电信2班学姓成绩号名10003…
信息科学与工程学院高级语言程序设计课程设计报告学生成绩管理系统学科专业计算机科学与技术班级1301学号指导教师唐郑熠讲师学生二零年…
扬州大学数据结构课程设计报告课题名称姓名学院系科班级指导老师日期自来水管架设问题广陵学院陈宏建1一课程设计的题目自来水管理架设问题…
《课程设计报告》的格式要求撰写课程设计报告参考格式:题目(居中,小三号黑体字)学生姓名(学号)(学生姓名之间两格)(一律采用五号宋…
C语言程序设计实验报告1实验目的(1)掌握函数的定义方法、调用方法、参数说明以及返回值;(2)掌握实参与形参的对应关系,以及参数之…
信息科学与工程学院高级语言程序设计课程设计报告学生成绩管理系统学科专业计算机科学与技术班级1301学号指导教师唐郑熠讲师学生二零年…
课程设计报告C课程设计简单计算器学院专业名称班级学号学生姓名指导教师计算机与通信工程物联网工程敬茂华20xx年月日东北大学秦皇岛分…
苏州科技学院天平学院模拟电子技术课程设计报告课设名称正弦波方波三角波信号发生器设计专业班级电子信息工程物联网1221学号姓名张琪梁…
华中科技大学电子科学与技术系课程设计报告(20--xx年度第学期)名称:软件课程设计题目:院系:班级:学号:学生姓名:指导教师:设…