Here is the code I use and I have never had any issue out of it.

HttpURLConnection hucConnection = null;

try
{
URL uInfo = new URL( YOUR_URL_HERE );
hucConnection = (HttpURLConnection)uInfo.openConnection();
hucConnection.setRequestMethod( "POST" );
hucConnection.setRequestProperty( "Accept-Encoding", "identity" );
hucConnection.setDoOutput( true );

String sPost = URLEncoder.encode( "key1", "UTF-8") + "=" + 
URLEncoder.encode( "value1", "UTF-8");
sPost += "&" + URLEncoder.encode( "key2", "UTF-8") + "=" + 
URLEncoder.encode( "value1", "UTF-8");
sPost += "&" + URLEncoder.encode( "key3", "UTF-8") + "=" + 
URLEncoder.encode( "value1", "UTF-8");

OutputStreamWriter out = new OutputStreamWriter( 
hucConnection.getOutputStream() );
out.write( sPost );
out.flush();

int iSize = hucConnection.getContentLength();
byte[] bReturn = new byte[ iSize ];

InputStream in = new BufferedInputStream( hucConnection.getInputStream() );
in.read( bReturn, 0, iSize );

String sReturn = new String( bReturn );
Log.i( "URL Return", "Return: " + sReturn );
}
catch( Exception e ) { e.printStackTrace(); }
finally { hucConnection.disconnect(); }

I use the accept-encoding line to disable compression because I don't send 
large amounts of data via the connection and it simply (and speeds up) 
processing the return. See the note in the following link for more 
information.

Depending on what platform version you are aiming for, you may want to check 
out the following link:

http://android-developers.blogspot.com/2011/09/androids-http-clients.html

It will give you more detail about what they recommend.

Steven
Studio LFP
http://www.studio-lfp.com


On Saturday, October 15, 2011 6:44:34 AM UTC-5, aparna rani wrote:
>
> hii all. i am trying to connect to my database using url connection is 
> working fine.but i am using httpclient its not working.
> my URL is  *http://122.166.229.173:8080/indela/ServletDataBase*
> in server side i wrote program in servelets.
>
>
> this code is corking fine. getting results also.
>
> *URL url = new URL(FETCH_FRIEND_UPDATES_URI);*
> *            URLConnection connection = url.openConnection();*
> *            connection.setDoInput(true);*
> *            connection.setDoOutput(true);*
> *            connection.setUseCaches(false);*
> *            connection.setRequestProperty("Content-Type", 
> "application/x-www-form-urlencoded");*
> *            DataInputStream dataIn = new 
> DataInputStream(connection.getInputStream());*
> *            String inputLine;*
> *            String result="";*
> *            while ((inputLine = dataIn.readLine()) != null) {*
> *            result += inputLine;*
> *            }*
> *            dataIn.close();*
> *            *
> this code is not getting results.
> *
> *
> *
>  HttpEntity entity = null;
>             entity = new UrlEncodedFormEntity(params);
>             final HttpPost post = new HttpPost(FETCH_FRIEND_UPDATES_URI);
>             post.addHeader(entity.getContentType());
>             post.setEntity(entity);
>             maybeCreateHttpClient();
>
>             final HttpResponse resp = mHttpClient.execute(post);
>             final String response = EntityUtils.toString(resp.getEntity());
>
> public static void maybeCreateHttpClient() {
>         if (mHttpClient == null) {
>             mHttpClient = new DefaultHttpClient();
>             final HttpParams params = mHttpClient.getParams();
>             HttpConnectionParams.setConnectionTimeout(params,
>                 REGISTRATION_TIMEOUT);
>             HttpConnectionParams.setSoTimeout(params, 
> REGISTRATION_TIMEOUT);
>             ConnManagerParams.setTimeout(params, REGISTRATION_TIMEOUT);
>         }
>     }
> actually i my application i need to pass some parameters to the server side 
> table because whereClause .
>
> please give me some code to connect to my url.
>
> thanks in advance.....
>
> *
>

-- 
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+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to