Hey Delta Foxtrot, I agree with you on rolling your own. I've got a multipart form upload class that populates form and uploads data (in this case picture), however in android this code just doesn't work. It works every time from any other java client - never fails, but on android, it looks like it writes to the connection and then when you go to read the response, it hangs, times out and the response code is -1
I've looked at your code and it seems I'm doing everything you are doing now (I added a user agent for grins) but no joy. All of the HttpGets I do (restful calls) on android to may service work great, but the post and MPF, not so much... Is there any setup android requires to make a post/multipart file upload work that you didn't specify in your example? thanks jos On Jun 26, 8:53 pm, Delta Foxtrot <[email protected]> wrote: > As I said, multipart uploads aren't hard to do your own function for. > > private void UploadFile(String FileName) > { > try > { > String lineEnd = "\r\n"; > String twoHyphens = "--"; > String boundary = "*****MyMultiPartBoundary"; > > File sdcard = Environment.getExternalStorageDirectory(); > File mydir = new File(sdcard, "MyFileDir"); > File file = new File(mydir, FileName); > > FileInputStream fileInputStream = new FileInputStream(file); > > String UPLOADSCRIPT_URL = > "http://www.example.com/upload.php?username=example&password=example"; > System.out.println("Opening: "+UPLOADSCRIPT_URL); > URL url = new URL(UPLOADSCRIPT_URL); > HttpURLConnection conn = (HttpURLConnection)url.openConnection(); > > conn.setDoInput(true); > conn.setDoOutput(true); > conn.setUseCaches(false); > > conn.setRequestMethod("POST"); > conn.setRequestProperty("User-Agent", > "MyExampleApp/"+appversion); > conn.setRequestProperty("Content-Type", > "multipart/form-data;boundary="+boundary); > conn.connect(); > DataOutputStream dos = new > DataOutputStream(conn.getOutputStream()); > dos.writeBytes(twoHyphens + boundary + lineEnd); > dos.writeBytes("Content-Disposition: form-data; > name=\"uploadedfile\";filename=\""+ > FileName+"\"" + lineEnd); > dos.writeBytes(lineEnd); > > int bytesAvailable; > > while((bytesAvailable = fileInputStream.available()) > 0) > { > int bufferSize = Math.min(bytesAvailable, 4096); > byte[] buffer = new byte[bufferSize]; > int bytesRead = fileInputStream.read(buffer, 0, > bufferSize); > dos.write(buffer, 0, bytesRead); > } > > dos.writeBytes(twoHyphens + boundary + twoHyphens + > lineEnd); > fileInputStream.close(); > dos.flush(); > dos.close(); > DataInputStream dis = new > DataInputStream(conn.getInputStream()); > byte[] data = new byte[1024]; > len = dis.read(data, 0, 1024); > dis.close(); > int respcode = conn.getResponseCode(); > System.out.println("done uploading! resp code = "+respcode); > String respstr = "FAILED"; > if(len > 0) > respstr = new String(data, 0, len); > > if(respcode == 200 && respstr.equals("OK")) > System.out.println("Uploaded "+FileName+" successfully!"); > else > System.out.println("Upload Error: respcode="+respcode+", > respstr="+respstr); > } catch (Exception e) { > System.out.println("upload error: "+e.toString()); > } > > > > } --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en -~----------~----~----~----~------~----~------~--~---

