0
다른 값과 함께 이미지 (base64로 인코딩 됨)를 PHP 웹 서버에 업로드하려고합니다. PHP 웹 서버는이 정보를 취하여 추가 처리를 수행합니다.HttPost 및 HttpClient를 사용하여 base64로 인코딩 된 이미지 업로드
folowing 함수는 요청을 실행하는 AsyncTask의 일부입니다.
다른 값을 직접 연결하여 postData를 작성합니다.
JsonUrlLook 클래스는 데이터를 전송할 올바른 URL을 찾는 데 도움이됩니다. 나는 그것을 확인했다. 맞습니다.
생성 된 HttpPost를 실행하려고하면 내 응답이 null입니다. 왜 이거지?
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";
}
httpmime-4.2.1-1.jar
가 나는 바이너리로 base64로 인코딩 된 문자열로 이미지를 전송하지 할 필요가 있다고합니다. 하지만 여하튼, 나는 당신의 해결책을 시도 할 것입니다. – Klaus"writeParamData"함수를 설명해 주시겠습니까? – Klaus
"응답 받기"게시물에서 httpClient와 httpPost를 설정 엔티티와 함께 사용합니다. 이것은 첫 번째 부분과 많이 다릅니다. 그들은 서로 연결되어 있습니까, 아니면 완전히 다른 것입니까? 나는 그것을 얻지 않는다. – Klaus