Friday, April 30, 2010

[android-developers] Re: uploading image + optional tags via HTTP POST

If Android does not have all the classes necessary to compile that,
try include commons-httpclient in your classpath (http://hc.apache.org/
downloads.cgi)

On Apr 30, 11:54 am, shaun <shashepp...@gmail.com> wrote:
> I found this bit of code.  Not put it to use yet, but looks good to
> go.  The "data" object is the byte[] representing the image.
>
>  PostMethod method;
>                 try {
>                         HttpClient client = new HttpClient();
>
> client.getHttpConnectionManager().getParams().setConnectionTimeout(
>                                         5000);
>                         method = new PostMethod(urlString);
>
>                         Part[] parts = {
>                                         new FilePart("videoFile", new
> ByteArrayPartSource(
>                                                         "videoFile",
> data)),
>                                         new StringPart("videoName",
> filename) };
>                         method.setRequestEntity(new
> MultipartRequestEntity(parts, method
>                                         .getParams()));
>                         client.executeMethod(method);
>                         response = method.getResponseBodyAsString();
>                         method.releaseConnection();
>                 } catch (Exception ex) {
>                         Log.v(TAG, "Exception", ex);
>                 } finally {
>                         method.releaseConnection();
>                 }
>
> On Apr 29, 3:25 am, Amit <prabhudesai.a...@gmail.com> wrote:
>
>
>
>
>
> > Hi,
>
> > I am a newbie to Android development; and I want to upload an image
> > (NOT an image file on the device, but a frame from a camera preview)
> > to a web-server. I searched around and got a snippet of code that can
> > be used to upload a byte array. This is the code:
>
> >         public void uploadImage(final byte[] data) {
> >                 // spawn off a new thread to do the image uploading
> >                 new Thread(new Runnable() {
>
> >                         @Override
> >                         public void run() {
> >                                 // TODO Auto-generated method stub
> >                                 Log.d(TAG, "onRun:");
> >                                 HttpURLConnection conn = null;
> >                                 try {
> >                                         serverURL = new URL(URL);
> >                                 } catch (MalformedURLException e) {
> >                                         // TODO Auto-generated catch block
> >                                         Log.e(TAG, "Malformed URL!");
> >                                         e.printStackTrace();
> >                                 }
> >                                 try {
> >                                         // open up a connection with the server
> >                                         conn = (HttpURLConnection) serverURL.openConnection();
>
> >                                         // set up the conection
> >                                         conn.setDoInput(true);
> >                                         conn.setDoOutput(true);
> >                                         conn.setUseCaches(true);
> >                                         conn.setRequestMethod("POST");
> >                                         conn.setRequestProperty("Connection", "Keep-Alive");
>
> >                                         // now transfer the byte-stream
> >                                         DataOutputStream ostream = new
> > DataOutputStream(conn.getOutputStream());
> >                                         ostream.write(data);
> >                                         ostream.flush();
> >                                         ostream.close();
>
> >                                 } catch (IOException e) {
> >                                         // TODO Auto-generated catch block
> >                                         Log.e(TAG, "No response from server!");
> >                                         e.printStackTrace();
> >                                 } finally {
> >                                         conn.disconnect();
> >                                 }
>
> >                                 // wait for server response
> >                                 try {
> >                                         BufferedReader reader = new BufferedReader(
> >                                                         new InputStreamReader(conn.getInputStream())
> >                                                         );
> >                                         String response;
> >                                         while ((response = reader.readLine()) != null ) {
> >                                                 Log.d("server response: ", response);
> >                                         }
> >                                         reader.close();
> >                                 } catch (IOException e) {
> >                                         // TODO Auto-generated catch block
> >                                         e.printStackTrace();
> >                                 }
>
> >                         }
>
> >                 }).start();
> >         }
>
> > First, is this code right, or am I missing something here?
>
> > Next, I want to know how can I simultaneously upload image AND
> > optional image tags to the server?
> > I got to the level that I need to send a multi-part request, but can't
> > seem to figure out the specifics. Can someone help me?
>
> > Also are there some 'best practices' to follow while doing this (like
> > for responsiveness, etc)?
>
> > Any help is much appreciated!
>
> > Thanks,
> > Amit
>
> > PS: Although this may be a little out-of-bounds for this forum, but
> > I'm also writing the server -- any help/ pointers on how to write it
> > to handle this POST request that the client sends? Got to write the
> > server in ASP.NET
> > Thanks again,
> > amit
>
> > --
> > You received this message because you are subscribed to the Google
> > Groups "Android Developers" group.
> > To post to this group, send email to android-developers@googlegroups.com
> > To unsubscribe from this group, send email to
> > android-developers+unsubscribe@googlegroups.com
> > For more options, visit this group athttp://groups.google.com/group/android-developers?hl=en
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group athttp://groups.google.com/group/android-developers?hl=en- Hide quoted text -
>
> - Show quoted text -

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment