`
sillycat
  • 浏览: 2551340 次
  • 性别: Icon_minigender_1
  • 来自: 成都
社区版块
存档分类
最新评论

Android Framework(II)Person Sample with restserver

阅读更多
Android Framework(II)Person Sample with restserver

1. Base settings
I only use the spring rest template part, so my pom.xml will be like this:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sillycat.restclient</groupId>
<artifactId>restclient</artifactId>
<version>1.0</version>
<packaging>apk</packaging>
<name>restclient</name>
<properties>
<android-platform>10</android-platform>
<android-path>/home/luohua/tools/android-sdk-linux_x86</android-path>
<android-emulator>10</android-emulator>
<maven-android-plugin-version>2.8.4</maven-android-plugin-version>
<maven-compiler-plugin-version>2.3.2</maven-compiler-plugin-version>
<maven-eclipse-plugin-version>2.8</maven-eclipse-plugin-version>
<android-version>2.3.3</android-version>
<!-- Available Android versions: 1.5_r3, 1.5_r4, 1.6_r2, 2.1.2, 2.1_r1, 2.2.1, 2.3.1, 2.3.3 -->
<spring-android-version>1.0.0.M3</spring-android-version>
<spring-social-version>1.0.0.M3</spring-social-version>
<jackson-version>1.8.0</jackson-version>
<simple-version>2.5.3</simple-version>
<android-rome-version>1.0.0-r2</android-rome-version>
</properties>
<build>
<sourceDirectory>src</sourceDirectory>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<version>${maven-android-plugin-version}</version>
<configuration>
<sdk>
                        <path>${android-path}</path>
<platform>${android-platform}</platform>
</sdk>
<emulator>
<avd>${android-emulator}</avd>
</emulator>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>${maven-eclipse-plugin-version}</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${android-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<!--  Using Spring restTemplate -->
<groupId>org.springframework.android</groupId>
<artifactId>spring-android-rest-template</artifactId>
<version>${spring-android-version}</version>
</dependency>
<dependency>
<!-- Using Jackson for JSON marshaling -->
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<!-- API -->
<groupId>com.sillycat</groupId>
<artifactId>restserver</artifactId>
<version>1.0</version>
</dependency>
</dependencies>
<repositories>
<!-- For developing with Android ROME Feed Reader -->
<repository>
<id>android-rome-feed-reader-repository</id>
<name>Android ROME Feed Reader Repository</name>
<url>https://android-rome-feed-reader.googlecode.com/svn/maven2/releases</url>
</repository>
<repository>
<id>sillycat-repository</id>
<name>Sillycat Repository</name>
<url>https://sillycat.googlecode.com/svn/repository</url>
</repository>
<!-- For testing against latest Spring snapshots -->
<repository>
<id>org.springframework.maven.snapshot</id>
<name>Spring Maven Snapshot Repository</name>
<url>http://maven.springframework.org/snapshot</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<!-- For developing against latest Spring milestones -->
<repository>
<id>org.springframework.maven.milestone</id>
<name>Spring Maven Milestone Repository</name>
<url>http://maven.springframework.org/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
</project>

My android project configuration file AndroidManifest.xml will be:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.sillycat.restclient</groupId>
<artifactId>restclient</artifactId>
<version>1.0</version>
<packaging>apk</packaging>
<name>restclient</name>

<properties>
<android-platform>10</android-platform>
<android-path>/home/luohua/tools/android-sdk-linux_x86</android-path>
<android-emulator>10</android-emulator>
<maven-android-plugin-version>2.8.4</maven-android-plugin-version>
<maven-compiler-plugin-version>2.3.2</maven-compiler-plugin-version>
<maven-eclipse-plugin-version>2.8</maven-eclipse-plugin-version>
<android-version>2.3.3</android-version>
<!-- Available Android versions: 1.5_r3, 1.5_r4, 1.6_r2, 2.1.2, 2.1_r1, 2.2.1, 2.3.1, 2.3.3 -->
<spring-android-version>1.0.0.M3</spring-android-version>
<spring-social-version>1.0.0.M3</spring-social-version>
<jackson-version>1.8.0</jackson-version>
<simple-version>2.5.3</simple-version>
<android-rome-version>1.0.0-r2</android-rome-version>
</properties>

<build>
<sourceDirectory>src</sourceDirectory>
<finalName>${project.artifactId}</finalName>
<plugins>
<plugin>
<groupId>com.jayway.maven.plugins.android.generation2</groupId>
<artifactId>maven-android-plugin</artifactId>
<version>${maven-android-plugin-version}</version>
<configuration>
<sdk>
                        <path>${android-path}</path>
<platform>${android-platform}</platform>
</sdk>
<emulator>
<avd>${android-emulator}</avd>
</emulator>
<deleteConflictingFiles>true</deleteConflictingFiles>
<undeployBeforeDeploy>true</undeployBeforeDeploy>
</configuration>
<extensions>true</extensions>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin-version}</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-eclipse-plugin</artifactId>
<version>${maven-eclipse-plugin-version}</version>
<configuration>
<downloadSources>true</downloadSources>
<downloadJavadocs>true</downloadJavadocs>
</configuration>
</plugin>
</plugins>
</build>

<dependencies>
<dependency>
<groupId>com.google.android</groupId>
<artifactId>android</artifactId>
<version>${android-version}</version>
<scope>provided</scope>
</dependency>
<dependency>
<!--  Using Spring restTemplate -->
<groupId>org.springframework.android</groupId>
<artifactId>spring-android-rest-template</artifactId>
<version>${spring-android-version}</version>
</dependency>
<dependency>
<!-- Using Jackson for JSON marshaling -->
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>${jackson-version}</version>
</dependency>
<dependency>
<!-- API -->
<groupId>com.sillycat</groupId>
<artifactId>restserver</artifactId>
<version>1.0</version>
</dependency>
</dependencies>

<repositories>
<!-- For developing with Android ROME Feed Reader -->
<repository>
<id>android-rome-feed-reader-repository</id>
<name>Android ROME Feed Reader Repository</name>
<url>https://android-rome-feed-reader.googlecode.com/svn/maven2/releases</url>
</repository>
<repository>
<id>sillycat-repository</id>
<name>Sillycat Repository</name>
<url>https://sillycat.googlecode.com/svn/repository</url>
</repository>
<!-- For testing against latest Spring snapshots -->
<repository>
<id>org.springframework.maven.snapshot</id>
<name>Spring Maven Snapshot Repository</name>
<url>http://maven.springframework.org/snapshot</url>
<releases><enabled>false</enabled></releases>
<snapshots><enabled>true</enabled></snapshots>
</repository>
<!-- For developing against latest Spring milestones -->
<repository>
<id>org.springframework.maven.milestone</id>
<name>Spring Maven Milestone Repository</name>
<url>http://maven.springframework.org/milestone</url>
<snapshots><enabled>false</enabled></snapshots>
</repository>
</repositories>
</project>

The listview in res/layout/persons_list_item.xml:
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="fill_parent"
     android:layout_height="wrap_content"
     android:orientation="vertical"
     android:padding="5dp"
     >
     <TextView 
         android:id="@+id/person_name"
         android:textSize="16sp"
         android:textStyle="bold"
         android:layout_width="wrap_content"
         android:layout_height="wrap_content"
         />
</LinearLayout>

The string values in res/values/strings.xml  and res/values/urls.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="app_name">restclient</string>
<string-array name="main_options">
<item>all Person</item>
<item>get Person</item>
<item>add PERSON</item>
<item>update PERSON</item>
<item>delete PERSON</item>
</string-array>
</resources>

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="base_uri">http://10.0.2.2:8080/restserver</string>
</resources>

2. Base Java Class
MainActivity.java:
package com.sillycat.restclient;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.sillycat.restclient.rest.AllPersonActivity;
public class MainActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[] options = getResources().getStringArray(R.array.main_options);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, options);
setListAdapter(arrayAdapter);
}
protected void onListItemClick(ListView l, View v, int position, long id)
{
Intent intent = new Intent();
switch(position)
{
case 0:
intent.setClass(this, AllPersonActivity.class);
startActivity(intent);
break;
...snip...
default:
      break;
}
}
}
MainApplication.java:
package com.sillycat.restclient;
import android.app.Application;
public class MainApplication extends Application {
...snip...
}
AsyncActivity.java:
package com.sillycat.restclient;
public interface AsyncActivity
{
public MainApplication getApplicationContext();
public void showLoadingProgressDialog();
public void showProgressDialog(CharSequence message);
public void dismissProgressDialog();
}
AbstractAsyncActivity.java:
package com.sillycat.restclient;
import android.app.Activity;
import android.app.ProgressDialog;
public abstract class AbstractAsyncActivity extends Activity implements AsyncActivity
{
protected static final String TAG = AbstractAsyncActivity.class.getSimpleName();
private ProgressDialog _progressDialog;
private boolean _destroyed = false;
@Override
public MainApplication getApplicationContext()
{
return (MainApplication) super.getApplicationContext();
}
@Override
protected void onDestroy()
{
super.onDestroy();
_destroyed = true;
}
public void showLoadingProgressDialog()
{
this.showProgressDialog("Loading. Please wait...");
}
public void showProgressDialog(CharSequence message)
{
if (_progressDialog == null)
{
_progressDialog = new ProgressDialog(this);
_progressDialog.setIndeterminate(true);
}
_progressDialog.setMessage(message);
_progressDialog.show();
}
public void dismissProgressDialog()
{
if (_progressDialog != null && !_destroyed)
{
_progressDialog.dismiss();
}
}
}
AbstractAsyncListActivity.java is almost the same:
package com.sillycat.restclient;
import android.app.Activity;
import android.app.ProgressDialog;
public abstract class AbstractAsyncActivity extends Activity implements AsyncActivity
{
protected static final String TAG = AbstractAsyncActivity.class.getSimpleName();
private ProgressDialog _progressDialog;
private boolean _destroyed = false;
@Override
public MainApplication getApplicationContext()
{
return (MainApplication) super.getApplicationContext();
}
@Override
protected void onDestroy()
{
super.onDestroy();
_destroyed = true;
}
public void showLoadingProgressDialog()
{
this.showProgressDialog("Loading. Please wait...");
}
public void showProgressDialog(CharSequence message)
{
if (_progressDialog == null)
{
_progressDialog = new ProgressDialog(this);
_progressDialog.setIndeterminate(true);
}
_progressDialog.setMessage(message);
_progressDialog.show();
}
public void dismissProgressDialog()
{
if (_progressDialog != null && !_destroyed)
{
_progressDialog.dismiss();
}
}
}
3. AllPerson Activity
AllPersonActivity.java:
package com.sillycat.restclient.rest;
import java.util.ArrayList;
import java.util.List;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import com.sillycat.easymarket.model.Person;
import com.sillycat.easymarket.view.PersonList;
import com.sillycat.restclient.AbstractAsyncListActivity;
import com.sillycat.restclient.R;
public class AllPersonActivity extends AbstractAsyncListActivity {
protected static final String TAG = AllPersonActivity.class.getSimpleName();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
}
@Override
public void onStart()
{
super.onStart();
// when this activity starts, initiate an asynchronous HTTP GET request
new DownloadStatesTask().execute();
}
private void refreshStates(List<Person> persons)
{
if (persons == null)
{
return;
}
PersonsListAdapter adapter = new PersonsListAdapter(this, persons);
setListAdapter(adapter);
}
private class DownloadStatesTask extends AsyncTask<Void, Void, List<Person>>
{
@Override
protected void onPreExecute()
{
// before the network request begins, show a progress indicator
showLoadingProgressDialog();
}
@Override
protected List<Person> doInBackground(Void... params)
{
try
{
// The URL for making the GET request
final String url = getString(R.string.base_uri) + "/service/persons";
// Set the Accept header for "application/json"
HttpHeaders requestHeaders = new HttpHeaders();
List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(acceptableMediaTypes);
// Populate the headers in an HttpEntity object to use for the request
HttpEntity<?> requestEntity = new HttpEntity<Object>(requestHeaders);
// Create a new RestTemplate instance
RestTemplate restTemplate = new RestTemplate();
// Perform the HTTP GET request
ResponseEntity<PersonList> responseEntity = restTemplate.exchange(url, HttpMethod.GET, requestEntity, PersonList.class);
// convert the array to a list and return it
PersonList persons_list = responseEntity.getBody();
return persons_list.getPersons();
}
catch(Exception e)
{
Log.e(TAG, e.getMessage(), e);
}
return null;
}
@Override
protected void onPostExecute(List<Person> result)
{
// hide the progress indicator when the network request is complete
dismissProgressDialog();
// return the list of states
refreshStates(result);
}
}
}
PersonsListAdapter.java
package com.sillycat.restclient.rest;
import java.util.List;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;
import com.sillycat.easymarket.model.Person;
import com.sillycat.restclient.R;
public class PersonsListAdapter extends BaseAdapter
{
private List<Person> _persons;
private final LayoutInflater _layoutInflater;
public PersonsListAdapter(Context context, List<Person> _persons)
{
this._persons = _persons;
this._layoutInflater = LayoutInflater.from(context);
}
public int getCount()
{
return _persons.size();
}
public Person getItem(int position)
{
return _persons.get(position);
}
public long getItemId(int position)
{
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
Person person = getItem(position);
View view = convertView;
if (view == null)
{
view = _layoutInflater.inflate(R.layout.persons_list_item, parent, false);
}
TextView t = (TextView) view.findViewById(R.id.person_name);
t.setText(person.getPersonName());
return view;
}
}




分享到:
评论

相关推荐

    Android Framework 开发揭秘

    《Android Framework 开发揭秘》是腾讯技术团队倾力打造的一部深入探讨Android系统框架底层原理的专业书籍。本书针对Android开发者,尤其是对系统级开发有深厚兴趣的工程师们,旨在揭示Android系统的神秘面纱,帮助...

    android framework 开发揭秘

    《Android Framework 开发揭秘》这本书深入剖析了Android系统的内部运作机制,是Android开发者提升技术深度的宝贵资料。它涵盖了从系统启动到服务框架的构建,再到关键组件的工作原理,旨在帮助读者理解和掌握...

    Android Framework精编内核解析

    《Android Framework精编内核解析》是一本深入探讨Android系统核心框架层的专著,它主要涵盖了Android系统架构的关键组成部分,旨在帮助读者理解和掌握Android应用开发背后的机制。在这个压缩包中,包含了一份名为...

    Android Framework精编内核解析.pdf 百度网盘永久地址

    Android Framework精编内核解析.pdf 百度网盘永久地址

    android framework面试题集

    Android Framework 面试题集学习指南 一、Android 应用性能分析 Android 应用性能分析是指对 Android 应用程序的性能进行测量、评估和优化的过程。主要关注两个方面:响应速度和 UI 刷新速度。可以使用 Android ...

    android Framework层的修改规则

    ### Android Framework层的修改规则详解 #### 一、概述 Android Framework层是Android系统的核心组件之一,它提供了连接硬件抽象层(HAL)与应用程序层之间的桥梁。为了更好地理解和操作这一层,本文将详细介绍如何...

    Android Framework进阶全套视频资源

    Android Framework,从入门到精通全套视频,欢迎下载!

    探索Android FrameWork底层开发视频全套

    1.初探Android 2.Android开发环境搭建与编译 3.Android编译过程分析 4.android.mk初识 5.Android.mk学习1 6.Android.mk学习2 7.Android.mk学习3 8.Android启动课程大纲 9.Android启动模式 10.init启动分析 11.走入...

    Android framework.jar 下载,可以直接依赖

    `framework.jar`是Android系统的一个关键组件,包含了Android Framework的类库,包括Activity管理、Intent机制、BroadcastReceiver、Service、ContentProvider、View体系等核心功能。这个文件对于系统开发者和进行...

    android framework 编译出来的jar包

    android4.4下 framework中的资源包,导入项目后可以使用android隐藏api

    android Framework源码最新版

    Android Framework源码是Android操作系统的核心组成部分,包含了系统服务、库和API框架,是开发者深入理解Android系统工作原理的重要参考资料。这份最新的源码版本提供了一个深入洞察Android系统内部机制的机会,...

    Android-androidframework层源码解析

    在Android系统中,框架层(Android Framework)是操作系统的核心部分,它定义了应用程序与系统服务之间的接口,使得开发者可以利用这些接口构建丰富的应用。本文将深入解析Android Framework层的源码,带你理解这一...

    android 14 framework jar 包

    android14 framework jar 包

    Android 13.0 framework jar 包

    Android 13.0 framework jar 包

    android framework 源代码

    Android Framework是Android操作系统的核心组成部分,它为应用程序层提供了一个运行环境和API,使得开发者能够创建出功能丰富的应用程序。深入理解Android Framework源代码对于任何想要在Android平台上进行深度开发...

    android底层framework开发课件

    Android底层Framework开发是Android系统开发中的一个重要组成部分,它涉及到操作系统的核心功能,如系统服务、硬件接口、应用程序框架等。这份“android底层framework开发课件”提供了深入学习这一领域的宝贵资源。 ...

    Android-framework详细分析

    《Android Framework深度解析》 Android Framework是Android操作系统的核心组成部分,它是Android应用开发的基础,负责提供应用程序接口(API)以及系统服务。在这个详细分析中,我们将深入探讨Android Framework的...

    如何深入学习AndroidFramework

    深入学习Android Framework是Android开发中的一个高级阶段,它涉及到操作系统的核心层面,对于想要成为顶级Android开发者或者希望从事系统级优化的人来说至关重要。以下将详细解析各部分知识点: **第一节 什么是 ...

    Android Framework Source Code

    Android Framework Source Code,下载并解压后,将解压出的整个文件夹复制或者移动到.../android-sdk-windows/sources文件夹下,然后重新打开SDK Manager.exe。

    Android12版本-framework源码jar

    自己编译的 Android12版本-framework源码jar 来源:AOSP源分支 android-12.1.0_r11

Global site tag (gtag.js) - Google Analytics