Commits

Anonymous committed 3b72661

XMLパース処理の追加

Comments (0)

Files changed (8)

Binary file modified.

Binary file modified.

Add a comment to this file

bin/org/homelinux/sheephead/androizou/Upload$1.class

Binary file added.

Add a comment to this file

bin/org/homelinux/sheephead/androizou/Upload.class

Binary file modified.

Binary file modified.

res/layout/upload.xml

 
 
 
+
 </LinearLayout>

res/values/strings.xml

 <?xml version="1.0" encoding="utf-8"?>
 <resources>
     <string name="hello">Androizou</string>
-    <string name="app_name">Androizou</string>
+    <string name="app_name">アンドロイ蔵</string>
     <string name="user_name">user</string>
     <string name="pass_name">pass</string>
 	<string name="button_name">login</string>

src/org/homelinux/sheephead/androizou/Upload.java

 package org.homelinux.sheephead.androizou;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.IOException;
+import java.io.StringReader;
 import java.io.UnsupportedEncodingException;
 import java.net.URI;
 
 import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
 import org.apache.http.auth.AuthScope;
 import org.apache.http.auth.UsernamePasswordCredentials;
 import org.apache.http.client.ClientProtocolException;
 import org.apache.http.entity.mime.content.StringBody;
 import org.apache.http.impl.client.DefaultHttpClient;
 import org.apache.http.util.EntityUtils;
+import org.xmlpull.v1.XmlPullParser;
+import org.xmlpull.v1.XmlPullParserException;
+import org.xmlpull.v1.XmlPullParserFactory;
 
 import android.app.Activity;
 import android.content.Intent;
+import android.content.SharedPreferences;
 import android.database.Cursor;
 import android.net.Uri;
 import android.os.Bundle;
 import android.util.Log;
