HttpClient and POST requests

by fala70 » Sat, 22 Nov 2008 22:39:17 GMT


Sponsored Links
 Hi, I've a problem to translate follow code to org.apache.http.*;

           OutputStream pos = new OutputStream();
            pos.writeShort(Const.INVOCATION_CODE);
            pos.writeInt(requestId);
            pos.writeString(nickname);
            pos.writeString(password);

           conn = (HttpConnection) Connector.open(url,
Connector.READ_WRITE);
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Type", "application/octet-
stream");
            conn.setRequestProperty("Accept", "application/octet-stream");

            if(sessionCookie == null)
            {
                conn.setRequestProperty("version", "???");
            }
            else
            {
                conn.setRequestProperty("cookie", sessionCookie);
            }


            // Getting the output stream may flush the headers
            os = conn.openDataOutputStream();
            os.write(pos.getBytes());
            os.close();


start connection I must use :
                HttpClient conn = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

but I've difficult to understand what must I use to set the request
proprieties (setRequestProperty) and use an OutputStream to send my
requests

thanks
--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by Mark Murphy » Sat, 22 Nov 2008 22:47:04 GMT


 


Questions related to the Apache HTTPComponents are best asked of the 
Apache HTTPComponents team:

 http://hc.apache.org/ 

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com 
_The Busy Coder's Guide to Android Development_ Version 1.4 Published!

--~--~---------~--~----~------------~-------~--~----~


Sponsored Links


HttpClient and POST requests

by Adam Connors » Sun, 23 Nov 2008 00:08:47 GMT


 Hope this is useful:

   88:     HttpPost post = null;
   89:     post = new HttpPost(url);
   91:     post.addHeader("Content-Type", "multipart/related;
boundary=\"END_OF_PART\"");
   92:     post.addHeader("MIME-version", "1.0");
   93:
  116:
  117:     HttpEntity body = new ByteArrayEntity(bodyBytes);
  118:     post.setEntity(body);
  121:     HttpResponse resp = uploadClient.execute(post);

Instead of ByteArrayEntity sounds like you want InputStreamEntity or such
like...

*BUT: *In my experience using an InputStreamEntity is bad because over flaky
connections the HttpClient sometimes attempts to resend packets and the
InputStreamEntity is "not-repeatable"... (You'll get periodic
EntityNotRepeatable errors). Which is why I switched to using a
ByteArrayEntity as above which seems to work slightly better for large
streams.

Hope that answers your question,

Best,

Adam.





--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by fala70 » Sun, 23 Nov 2008 04:45:33 GMT


 Hi, I've a problem to translate follow code to org.apache.http.*;

           OutputStream pos = new OutputStream();
            pos.writeShort(Const.INVOCATION_CODE);
            pos.writeInt(requestId);
            pos.writeString(nickname);
            pos.writeString(password);

           conn = (HttpConnection) Connector.open(url,
Connector.READ_WRITE);
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Type", "application/octet-
stream");
            conn.setRequestProperty("Accept", "application/octet-stream");

            if(sessionCookie == null)
            {
                conn.setRequestProperty("version", "???");
            }
            else
            {
                conn.setRequestProperty("cookie", sessionCookie);
            }


            // Getting the output stream may flush the headers
            os = conn.openDataOutputStream();
            os.write(pos.getBytes());
            os.close();


start connection I must use :
                HttpClient conn = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

but I've difficult to understand what must I use to set the request
proprieties (setRequestProperty) and use an OutputStream to send my
requests

thanks

--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by fala70 » Sun, 23 Nov 2008 04:45:40 GMT


 I am using android-sdk-windows-1.0_r1 and need to translate a snip
