今日のメモ (3)

とりあえず post できるように、って事で以下なコードを GAE 側に追加。

  def post(self):
    user = users.get_current_user()
    if user:
      d = dict([("result", "Login Success")])
    else:
      d = dict([("result", "Login Unsuccessful")])

    self.response.out.write(simplejson.dumps(d, ensure_ascii=False))

get と同じです。若干冗長。
で、上記を deploy してリトライ。

してみたけれど

ステイタス 500 が戻るな。ざっくりですが実装は以下なカンジ。

        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("login", authKey));
        nvps.add(new BasicNameValuePair("continue", "/"));
 
        HttpResponse response = null;

        HttpPost httpost = new HttpPost("https://test-yamanetoshi.appspot.com/_ah/login");
        try {
        	httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
        	response = httpClient.execute(httpost);

post は無理なのかな。別途調査する事にしてここはスルー。元に戻してみたら当たり前に JSON が戻ってきました。ちょっとこのあたり理解が微妙な所があるんで別途確認します。

なんとなく

手続きを分割してみた。

package com.example.gaetest;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.protocol.HTTP;

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class TestActivity extends Activity {

    private String TAG = "TestActivity";
    private DefaultHttpClient httpClient;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        httpClient = new DefaultHttpClient();

        try {
        	String authKey = getAuthKey();
        	HttpResponse response = getAppspotContent(authKey);
        }
        catch (Exception e) {
        	Log.e(TAG, "error");
        	finish();
        }

        setContentView(R.layout.main);
    }
    
    private HttpResponse getAppspotContent(String authKey) throws Exception {
        HttpResponse response = null;
        HttpGet httpget = new HttpGet("http://test-yamanetoshi.appspot.com/_ah/login?auth=" + authKey + "&continue=" + "/");

       	response = httpClient.execute(httpget);
        	
        Log.i(TAG, "Appspot.com Login Response: " + response.getStatusLine());
            
       	InputStream is = response.getEntity().getContent();
       	BufferedReader isr = null;

	isr = new BufferedReader(new InputStreamReader(is));
	String line = null;
	while ((line = isr.readLine()) != null) {
	    Log.i(TAG, "RESPONSE: " + line);
	}

       	if (isr != null) isr.close();
        
        return response;
    }
    
    private String getAuthKey() throws Exception {
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("Email", "hoge@gmail.com"));
        nvps.add(new BasicNameValuePair("Passwd", "fugafuga"));
        nvps.add(new BasicNameValuePair("service", "ah"));
        nvps.add(new BasicNameValuePair("source", "test-yamanetoshi"));
        nvps.add(new BasicNameValuePair("accountType", "HOSTED_OR_GOOGLE"));

        //Login at Google.com
        HttpPost httpost = new HttpPost("https://www.google.com/accounts/ClientLogin");
        HttpResponse response = null;

       	httpost.setEntity(new UrlEncodedFormEntity(nvps, HTTP.UTF_8));
       	response = httpClient.execute(httpost);
        Log.i(TAG, "Login Response: " + response.getStatusLine());
        
        String authKey = null;
        BufferedReader br = null;
        br = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = null;
        while ((line = br.readLine()) != null) {
            String[] s = line.split("=");
            if (s.length == 2 && s[0].equalsIgnoreCase("auth")) {
                authKey = s[1];
                break;
            }
        }

        if (br != null) br.close();
        
        Log.i(TAG, "AUTH = " + authKey);
        
        return authKey;
    }
}
  • getAuthKey() は認証できたら authKey を戻す
  • getAppspotContent() は authKey 渡して response を戻す
    • できたら URL (てか URI) を渡して response 戻す形が理想か
  • とりあえず別スレッドで、ってのは別途検討します
    • この戻りは同期がとれてた方が良さげなので
    • 砂時計とかってどうやるんだろ