+import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
+import android.widget.Toast;
 
 public class Upload extends Activity {
-    private static final String TAG = "Logact";
-    private String filepath;
-    public EditText titleEdit;
-    public EditText tagEdit;
-    public Button uploadButton;
+	private static final String TAG = "Logact";
+	private String filepath;
+	public EditText titleEdit;
+	public EditText tagEdit;
+	public Button uploadButton;
+	private String strpass;
+	private String strid;
 
-    public void onCreate(Bundle savedInstanceState) {
-        super.onCreate(savedInstanceState);
-        setContentView(R.layout.upload);
-        titleEdit = (EditText) findViewById(R.id.user_edit);
-        tagEdit = (EditText) findViewById(R.id.pass_edit);
-        uploadButton = (Button) findViewById(R.id.login_button);
+	public void onCreate(Bundle savedInstanceState) {
+		super.onCreate(savedInstanceState);
+		setContentView(R.layout.upload);
+		titleEdit = (EditText) findViewById(R.id.EditTitle);
+		tagEdit = (EditText) findViewById(R.id.EditTag);
+		uploadButton = (Button) findViewById(R.id.Button01);
 
+		//ファイルのpathを取得		
+		Uri uri =  this.getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
+		Cursor csr = this.managedQuery(uri, null, null, null, null);
+		csr.moveToFirst();
+		for (int k = 0;k < csr.getCount(); k++){
+			filepath = csr.getString(csr.getColumnIndexOrThrow("_data"));
+			Log.d(TAG,"data=" + csr.getString(csr.getColumnIndexOrThrow("_data")));
+			csr.moveToNext();
+		}
 
-        Uri uri =  this.getIntent().getParcelableExtra(Intent.EXTRA_STREAM);
-        Cursor csr = this.managedQuery(uri, null, null, null, null);
-        csr.moveToFirst();
-        for (int k = 0;k < csr.getCount(); k++){
-            filepath = csr.getString(csr.getColumnIndexOrThrow("_data"));
-            Log.d(TAG,"data=" + csr.getString(csr.getColumnIndexOrThrow("_data")));
-            csr.moveToNext();
-        }
+		SharedPreferences preferences = getSharedPreferences( getString(R.string.preferences_name), MODE_PRIVATE);
+		strid = preferences.getString( getString(R.string.preferences_id), "");
+		strpass = preferences.getString( getString(R.string.preferences_pass), "");
 
+		DefaultHttpClient spnCandidate = new DefaultHttpClient();
+		HttpPost spnGet = new HttpPost("http://api.photozou.jp/rest/photo_album"); 
 
+		URI albumUri = spnGet.getURI();
+		spnCandidate.getCredentialsProvider().setCredentials(
+				new AuthScope(albumUri.getHost(), albumUri.getPort()),
+				new UsernamePasswordCredentials(strid,strpass));
+		try {
+			HttpResponse albumRes = spnCandidate.execute(spnGet);
+			int status = albumRes.getStatusLine().getStatusCode();
+			if (status == HttpStatus.SC_OK) { 
 
+				//レスポンスをOutputStreamに書き出す
+				ByteArrayOutputStream ostream = new ByteArrayOutputStream();
+				albumRes.getEntity().writeTo(ostream);
+				//XMLパーサーを生成する
+				final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
+				final XmlPullParser parser = factory.newPullParser();
+				//XMLパーサに解析したい内容を設定する
+				parser.setInput(new StringReader(ostream.toString()));
+				//XML文章の終わりまでループして解析する
+				for(int eventType = parser.getEventType();eventType != XmlPullParser.END_DOCUMENT; eventType = parser.next()){
+					String tagName;
+					String tagText;
+					//String albumName;
+					//String album_id;
 
-        DefaultHttpClient client = new DefaultHttpClient();
-       
+					switch(eventType){
+					//TAGの始まり
+					case XmlPullParser.START_TAG:
+						//TAGの名前を取得する
+						tagName = parser.getName();
 
-        //HttpPost post = new HttpPost("http://api.photozou.jp/rest/photo_album");
-        HttpPost post = new HttpPost("http://api.photozou.jp/rest/photo_add"); 
+						//TAGの名前がtitle
+						if(tagName.equals("album_id")){
+							//次の要素へ進む
+							parser.next();
+							//要素がTEXTだったら内容を取り出す
+							if(parser.getEventType() == XmlPullParser.TEXT){
+								tagText = parser.getText();
+								Log.i(TAG, tagText);
+							}
 
-        URI posturi = post.getURI();
-        client.getCredentialsProvider().setCredentials(
-                new AuthScope(posturi.getHost(), posturi.getPort()),
-                new UsernamePasswordCredentials("mail","pass"));
+							break;
+						}
+					}
+				}
 
-        MultipartEntity entity = new MultipartEntity();
-        post.setEntity(entity);
-        //↓送りたいファイルのセット
-        FileBody fileBody = new FileBody(new File(filepath),"image/jpeg");
-        entity.addPart("photo", fileBody);
-        try {
-            entity.addPart("album_id", new StringBody("134703"));
-        } catch (UnsupportedEncodingException e1) {
-            // 自動生成された catch ブロック
-            e1.printStackTrace();
-        }
+			}
+		} catch (ClientProtocolException e1) {
+			Toast.makeText( Upload.this, "アップロードできませんでした。ファイルの種類を確認してください。", Toast.LENGTH_LONG).show();
+			e1.printStackTrace();
+		} catch (IOException e1) {
+			Toast.makeText( Upload.this, "アップロードできませんでした。ネットワークの状態を確認してください。", Toast.LENGTH_LONG).show();
+			e1.printStackTrace();
+		} catch (XmlPullParserException e) {
+			Toast.makeText( Upload.this, "アップロードできませんでした。XMLがおかしいかもしれません。", Toast.LENGTH_LONG).show();
+			// TODO 自動生成された catch ブロック
+			e.printStackTrace();
+		}	
 
-        //送信
-        try {
-            HttpResponse res = client.execute(post);
-            String strres = EntityUtils.toString(res.getEntity());
-            Log.d(TAG,strres);
-        } catch (ClientProtocolException e) {
-            Log.d(TAG,"post Protocol error");
-        } catch (IOException e) {
+		//		アップロードボタンのイベントリスナ
+		uploadButton.setOnClickListener(new View.OnClickListener() {
+			public void onClick(View v) {
 
 
-            Log.d(TAG,"io error");
-        } 
 
-        //        	private void SetIDAndPass( boolean bCreate){
-        //		updateButton.setClickable(false);
-        //		reloadButton.setClickable(false);
-        // プリファレンスからID/Passを取得
-        //		SharedPreferences preferences = getSharedPreferences( getString(R.string.preferences_name), MODE_PRIVATE);
-        //		strid = preferences.getString( getString(R.string.preferences_id), "");
-        //		strpass = preferences.getString( getString(R.string.preferences_pass), "");
-        //		if(!chkid( bCreate)){
-        //			return;
-        //		}
-        //		httpClient = new DefaultHttpClient();
-        //		Credentials cred = new UsernamePasswordCredentials( strid, strpass);
-        //		httpClient.getCredentialsProvider().setCredentials( new AuthScope("twitter.com", 80), cred);
-        //		GetAndWriteHomeTimeLine();
-        //		updateButton.setClickable(true);
-        //		reloadButton.setClickable(true);
-        //	}
+				DefaultHttpClient client = new DefaultHttpClient();
+				//HttpPost post = new HttpPost("http://api.photozou.jp/rest/photo_album");
+				HttpPost post = new HttpPost("http://api.photozou.jp/rest/photo_add"); 
+				URI posturi = post.getURI();
+				client.getCredentialsProvider().setCredentials(
+						new AuthScope(posturi.getHost(), posturi.getPort()),
+						new UsernamePasswordCredentials(strid,strpass));
+				MultipartEntity entity = new MultipartEntity();
+				post.setEntity(entity);
+				//↓送りたいファイルのセット
+				FileBody fileBody = new FileBody(new File(filepath),"image/jpeg");
+				entity.addPart("photo", fileBody);
+				try {
+					entity.addPart("album_id", new StringBody("134703"));
+				} catch (UnsupportedEncodingException e1) {
+					// 自動生成された catch ブロック
+					e1.printStackTrace();
+				}
+				//送信
+				try {
+					HttpResponse res = client.execute(post);
+					String strres = EntityUtils.toString(res.getEntity());
+					Log.d(TAG,strres);
+				} catch (ClientProtocolException e) {
+					Log.d(TAG,"post Protocol error");
+					Toast.makeText( Upload.this, "アップロードできませんでした。ファイルの種類を確認してください。。", Toast.LENGTH_LONG).show();
+				} catch (IOException e) {
+					Log.d(TAG,"io error");
 
-    }
+				} 
+
+				Toast.makeText( Upload.this, "アップロードが完了しました。", Toast.LENGTH_LONG).show();
+
+			}
+		});
+
+
+
+
+	}
 }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.