我尝试将图像(base64编码)与其他值一起上传到PHP网络服务器,该服务器获取此信息并进行进一步处理。
以下函数是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";
}
答案 0 :(得分:0)
你必须像这样工作
public void connectForMultipart() throws Exception {
con = (HttpURLConnection) ( new URL(url)).openConnection();
con.setRequestMethod("POST");
con.setDoInput(true);
con.setDoOutput(true);
con.setRequestProperty("Connection", "Keep-Alive");
con.setRequestProperty("Content-Type", "multipart/form-data; boundary=" + boundary);
con.connect();
os = con.getOutputStream();
}
public void addFormPart(String paramName, String value) throws Exception {
writeParamData(paramName, value);
}
public void addFilePart(String paramName, String fileName, byte[] data) throws Exception {
os.write( (delimiter + boundary + "\r\n").getBytes());
os.write( ("Content-Disposition: form-data; name=\"" + paramName + "\"; filename=\"" + fileName + "\"\r\n" ).getBytes());
os.write( ("Content-Type: application/octet-stream\r\n" ).getBytes());
os.write( ("Content-Transfer-Encoding: binary\r\n" ).getBytes());
os.write("\r\n".getBytes());
os.write(data);
os.write("\r\n".getBytes());
}
public void finishMultipart() throws Exception {
os.write( (delimiter + boundary + delimiter + "\r\n").getBytes());
}
httpPost.setEntity(entity);
HttpResponse response = httpClient.execute(httpPost);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String sResponse;
while ((sResponse = reader.readLine()) != null)
{
s = s.append(sResponse);
}
if(response.getStatusLine().getStatusCode() == HttpStatus.SC_OK)
{
return s.toString();
}else
{
return "{\"status\":\"false\",\"message\":\"Some error occurred\"}";
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
代替图像,您可以发送任何内容。
在这种情况下,您需要httpmime-4.2.1-1.jar