- 浏览: 2486357 次
- 性别:
- 来自: 成都
文章分类
最新评论
-
nation:
你好,在部署Mesos+Spark的运行环境时,出现一个现象, ...
Spark(4)Deal with Mesos -
sillycat:
AMAZON Relatedhttps://www.godad ...
AMAZON API Gateway(2)Client Side SSL with NGINX -
sillycat:
sudo usermod -aG docker ec2-use ...
Docker and VirtualBox(1)Set up Shared Disk for Virtual Box -
sillycat:
Every Half an Hour30 * * * * /u ...
Build Home NAS(3)Data Redundancy -
sillycat:
3 List the Cron Job I Have>c ...
Build Home NAS(3)Data Redundancy
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. 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;
}
}
发表评论
-
ionic UI(4)ionic2 framework - basic and components and native
2016-03-24 02:33 1222ionic UI(4)ionic2 framework - b ... -
ionic UI(3)TypeScript - handbook
2016-03-22 23:21 590ionic UI(3)TypeScript - handboo ... -
ionic UI(2)ionic2 framework - TypeScript - tutorial
2016-03-22 06:52 1619ionic UI(2)ionic2 framework - T ... -
Parse and Heroku Service(3)Parse Server and Parse Dashboard
2016-03-22 06:30 941Parse and Heroku Service(3)Pars ... -
Parse and Heroku Service(2)Mail Templates and Push Notification
2016-03-22 02:45 542Parse and Heroku Service(2)Mail ... -
ionic UI(1)Introduction
2016-03-19 03:18 679ionic UI(1)Introduction 1 Inst ... -
Parse and Heroku Service(1)Heroku Installation and Play
2016-03-19 00:13 784Parse and Heroic Service(1)Hero ... -
Hybrid(5)Customize Meteor Directly Google Login
2015-09-01 02:33 885Hybrid(5)Customize Meteor Direc ... -
Hybrid(4)Favorite Places - Google Login
2015-09-01 02:02 1295Hybrid(4)Favorite Places - Goog ... -
Hybrid(3)More Meteor Example - Social
2015-08-11 05:04 727Hybrid(3)More Meteor Example - ... -
Hybrid(2)meteor Running Android and iOS
2015-07-28 23:59 1003Hybrid(2)meteor Running Android ... -
Create the Google Play Account
2015-07-18 06:42 1055Create the Google Play Account ... -
Secure REST API and Mobile(1)Document Read and Understand OAUTH2
2015-07-14 00:36 728Secure REST API and Mobile(1)Do ... -
Screen Size and Web Design
2015-07-11 01:11 693Screen Size and Web Design iPh ... -
Hybrid(1)ionic Cordova meteor
2015-06-25 05:49 429Hybrid(1)ionic Cordova meteor ... -
Android Fire Project(1)Recall Env and Knowledge
2015-02-11 12:28 648Android Fire Project(1)Recall ... -
Android Content Framework(1)Concept
2014-06-14 13:54 1048Android Content Framework(1)Con ... -
Feel Android Studio(1)Install and Update Android Studio
2014-04-11 03:12 1974Feel Android Studio(1)Install a ... -
IOS7 App Development Essentials(2)iBeacon
2014-03-05 05:55 846IOS7 App Development Essentials ... -
IOS7 App Development Essentials(1) Persistent Store
2014-03-05 05:54 1276IOS7 App Development Essentials ...
相关推荐
Android Framework精编内核解析
android framework 实现细节,常见面试问题汇总
android Framework精编内核解析
This is a android framework full classes jar file
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精编内核解析.pdf 百度网盘永久地址
android framework相关面试题,详细深入的介绍了android framework相关知识点,可以很好帮助对android framework的深入理解
android framework 源代码
android framework开发经验总结
android framework面试题集 自己亲自总结的,各位有需要的可以随时下载
Android Framework 分析Android Framework 分析Android Framework 分析Android Framework 分析
Android 13.0 framework jar 包
android Framework层的修改规则,包括了编译过程和jni调用等。
《Android Framework 开发揭秘》,系统框架底层原理解密,腾讯技术团队最新出品!
自己编译的 Android12版本-framework源码jar 来源:AOSP源分支 android-12.1.0_r11
android framework层源码解析
Android Framework Source Code,下载并解压后,将解压出的整个文件夹复制或者移动到.../android-sdk-windows/sources文件夹下,然后重新打开SDK Manager.exe。
Android11.0 系统分析 Android11.0 启动分析 Android11.0 最新Framework解析 Android11.0 Binder通信原理
android4.4下 framework中的资源包,导入项目后可以使用android隐藏api
系统包 framework.jar ,可以实现隐藏方法的调用!以前写系统应用的时候用到过,之后找不到了,备份一下以免以后丢失