0
我嘗試將圖像(base64編碼)與其他值一起上傳到PHP網絡服務器,PHP網絡服務器接收此信息並執行一些進一步處理。使用HttPost和HttpClient上傳base64編碼圖像
以下功能是執行請求的AsyncTask的一部分。
我手動構建postData連接不同的值。
JsonUrlLook類幫助我找到發送數據的正確URL。我證實了它。它是正確的。
當我嘗試執行創建的HttpPost時,我的響應爲空。任何人有一個想法,爲什麼這是?
private String getPostData(String arrayString, String base64Image, String boundary) {
String remoteMethod = "uploadPic";
String postData = "";
// Add remote method name
postData += "--" + boundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"" + boundary + "[method]\"\r\n\r\n";
postData += remoteMethod;
postData += "--" + boundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"" + boundary + "[params]\"\r\n\r\n";
postData += arrayString;
postData += "--" + boundary + "\r\n";
postData += "Content-Disposition: form-data; name=\"" + boundary + "\"\r\n\r\n";
postData += "filename=\"image.jpg\"\r\n\r\n";
postData += base64Image;
postData += "--" + boundary + "--";
return postData;
}
private String uploadPicture(String arrayString, String base64Image) {
String boundary = "tx_fpoemobile_pi2";
// Get post data
String postData = getPostData(arrayString, base64Image, boundary);
// Get webservice uri
JsonUrlLookup lookup = new JsonUrlLookup(context);
URL webserviceUrl = lookup.buildUrl(context.getResources().getString(R.string.page_uid_webservice));
URI webserviceUri = null;
try {
webserviceUri = new URI(webserviceUrl.toString());
} catch(Exception e) {
// TODO
}
// Build client and post
HttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(webserviceUri);
// Set header of post
httpPost.setHeader("Content-Type", "multipart/form-data; boundary=" + boundary);
httpPost.setHeader("Content-Length", String.valueOf(postData.length()));
// Set body
try {
httpPost.setEntity(new StringEntity(postData));
} catch (Exception e) {
// TODO
}
HttpResponse response = null;
try {
response = httpClient.execute(httpPost);
} catch (ClientProtocolException e) {
// TODO
} catch (IOException e) {
// TODO
}
return "uploadPictureSuccess";
}
我說,我需要將圖像傳送作爲base64編碼字符串,而不是二進制。但無論如何,我會嘗試你的解決方案。 – Klaus 2014-10-16 13:06:15
你介意解釋一下函數「writeParamData」嗎? – Klaus 2014-10-16 13:20:38
在你的文章「獲得回覆」中,你使用了一個httpClient和一個具有設置實體的httpPost。這與第一部分有很大不同。他們連接,還是完全不同的認同?我沒有明白。 – Klaus 2014-10-16 13:23:32