code to use over android G1. Old is that:

           conn = (HttpConnection) Connector.open(url,
Connector.READ_WRITE);
            conn.setRequestMethod(HttpConnection.POST);
            conn.setRequestProperty("Content-Type", "application/octet-
stream");
            conn.setRequestProperty("Accept", "application/octet-stream");

            if(sessionCookie == null)
            {
                conn.setRequestProperty("version", "???");
            }
            else
            {
                conn.setRequestProperty("cookie", sessionCookie);
            }


            // Getting the output stream may flush the headers
            os = conn.openDataOutputStream();
            os.write(pos.getBytes());
            os.close();


I must used org.apache.http.client.HttpClient; package. The new
connection type will be:
                HttpClient conn = new DefaultHttpClient();
                HttpPost httppost = new HttpPost(url);

but I didn't understand what use to translate setRequestProperty, for
set all my prorprieties.

thanks

--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by fala70 » Sun, 23 Nov 2008 07:24:32 GMT


 thanks Adam, for your answer. I tried your instructions but when I
call the execute I receive an IOException. Follow the stack log. Any
idea ???

Also I tried to use the same Header, but without success.
                httppost.addHeader("Content-Type", "application/octet-
stream");
                httppost.addHeader("Accept", "application/octet-stream");


stack log.

11-22 23:05:43.834: WARN/System.err(478): java.net.SocketException:
unknown error
11-22 23:05:43.843: WARN/System.err(478):     at
org.apache.harmony.luni.platform.OSNetworkSystem.createSocketImpl
(Native Method)
11-22 23:05:43.853: WARN/System.err(478):     at
org.apache.harmony.luni.platform.OSNetworkSystem.createSocket
(OSNetworkSystem.java:79)
11-22 23:05:43.853: WARN/System.err(478):     at
org.apache.harmony.luni.net.PlainSocketImpl2.create
(PlainSocketImpl2.java:59)
11-22 23:05:43.863: WARN/System.err(478):     at
java.net.Socket.checkClosedAndCreate(Socket.java:763)
11-22 23:05:43.873: WARN/System.err(478):     at
java.net.Socket.connect(Socket.java:910)
11-22 23:05:43.873: WARN/System.err(478):     at
org.apache.http.conn.scheme.PlainSocketFactory.connectSocket
(PlainSocketFactory.java:117)
11-22 23:05:43.882: WARN/System.err(478):     at
org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection
(DefaultClientConnectionOperator.java:129)
11-22 23:05:43.892: WARN/System.err(478):     at
org.apache.http.impl.conn.AbstractPoolEntry.open
(AbstractPoolEntry.java:164)
11-22 23:05:43.892: WARN/System.err(478):     at
org.apache.http.impl.conn.AbstractPooledConnAdapter.open
(AbstractPooledConnAdapter.java:119)
11-22 23:05:43.904: WARN/System.err(478):     at
org.apache.http.impl.client.DefaultRequestDirector.execute
(DefaultRequestDirector.java:348)
11-22 23:05:43.914: WARN/System.err(478):     at
org.apache.http.impl.client.AbstractHttpClient.execute
(AbstractHttpClient.java:555)
11-22 23:05:43.914: WARN/System.err(478):     at
org.apache.http.impl.client.AbstractHttpClient.execute
(AbstractHttpClient.java:487)
11-22 23:05:43.923: WARN/System.err(478):     at
org.apache.http.impl.client.AbstractHttpClient.execute
(AbstractHttpClient.java:465)

--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by Adam Connors » Sun, 23 Nov 2008 17:36:38 GMT


 Best guess... Do you have the INTERNET permission correctly declared
in your manifest...?




--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by Guillaume Perrot » Sun, 23 Nov 2008 19:34:03 GMT


  http://hc.apache.org/httpcomponents-client/examples.html 



--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by fala70 » Sun, 23 Nov 2008 22:09:47 GMT


 Yeah.... Thanks Adam now work




--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by Christine » Mon, 24 Nov 2008 05:56:16 GMT


 The examples that Guillaume points you to cover most of what you want
to do in a POST.
What they don't cover is multipart mime messages and setting mime
types. With "application/octet" as the mime type you should be ok with
the examples. Otherwise, do have a look at HttpMime.

On Nov 23, 12:33pm, Guillaume Perrot <[EMAIL PROTECTED]>



--~--~---------~--~----~------------~-------~--~----~



HttpClient and POST requests

by agal » Thu, 18 Dec 2008 01:26:35 GMT


 i,

I would like to know how to set headers in an Http Post request. This
is what I tried:
...
HttpPost postMethod=new HttpPost(posturl);
postMethod.addHeader("Content-Type", "something");
postMethod.addHeader("Content-Length", "length of content");
String requestBody = "some text";
postMethod.setEntity(new StringEntity(requestBody));
...

The server that I'm posting to is rejecting this request because it's
not able to retrieve the content length.

Could anyone help me with this?

Thanks.


On Nov 23, 7:09pm, fala70 <fal...@gmail.com> wrote:

--~--~---------~--~----~------------~-------~--~----~



Other Threads

1. Bluetooth in Android

Hello Everyone..

I want to connect another android device using bluetooth. I have tried
but was nt able to..
Can anyone help me

-- 

2. ANIMATION WITH BUTTON CLICK

hi,  I am new to android and I could nt able to solve this, I want to
create a button and with the click of the button the animation should
happen , whats the wrong with this , please help me with the sample
code

Help is always appreciated
Thanks



package com.transformation;

//import android.R;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.Animation.AnimationListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class Transformation extends Activity implements
AnimationListener {
  LinearLayout layout;
  LinearLayout layout2;
  Animation movement;
  Animation movement2;

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
   final Button button = (Button)findViewById(R.id.widget40);
   button.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {


            public void startAnimation(View v)

                // Get the layouts
                layout = (LinearLayout) findViewById(R.id.linearLayout1);
                layout2 = (LinearLayout) findViewById(R.id.linearLayout2);

                // Create animation for right image
                movement = AnimationUtils.loadAnimation(this,
R.layout.animation_test);
                movement.reset();
                movement.setAnimationListener(this);

                // Create animation for left image
                movement2 = AnimationUtils.loadAnimation(this,
R.layout.animation_test2);
                movement2.reset();
                movement2.setAnimationListener(this);


                // Start animation on each image
                layout2.startAnimation(movement2);
                layout.startAnimation(movement);}

              // Listen for animations to be finished
              // There are more efficient ways, I'm just being lazy.
              public void onAnimationEnd(Animation animation) {
                layout.setVisibility(View.INVISIBLE);
                layout2.setVisibility(View.INVISIBLE);
                // or do whatever it is you wanted to do here, like launch
another activity?
              }

              public void onAnimationRepeat(Animation animation) {
              }

              public void onAnimationStart(Animation animation) {
            }
            });


            }

@Override
public void onAnimationEnd(Animation animation) {
        // TODO Auto-generated method stub

}

@Override
public void onAnimationRepeat(Animation animation) {
        // TODO Auto-generated method stub

}

@Override
public void onAnimationStart(Animation animation) {
        // TODO Auto-generated method stub

}
          };

-- 

3. Streaming Live Video to Android

4. Android JNI + USB serial

5. How to run my applications more efficiently

6. ScheduledExecutorService memory leak question.

7. Setting border for Gridview