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