I Also have problem uploading to a server with new HttpClient v.4
I am trying to upload a file same as the example in the httpclient
docs.
but it doesn't work and I get NullPointerException when I execute the
post method:
HttpClient client = new DefaultHttpClient();
post = new HttpPost(myServletAddress);
File file = new File(filepath);
FileEntity entity;
if (filepath.substring(filepath.length()-3, filepath.length
()).equalsIgnoreCase("txt") ||
filepath.substring(filepath.length()-3, filepath.length
()).equalsIgnoreCase("log")) {
entity = new FileEntity(file,"text/plain;
charset=\"UTF-8\"");
}else {
entity = new FileEntity(file,"binary/octet-stream");
}
post.setEntity(entity);
HttpResponse response = httpClient.execute(post);
On Nov 9, 9:43 pm, Mark Hansen <[email protected]> wrote:
> Gah let me try posting this in the correct forum...
>
> I'm working on an application that does an upload to a webserver, but
> struggling with the actual fileupload.. code:
>
> byte[] data = params[0]; // this is my picture data in a ByteArray
>
> HttpClienthttpClient= new DefaultHttpClient();
>
> HttpPost request = new HttpPost("http://www.someurl.com/upload.aspx");
>
> List<BasicNameValuePair> nameValuePairs = new
> ArrayList<BasicNameValuePair>();
> nameValuePairs.add(new BasicNameValuePair
> ("filename","droidUpload.jpg"));
> nameValuePairs.add(new BasicNameValuePair("userfile", ?????) // how do
> I add my byte array, obviously can't go here.
>
> request.setEntity(new UrlEncodedFormEntity(nameValuePairs));
>
> Now, I have this working with what is no longer supported in
> MultipartEntity as follows:
>
> MultipartEntity entity = new MultipartEntity();
> entity.addPart("filename",new StringBody("droidUpload.jpg"));
> entity.addPart("userfile[]", new InputStreamBody(new
> ByteArrayInputStream(data), "droidUpload.jpg"));
> request.setEntity(entity);
>
> Problem is MultipartEntity is apparently no longer supported, although
> I found the libraries I can include in my project, so I'm hoping I"m
> just missing something stupid here.
--
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