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;
}
}




更多相关文章

  1. 代码中设置drawableleft
  2. android 3.0 隐藏 系统标题栏
  3. Android开发中activity切换动画的实现
  4. Android(安卓)学习 笔记_05. 文件下载
  5. Android中直播视频技术探究之—摄像头Camera视频源数据采集解析
  6. 技术博客汇总
  7. android 2.3 wifi (一)
  8. AndRoid Notification的清空和修改
  9. Android中的Chronometer

随机推荐

  1. Android旅游APP类项目源代码
  2. 探索Android中selector和shape的结合使用
  3. Android中建造者模式自定义Dialog
  4. Android客户端如何从Web服务端加载图片
  5. 什么特性造就了Android快速启动
  6. Android屏幕适配(个人的一点点总结及经验)
  7. Android兼容性
  8. android wifi adb调试
  9. Android 实际项目架构提炼开篇一
  10. android中的强指针和弱指针