1) JSON オブジェクトとともに複数のファイルをアップロードするためのネイティブ Android プラグインを作成します。2) コンセプトはマルチパートファイルのアップロードです。3) オフライン モード同期用に作成します。4) Phonegap と Android ネイティブ コード。
ネイティブ Andorid コードの Javascript 呼び出し
alert(window.FilesUpload.sendFiles(JSON.stringify(jsonObj)));
以下は Android プラグイン (FilesUpload.java) です。
package com.yourpackagename.core;
import java.io.*;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.ResponseHandler;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.BasicResponseHandler;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntity;
import org.apache.http.entity.mime.content.FileBody;
import org.apache.http.entity.mime.content.StringBody;
import org.json.JSONArray;
import org.json.JSONObject;
public class FilesUpload
{
public FilesUpload() {
}
public String sendFiles(String s) {
String responseBody = "";
try
{
JSONObject jsonObject = new JSONObject(s);
int len_outer = jsonObject.getJSONArray("electricityExpenseManagement").getJSONObject(0).length();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://yourservername.com/php_file_upload/file_upload2.php");
MultipartEntity reqEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE);
StringBody elecExpObj = new StringBody(s);
reqEntity.addPart("elecExpObj", elecExpObj);
for(int i=0;i<len_outer;i++)
{
int bill_count = jsonObject.getJSONArray("electricityExpenseManagement").getJSONObject(i).getJSONArray("electricityExpenseBillInfoData").length();
for(int j=0;j<bill_count;j++)
{
String sourceFileUri = jsonObject.getJSONArray("electricityExpenseManagement").getJSONObject(0).getJSONArray("electricityExpenseBillInfoData").getJSONObject(j).getString("image_path");
String partName = jsonObject.getJSONArray("electricityExpenseManagement").getJSONObject(i).getJSONArray("electricityExpenseBillInfoData").getJSONObject(j).getString("image_base64_encode");
FileBody bin = new FileBody(new File(sourceFileUri));
reqEntity.addPart(partName, bin);
}
}
httppost.setEntity(reqEntity);
System.out.println("Requesting : " + httppost.getRequestLine());
ResponseHandler<String> responseHandler = new BasicResponseHandler();
responseBody = httpclient.execute(httppost, responseHandler);
System.out.println("responseBody : " + responseBody);
return responseBody;
}
catch (UnsupportedEncodingException e) {
e.printStackTrace();
return e.getMessage();
}
catch (ClientProtocolException e) {
e.printStackTrace();
return e.getMessage();
}
catch (IOException e) {
e.printStackTrace();
return e.getMessage();
}
catch(Exception e){
e.printStackTrace();
System.out.println("error");
return e.getMessage();
}
}
}
メインの Android Java ファイル (プラグイン クラスの定義)
package com.yourpackagename.core;
import android.os.Bundle;
import org.apache.cordova.*;
public class Waterhealth extends DroidGap
{
private FilesUpload f;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
// Set by <content src="index.html" /> in config.xml
super.init();
f = new FilesUpload();
appView.addJavascriptInterface(f, "FilesUpload");
super.loadUrl(Config.getStartUrl());
super.loadUrl("file:///android_asset/elect_exp_FS/index.html");
}
}
ファイルをアップロードするための PHP スクリプト (Web サービス)
<?php
if(isset($_REQUEST['elecExpObj']))
{
$obj= json_decode(stripslashes($_REQUEST['elecExpObj']));
$len = count($obj->electricityExpenseManagement);
$str = "";
for($i=0;$i<$len;$i++)
{
$bill_count = count($obj->electricityExpenseManagement[$i]->electricityExpenseBillInfoData);
for($j=0;$j<$bill_count;$j++)
{
$filePath = $obj->electricityExpenseManagement[$i]->electricityExpenseBillInfoData[$j]->image_base64_encode;
if($_FILES[$filePath]['name'])
{
if(!$_FILES[$filePath]['error'])
{
$new_file_name = $filePath . rand() . ".jpg"; //rename file
move_uploaded_file($_FILES[$filePath]['tmp_name'], 'uploads/'.$new_file_name);
$str .= 'Congratulations! Your file was accepted.';
}
}
}
}
echo $str;
}
else{
echo "fail";
}
?>