0
    String pathToOurFile = "/sdcard/DCIM/Camera/foto.jpg";
    String urlServer = "http://server/upload.php";        
    String lineEnd = "\r\n";
    String twoHyphens = "--";
    String boundary =  "*****";        
    .
    .
    .
    URL url = new URL(urlServer);
    connection = (HttpURLConnection) url.openConnection();

    // Allow Inputs & Outputs
    connection.setDoInput(true);
    connection.setDoOutput(true);
    connection.setUseCaches(false);

    // Enable POST method
    connection.setRequestMethod("POST");

    connection.setRequestProperty("Connection", "Keep-Alive");
    connection.setRequestProperty("Content-Type", "multipart/form-data;boundary="+boundary);

    outputStream = new DataOutputStream( connection.getOutputStream() );
    outputStream.writeBytes(twoHyphens + boundary + lineEnd);
    outputStream.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\"" + pathToOurFile +"\"" + lineEnd);
    outputStream.writeBytes(lineEnd);

with the code above, i upload a image to server, but not how to pass parameters string type via post

anyone knows?

  • check this http://stackoverflow.com/questions/2253061/secure-http-post-in-android and this http://moazzam-khan.com/blog/?tag=android-http-post link consider using HttpGet and HttpPost classes – Sergey Benner Jan 17 '12 at 19:45
  • Try my way: [Android - HttpURLConnection parameters from Map via POST method](http://stackoverflow.com/questions/33448973/android-httpurlconnection-parameters-from-mapstring-object-via-post-method) – Lal Krishna Oct 31 '15 at 07:01

2 Answers2

0

I found this blog useful for writing some multipart form data code:
http://blog.rafaelsanches.com/2011/01/29/upload-using-multipart-post-using-httpclient-in-android/

Don't forget to change the boundary variable to match what you specify in the HttpURLConnection that you're using to send the multipart form to the server.

Dan J
  • 25,433
  • 17
  • 100
  • 173
0

for each parameter, where paramName=paramData:

outputStream.writeBytes(twoHyphens + boundary + lineEnd);
outputStream.writeBytes('Content-Disposition: form-data; name="paramName"' + lineEnd);
outputStream.writeBytes(lineEnd);
outputStream.writeBytes(paramData);
elijah
  • 2,904
  • 1
  • 17
  • 21
  • I used String param="valor"; outputStream.writeBytes(twoHyphens + boundary + lineEnd); outputStream.writeBytes("Content-Disposition: form-data; name=\"param\"" + lineEnd); outputStream.writeBytes(lineEnd); outputStream.writeBytes(param); and does not work, 'll know it's wrong? – Jonathan García Jan 17 '12 at 20:12
  • does it work when you remove the file submission? Does the file need to be submitted after the param data perhaps? – elijah Jan 17 '12 at 20:22
  • done! put parameters string type after of file type parameter – Jonathan García Jan 17 '12 at 21:47