ITeye博客

Android Framework(II)Person Sample with restserver

sillycat · 2011-05-30 23:52 · Mobile Platform · 编程语言 · Spring · REST · maven · Android · SVN · Markdown

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: 4.0.0 com.sillycat.restclient restclient 1.0 apk restclient 10 /home/luohua/tools/android-sdk-linux_x86 10 2.8.4 2.3.2 2.8 2.3.3 1.0.0.M3 1.0.0.M3 1.8.0 2.5.3 1.0.0-r2 src ${project.artifactId} com.jayway.maven.plugins.android.generation2 maven-android-plugin ${maven-android-plugin-version} ${android-path} ${android-platform} ${android-emulator} true true true maven-compiler-plugin ${maven-compiler-plugin-version} org.apache.maven.plugins maven-eclipse-plugin ${maven-eclipse-plugin-version} true true com.google.android android ${android-version} provided org.springframework.android spring-android-rest-template ${spring-android-version} org.codehaus.jackson jackson-mapper-asl ${jackson-version} com.sillycat restserver 1.0 android-rome-feed-reader-repository Android ROME Feed Reader Repository https://android-rome-feed-reader.googlecode.com/svn/maven2/releases sillycat-repository Sillycat Repository https://sillycat.googlecode.com/svn/repository org.springframework.maven.snapshot Spring Maven Snapshot Repository http://maven.springframework.org/snapshot false true org.springframework.maven.milestone Spring Maven Milestone Repository http://maven.springframework.org/milestone false My android project configuration file AndroidManifest.xml will be: 4.0.0 com.sillycat.restclient restclient 1.0 apk restclient 10 /home/luohua/tools/android-sdk-linux_x86 10 2.8.4 2.3.2 2.8 2.3.3 1.0.0.M3 1.0.0.M3 1.8.0 2.5.3 1.0.0-r2 src ${project.artifactId} com.jayway.maven.plugins.android.generation2 maven-android-plugin ${maven-android-plugin-version} ${android-path} ${android-platform} ${android-emulator} true true true maven-compiler-plugin ${maven-compiler-plugin-version} org.apache.maven.plugins maven-eclipse-plugin ${maven-eclipse-plugin-version} true true com.google.android android ${android-version} provided org.springframework.android spring-android-rest-template ${spring-android-version} org.codehaus.jackson jackson-mapper-asl ${jackson-version} com.sillycat restserver 1.0 android-rome-feed-reader-repository Android ROME Feed Reader Repository https://android-rome-feed-reader.googlecode.com/svn/maven2/releases sillycat-repository Sillycat Repository https://sillycat.googlecode.com/svn/repository org.springframework.maven.snapshot Spring Maven Snapshot Repository http://maven.springframework.org/snapshot false true org.springframework.maven.milestone Spring Maven Milestone Repository http://maven.springframework.org/milestone false The listview in res/layout/persons_list_item.xml: The string values in res/values/strings.xml and res/values/urls.xml restclient all Person get Person add PERSON update PERSON delete PERSON http://10.0.2.2:8080/restserver 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 arrayAdapter = new ArrayAdapter(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 persons) { if (persons == null) { return; } PersonsListAdapter adapter = new PersonsListAdapter(this, persons); setListAdapter(adapter); } private class DownloadStatesTask extends AsyncTask> { @Override protected void onPreExecute() { // before the network request begins, show a progress indicator showLoadingProgressDialog(); } @Override protected List 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 acceptableMediaTypes = new ArrayList(); acceptableMediaTypes.add(MediaType.APPLICATION_JSON); requestHeaders.setAccept(acceptableMediaTypes); // Populate the headers in an HttpEntity object to use for the request HttpEntity requestEntity = new HttpEntity(requestHeaders); // Create a new RestTemplate instance RestTemplate restTemplate = new RestTemplate(); // Perform the HTTP GET request ResponseEntity 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 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 _persons; private final LayoutInflater _layoutInflater; public PersonsListAdapter(Context context, List _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; } }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。