読者です 読者をやめる 読者になる 読者になる

AndroidからGAE(slim3)にデータを登録するサンプルを作成しました。

[開発環境]
 ・Eclipse Indigo
 ・Android 2.3.3
 ・Slim3 Version 1.0.13


[サーバ(GAE)側のソース(Slim3を使用)]


[src\jp\co\hoshisoft\slim3sample02\controller\RegistController.java]


package jp.co.hoshisoft.slim3sample02.controller;

import jp.co.hoshisoft.slim3sample02.service.MemberService;

import org.slim3.controller.Controller;
import org.slim3.controller.Navigation;
import org.slim3.util.RequestMap;

public class RegistController extends Controller {

private MemberService service = new MemberService();

@Override
public Navigation run() throws Exception {
RequestMap requestMap = new RequestMap(request);
service.insert(requestMap);
return null;
}
}




[src\jp\co\hoshisoft\slim3sample02\model\Member.java]

package jp.co.hoshisoft.slim3sample02.model;

import java.io.Serializable;

import com.google.appengine.api.datastore.Key;

import org.slim3.datastore.Attribute;
import org.slim3.datastore.Model;
import org.slim3.datastore.json.Json;

@Model(schemaVersion = 1)
public class Member implements Serializable {

private static final long serialVersionUID = 1L;

@Attribute(primaryKey = true, json=@Json(ignore=true))
private Key key;

@Attribute(version = true, json=@Json(ignore=true))
private Long version;

private String name;
private String email;

/**
* Returns the key.
*
* @return the key
*/
public Key getKey() {
return key;
}

/**
* Sets the key.
*
* @param key
* the key
*/
public void setKey(Key key) {
this.key = key;
}

/**
* Returns the version.
*
* @return the version
*/
public Long getVersion() {
return version;
}

/**
* Sets the version.
*
* @param version
* the version
*/
public void setVersion(Long version) {
this.version = version;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getEmail() {
return email;
}

public void setEmail(String email) {
this.email = email;
}

@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + *1;
return result;
}

@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
Member other = (Member) obj;
if (key == null) {
if (other.key != null) {
return false;
}
} else if (!key.equals(other.key)) {
return false;
}
return true;
}
}




[src\jp\co\hoshisoft\slim3sample02\service\MemberService.java]

package jp.co.hoshisoft.slim3sample02.service;

import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import com.google.appengine.api.datastore.Transaction;

import jp.co.hoshisoft.slim3sample02.meta.MemberMeta;
import jp.co.hoshisoft.slim3sample02.model.Member;

public class MemberService {
public List allList() {
MemberMeta e = new MemberMeta();
List list = Datastore.query(e)
.sort(e.name.asc)
.asList();
return list;
}




[src\jp\co\hoshisoft\slim3sample02\service\MemberService.java]

package jp.co.hoshisoft.slim3sample02.service;

import java.util.List;
import java.util.Map;

import org.slim3.datastore.Datastore;
import org.slim3.util.BeanUtil;

import com.google.appengine.api.datastore.Transaction;

import jp.co.hoshisoft.slim3sample02.meta.MemberMeta;
import jp.co.hoshisoft.slim3sample02.model.Member;

public class MemberService {

public Member insert(Map input) {
Member member = new Member();
BeanUtil.copy(input, member);
Transaction tx = Datastore.beginTransaction();
Datastore.put(member);
Datastore.commit(tx);
return member;
}

public List allList() {

MemberMeta e = new MemberMeta();
List list = Datastore.query(e)
.sort(e.name.asc)
.asList();

return list;
}




[war\WEB-INF\appengine-web.xml]

<?xml version="1.0" encoding="utf-8"?>
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
<application>アプリケーションID</application>
<version>1</version>

<precompilation-enabled>true</precompilation-enabled>

<system-properties>
<property name="slim3.hotReloading" value="true"/>
<!--
<property name="slim3.datastoreDeadline" value="8"/>
<property name="slim3.uploadSizeMax" value="1000000"/>
<property name="slim3.uploadFileSizeMax" value="100000"/>
-->
<property name="java.util.logging.config.file" value="WEB-INF/classes/logging.properties"/>
</system-properties>
<sessions-enabled>false</sessions-enabled>
<threadsafe>true</threadsafe>
</appengine-web-app>



[war\WEB-INF\appengine-web.xml]

<?xml version="1.0" encoding="utf-8"?>

<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5">
<context-param>
<param-name>slim3.rootPackage</param-name>
<param-value>jp.co.hoshisoft.slim3sample02</param-value>
</context-param>

 ・
 ・
 ・
</web-app>




[クライアント(Android)側のソース]


[src\jp\co\hoshisoft\AndroidTest08Activity.java]

package jp.co.hoshisoft.androidtest08;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Androidtest08Activity extends Activity {

private static final String URI = "http://XXXXXXX.appspot.com/regist";

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText editText1 = (EditText)findViewById(R.id.editText1);
EditText editText2 = (EditText)findViewById(R.id.editText2);
String name = editText1.getText().toString();
String email = editText2.getText().toString();

HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URI);
List nameValuePair = new ArrayList(2);
nameValuePair.add(new BasicNameValuePair("name", name));
nameValuePair.add(new BasicNameValuePair("email", email));

try {
httppost.setEntity(new UrlEncodedFormEntity(nameValuePair));
HttpResponse response = httpclient.execute(httppost);
if (response != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
showAlert("保存しました");
}
else {
showAlert("保存に失敗しました");
}
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
catch (IOException e) {
e.printStackTrace();
}
}
});
}

public void showAlert(String message) {
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setMessage(message);
ad.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
setResult(RESULT_OK);
}
});
ad.create();
ad.show();
}
}





[res\layout\main.xml]

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1">
<Button android:text="登録" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<LinearLayout android:layout_width="match_parent" android:id="@+id/linearLayout1" android:layout_height="wrap_content">
</LinearLayout>
<TableLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/tableLayout1">
<TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:text="名前" android:layout_height="wrap_content" android:id="@+id/textView1"></TextView>
<EditText android:layout_width="200px" android:layout_height="wrap_content" android:id="@+id/editText1"></EditText>
</TableRow>
<TableRow android:id="@+id/tableRow2" android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content" android:text="Email" android:layout_height="wrap_content" android:id="@+id/textView2"></TextView>
<EditText android:layout_width="200px" android:layout_height="wrap_content" android:id="@+id/editText2"></EditText>
</TableRow>
</TableLayout>
</LinearLayout>



[実行時の画面]
画面1


[登録後検索した画面]
画面2

*1:key == null) ? 0 : key.hashCode(