wood burning stoves 2.0*
The moose likes Java in General and the fly likes Creating HTTP Post Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Creating HTTP Post" Watch "Creating HTTP Post" New topic
Author

Creating HTTP Post

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Hi! I am creating a HTTP Post using the following code:



But on line 23, it says: cannot find symbol: httpclient.execute().
Also on line 16 and 17, also cannot find symbol: for constructor NameValuePair().

What is the correct way of creating the HTTP post?

Can someone help?

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
You are importing the wrong NameValuePair class - look at the package name.

Declare the client object as DefaultHttpClient, not HttpClient (which has abstract methods only).


Ping & DNS - updated with new look and Ping home screen widget
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

There's an example at HttpClient homepage. It's a GET, but you should be able to figure out how to change it for POST.


[My Blog]
All roads lead to JavaRanch
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Hi Christophe,

Your example is on version 3.1. Now is version 4.0.3. They are different.

Hi Ulf,

I have only 2 options:
1) org.omg.CORBA.NameValuePair
2) org.omg.DynamicAny.NameValuePair

which one is it? I have tried both, both gave me errors. I have also tried:

formparams.add(new BasicNameValuePair("param1", "value1"));
But it says cannot find symbol.

I thought I have already declared the client object as DefaultHttpClient?
HttpClient httpclient = new DefaultHttpClient();

Please advise. Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Line 23 should work, as HttpClient has a method execute(HttpUriRequest request) and HttpPost implements HttpUriRequest.

NameValuePair has two constructors. One takes no arguments, the other takes a String and an org.omg.COBRA.Any. You are providing two Strings. So you need to wrap that second String into an Any. Unfortunately, I see no sub classes for Any so you'll need to create one yourself.

Edit: indeed the wrong NameValuePair. But that would make me think the creation of entity would already cause an error. Odd...


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
Your example is on version 3.1. Now is version 4.0.3. They are different.

Yup, the 4.x URL is http://hc.apache.org/. Somewhere around there are tutorials and code examples.

formparams.add(new BasicNameValuePair("param1", "value1"));
But it says cannot find symbol.

That's the one. Which import did you add to your code?

I thought I have already declared the client object as DefaultHttpClient?
HttpClient httpclient = new DefaultHttpClient();

You created a DefaultHttpClient object, but you declared it to be HttpClient.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Ulf Dittmer wrote:Declare the client object as DefaultHttpClient, not HttpClient (which has abstract methods only).

What about declaring against interfaces? I wouldn't declare it as DefaultHttpClient unless I needed to call methods not declared in HttpClient.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Rob Prime wrote:Line 23 should work, as HttpClient has a method execute(HttpUriRequest request) and HttpPost implements HttpUriRequest.


errmm...it is still giving me "cannot find symbol". Am I right to say that I cast httpPost into HttpUriRequest to make it work?


NameValuePair has two constructors. One takes no arguments, the other takes a String and an org.omg.COBRA.Any. You are providing two Strings. So you need to wrap that second String into an Any. Unfortunately, I see no sub classes for Any so you'll need to create one yourself.

Edit: indeed the wrong NameValuePair. But that would make me think the creation of entity would already cause an error. Odd...


How do I wrap the second String into an Any?

Please advise. Thanks.
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Alan Blass wrote:
errmm...it is still giving me "cannot find symbol". Am I right to say that I cast httpPost into HttpUriRequest to make it work?

Are you compiling with the right JARs in your classpath ? Aren't there any older libraries somewhere ?

(sorry for posting a link to the old library)
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Alan Blass wrote:
Rob Prime wrote:Line 23 should work, as HttpClient has a method execute(HttpUriRequest request) and HttpPost implements HttpUriRequest.


errmm...it is still giving me "cannot find symbol". Am I right to say that I cast httpPost into HttpUriRequest to make it work?

Update your libraries. I downloaded version 4.0.3, made changes to the declaration and instantiation of NameValuePair, and it compiled successfully. Well, apart from an uncaught exception.


NameValuePair has two constructors. One takes no arguments, the other takes a String and an org.omg.COBRA.Any. You are providing two Strings. So you need to wrap that second String into an Any. Unfortunately, I see no sub classes for Any so you'll need to create one yourself.

Edit: indeed the wrong NameValuePair. But that would make me think the creation of entity would already cause an error. Odd...


How do I wrap the second String into an Any?

I you've read my update you'd seen that you used the wrong NameValuePair, and I looked at this wrong one. You need to import org.apache.http.NameValuePair instead, and instantiate as org.apache.http.message.BasicNameValuePair:
After this the creation of entity still fails because an uncaught exception but the other lines compile just fine.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Hi Rob,

I have checked and my libraries are httpclient-4.0.3.jar and httpmime-4.0.3.jar for this project.

The httpclient.execute(httpPost); is still giving me "cannot find symbol". Any ideas?

The formparams.add(new BasicNameValuePair("param2", "value2")); is also giving me "cannot find symbol".

But formparams.add(new NameValuePair("param1", "value1")); is not.

Please advise. Thanks.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19538
    
  16

Did you add all dependencies of HttpClient as well? I'm missing httpcore there, which is definitely in the ZIP file with the dependencies.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
Post the list of import statements. Make sure all classes you're using are covered by those imports, and that none of them is from the org.omg.CORBA.* hierarchy.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Hi!

I downloaded the HttpClient and the dependencies:
Library Files:

httpclient-4.0.3.jar
httpmime-4.0.3.jar
apache-mime4j-0.6.jar
commons-codec-1.3.jar
commons.logging-1.1.1.jar
httpcore-logging-1.1.1.jar

httpclient.execute(httpPost); now works.

My source code now:



But on line 15 and 16, it says "cannot find symbol". Am I missing something here?

In addition, how should I use entity on line 21?

Thanks.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39535
    
  27
But on line 15 and 16, it says "cannot find symbol". Am I missing something here?

Yes, the import statement for BasicNameValuePair. Note that it's in a different package than NameValuePair.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Hi!

How about line 21?

Thanks
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 118
Found it:

httpPost.setEntity(entity);
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating HTTP Post
 
Similar Threads
Client Android JSON--- Server side PhP .....error..!!
Httppost Problem
Apache HTTP Components SSL Request
How do I send a JSON object from the client to the server in a POST request?
How to post data from android app to php page