2011-03-15 6 views
3

Je veux envoyer un formulaire multipart dans mon application Android, mais sans utiliser org.apache.http.entity.mime, donc j'ai créé ma propre voie, mais cela ne fonctionne pas le manière dont je crée la demande:Envoi de formulaire multipart dans android par 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; 
} 

lorsque la méthode est appelée les informations du journal montrent que ByteArrayOutputStream ont une taille 0 au moment où j'ajoute imageData donc ressembler à la OutputStreamWriter ne pas écrire des chaînes , ou ai-je tort?

Des suggestions ou des solutions?

+0

pourquoi appelez-vous la chasse d'eau() sur l'auteur de sortie ** après ** votre message de journal? – HefferWolf

+0

En fait, le problème est résolu, je ne sais pas pourquoi il ne m'écrit rien, je veux dire outputStreamWriter – Robert

Répondre

5

Si quelqu'un va chercher réponse est ici comment je l'ai fait:

private byte[] generatePhotoRequest(byte[] imagedata){ 
    byte[] requestData = null; 
    ByteArrayOutputStream bufer = new ByteArrayOutputStream(); 
    DataOutputStream dataOut = new DataOutputStream(bufer); 
    try{ 
     dataOut.writeBytes("--"); 
     dataOut.writeBytes(BOUNDARY); 
     dataOut.writeBytes("\r\n"); 
     dataOut.writeBytes("Content-Disposition: form-data; name=\"auth\"; filename=\"auth\"\r\n"); 
     dataOut.writeBytes("Content-Type: text/xml; charset=utf-8\r\n"); 
     dataOut.writeBytes("\r\n"); 
     dataOut.write(generateAuth()); 
     dataOut.writeBytes("\r\n--" + BOUNDARY + "\r\n"); 
     dataOut.writeBytes("Content-Disposition: form-data; name=\""+CIMAGE+"\"; filename=\""+CIMAGE+"\"\r\n"); 
     dataOut.writeBytes("Content-Type: "+IMAGE_PNG+"\r\n"); 
     dataOut.writeBytes("\r\n"); 
     bufer.write(imagedata); 
     dataOut.writeBytes("\r\n"); 
     dataOut.writeBytes("\r\n--" + BOUNDARY + "--\r\n"); 

     requestData = bufer.toByteArray(); 
    } catch(IOException ex){ 
     ex.printStackTrace(); 
    } finally{ 
     if(bufer!=null){ 
      try { 
       bufer.close(); 
      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    } 
    return requestData; 
}