Posting data over HttpConnection

Problems with WiFi, SQLite ,Bluetooth, WiMax, Proxies, etc...

Posting data over HttpConnection

Postby joemarshalmathew » Wed Feb 10, 2010 7:19 pm

Hi all,
I am trying to create a new application which posts data to a server.The code which i used is

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. URL url;
  2. try {
  3.         url = new URL(UPLOAD_URL);
  4.         HttpURLConnection con = (HttpURLConnection) url.openConnection();
  5.         con.setDoOutput(true);
  6.         con.connect();
  7.         OutputStream os = con.getOutputStream();
  8.         DataOutputStream daos = new DataOutputStream(os);
  9.         byte[] by = getdata().getBytes();
  10.         daos.write(by);
  11.         daos.flush();
  12.         daos.close();
  13. } catch (MalformedURLException e) {
  14.         Log.e("Test_And", "Error at Data Sending MalformedURLException"+e.getMessage());
  15. } catch (IOException e) {
  16.         Log.e("Test_And", "Error at Data Sending IOException"+e.getMessage());
  17. }
Parsed in 0.015 seconds, using GeSHi

The problem is that when executing i am gettinag an exception at
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. con.setDoOutput(true);
Parsed in 0.011 seconds, using GeSHi
saying connection already established.
in the documentation for the method
it written like
Sets the flag indicating whether this URLConnection allows output. It cannot be set after the connection is established.

I found this code from this forum but i dont know how it works for others.

I had added the permissions in the manifest file.

Can anyone help me to sort this problem.
The data cannot be sent as parameters along with URL.

Thanks in advance.
Last edited by joemarshalmathew on Thu Feb 11, 2010 3:28 pm, edited 1 time in total.
Posts: 9
Joined: Sun Jan 03, 2010 6:25 am
Location: Kochi,India


Postby mitutitu » Thu Feb 11, 2010 5:38 am


I can see there is no logical problem in your code. In fact I already used HttpURLConnection method to post data. I've not found such type of error. You can dig a bit deep tp your code to find out what is going on actually or you can post the details of your code or PM me then I can check it.

You may try HTTPPost for posting your data to server.

Android is awesome!
User avatar
Senior Developer
Senior Developer
Posts: 113
Joined: Tue Jun 09, 2009 7:40 am
Location: St. John's, NL, Canada


Return to Networking & Database Problems

Who is online

Users browsing this forum: No registered users and 1 guest