hey guys,

 Could you please help me the debug the below code, I am getting response
code 405



HttpClient client = new DefaultHttpClient();
String data = "valid post request";
HttpPost httpPost = new HttpPost("url");
Log.d("inside", "created http post");
try {
          ByteArrayInputStream baos = new
ByteArrayInputStream(data.getBytes());

Log.d("inside", "firing request...");
HttpResponse httpResponse = client.execute(httpPost);
Log.d("inside", "request sent" +
httpResponse.getStatusLine().getStatusCode());
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

Any help is greatly appreciated!!!
On Sun, May 2, 2010 at 10:32 AM, Amit <[email protected]> wrote:

> Thanks, @shaun!
>
> I came across another thread someone had started with some code; and I
> tweaked that around and have seemed to got it working.
>
> Will post the code; it's very similar to the one you've put in.
>
>
> On Apr 30, 8:57 pm, shaun <[email protected]> wrote:
> > 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 <[email protected]> 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 <[email protected]> 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
> [email protected]
> > > > To unsubscribe from this group, send email to
> > > > [email protected]<android-developers%[email protected]>
> > > > 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
> [email protected]
> > > To unsubscribe from this group, send email to
> > > [email protected]<android-developers%[email protected]>
> > > 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 [email protected]
> > To unsubscribe from this group, send email to
> > [email protected]<android-developers%[email protected]>
> > 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 [email protected]
> To unsubscribe from this group, send email to
> [email protected]<android-developers%[email protected]>
> For more options, visit this group at
> http://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 [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

Reply via email to