この形式でレポートを送信するように指示されました
Name=tan ah kow
Email=myemail@google.com
Contact=81234567
Block=123
Street=Tampines Central
Description=Dirty water puddle
uploadedfile=a5fa23423-4907098 (BASE64-ENCODED STRING)
index=1
HTTP ヘッダーの「Content-Type」を「multipart/form-data;boundary=BOUNDARY_STRING」に設定します。
multipart/form-data FORMAT IS の HTTP POST 仕様に従って、HTTP 本文メッセージを形成します。
--BOUNDARY_STRING\r\n
Content-Disposition: form-data; name=\"Name\"\r\n\r\n
tan ah kow\r\n
--BOUNDARY_STRING\r\n
Content-Disposition: form-data; name=\"Email\"\r\n\r\n
myemail@google.com\r\n
...
...
...
--BOUNDARY_STRING\r\n
Content-Dispostion: form-data; name=\"uploadedfile\"; filename=\"android.png\"\r\n
Content-Type: MIME-TYPE\r\n
Content-Transfer-Encoding: binary\r\n\r\n
a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098a5fa23423-4907098\r\n
--BOUNDARY_STRING--
アップロードされた写真が 1 つだけの場合は、1 つの http 投稿を送信し、index=1 を設定します。写真が 2 枚ある場合は、同じデータ (名前、電子メール、連絡先など) の 2 つの HTTP 投稿を送信します。ただし、2 番目の写真のデータである Uploadfile を除き、index=2 を設定します。写真が 3 枚ある場合は、3 枚目の写真のデータである uploadfile を除いて、同じデータ (名前、電子メールなど) の 3 つの http 投稿を送信し、index=3 を設定します。
サーバーは、正しいレポートへの写真のリンクを処理します。
画像をサーバーに送信するための次のコードが機能しませんでした。誰でも私を助けてくれることを願っています。
String urlFEEDBACK = "http://zendragon.sg/tampinestc/report.php";
private class NewFeedbackReportAsyncTask extends AsyncTask<HashMap<String, String>, Void, String> {
@Override
protected void onPreExecute () {
}
@Override
protected String doInBackground(HashMap<String, String>... arrHashMapParameters) {
HashMap<String, String> hashmapInput = arrHashMapParameters[0];
String strPost = "source=android";
Iterator<Entry<String, String>> it = hashmapInput.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<String, String> pairs = (Map.Entry<String, String>)it.next();
strPost += "&"+(pairs.getKey() + "=" + pairs.getValue());
it.remove();
}
String strFileData = convertPhotoToString(bmp);
String result = null;
try {
result = multipartRequest(urlFEEDBACK, strPost, strFileData);
} catch (Exception e) {
Toast.makeText(getApplicationContext(), e.toString(), Toast.LENGTH_LONG).show();
}
return result;
}
@Override
protected void onPostExecute(String result) {
if (progressDialog.isShowing()) {
progressDialog.dismiss();
}
showResults();
}
}
このコードは、ビットマップを BASE64-ENCODED 文字列に変換するためのものです
protected String convertPhotoToString(Bitmap theBitMap) {
if(theBitMap == null) {
return null;
}
ByteArrayOutputStream bos = new ByteArrayOutputStream();
theBitMap.compress(CompressFormat.JPEG, 60, bos);
byte[] array = bos.toByteArray();
return android.util.Base64.encodeToString(array, android.util.Base64.NO_WRAP);
}
これは、サーバーに送信されるコードです。
public String multipartRequest(String urlFEEDBACK2, String strPost, String strFileData) throws ParseException, IOException{
HttpURLConnection connection = null;
DataOutputStream outputStream = null;
InputStream inputStream = null;
String twoHyphens = "--";
String lineEnd = "\r\n";
String boundary = "BOUNDARY_STRING";
String result = "";
try {
URL url = new URL(urlFEEDBACK2);
connection = (HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.setDoOutput(true);
connection.setUseCaches(false);
connection.setRequestMethod("POST");
connection.setRequestProperty("Connection", "Keep-Alive");
connection.setRequestProperty("User-Agent",
"Android Multipart HTTP Client 1.0");
connection.setRequestProperty("Content-Type",
"multipart/form-data; boundary=" + boundary);
outputStream = new DataOutputStream(connection.getOutputStream());
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Name\""
+ lineEnd + lineEnd + strName + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Email\""
+ lineEnd + lineEnd + strEmail + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Contact\""
+ lineEnd + lineEnd + strPhoneNo + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Block\""
+ lineEnd + lineEnd + strBLK + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Street\""
+ lineEnd + lineEnd + strStreet + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"Description\""
+ lineEnd + lineEnd + strDescription + lineEnd);
outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\"; filename=\"android.png\"" + lineEnd);
outputStream.writeBytes("Content-Type: image/png" + lineEnd);
outputStream.writeBytes("Content-Transfer-Encoding: binary" + lineEnd);
outputStream.writeBytes(lineEnd);
if(bmp!=null) {
//EDITED
outputStream.writeBytes(strFileData);
//ByteArrayOutputStream baos = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.PNG, 100, baos);
//baos.writeTo(outputStream);
//baos.close();
}
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(twoHyphens + boundary + twoHyphens);
inputStream = connection.getInputStream();
result = this.convertStreamToString(inputStream);
// Clean up
inputStream.close();
outputStream.flush();
outputStream.close();
// Log see debug
Log.d(getString(R.string.app_name), "Result: "+ result);
checkResponse(result);
return result;
} catch (Exception e) {
Log.e("MultipartRequest", "Multipart Form Upload Error");
e.printStackTrace();
return "error";
}
}
*編集: * 画像を Web サーバーに送信/書き込むためのコードに問題がないようです。ここで私の間違いを誰かが指摘できることを願っています。上司が私に尋ね続けるので、私はこれをできるだけ早く解決する必要があります. 誰かがここで私を助けてくれることを願っています。どんな助けでも感謝します。前もって感謝します。