3
我想在我的Android应用程序中发送多部分窗体,但不使用org.apache.http.entity.mime所以我创建了mu自己的方式,但它不起作用办法我是如何创建的请求:在Android中通过HttpURLConnection(POST)发送多部分窗体
public byte[] createRequest(byte[] imagedata){
byte[] requestData = null;
ByteArrayOutputStream buf = new ByteArrayOutputStream();
OutputStreamWriter output = null;
try {
Log.i(TAG, "Creating request");
output = new OutputStreamWriter(buf, "UTF-8");
output.write("--");
output.write(boundary);
output.write("\r\n");
output.write("Content-Disposition: form-data; name=\"auth\"; filename=\"auth\"\r\n");
output.write("Content-Type: text/xml; charset=utf-8\r\n");
output.write("\r\n");
byte temp2[] = buf.toByteArray();
Log.i(TAG, "BUF SIZE: " + temp2.length);
Log.i(TAG, "BUF: " + buf.toString());
ByteArrayOutputStream authBuffer = new ByteArrayOutputStream();
OutputStreamWriter authOut = new OutputStreamWriter(authBuffer, "UTF-8");
writeAuthRequestFragment(user, pass, company_id, "RESLINK CLIENT", "2.0", null, null, null, authOut);
buf.write(authBuffer.toByteArray());
Log.i(TAG, "BUF SIZE: " + buf.size());
output.write("\r\n--" + boundary + "\r\n");
output.write("Content-Disposition: form-data; name=\""+CIMAGE+"\"; filename=\""+CIMAGE+"\"\r\n");
output.write("Content-Type: "+IMAGE_PNG+"\r\n");
output.write("\r\n");
buf.write(imagedata);
output.write("\r\n--" + boundary + "--\r\n");
requestData = buf.toByteArray();
Log.i(TAG, "WHOLE SIZE " + requestData.length);
output.flush();
} catch(IOException ex){
ex.printStackTrace();
}
finally{
if(buf!=null){
try {
buf.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if(output!=null){
try {
output.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return requestData;
}
当方法被调用日志信息显示,ByteArrayOutputStream有大小为0的那一刻,当我加入的图象 - 所以它看起来像使用OutputStreamWriter不写串,还是我错了?
任何建议或解决方案?
为什么你调用flush()在输出作家** **后您的日志信息? – HefferWolf 2011-03-15 08:35:43
其实问题解决了,我只是不知道为什么它不写我什么我的意思是outputStreamWriter – Robert 2011-03-15 10:20:43