Post data to a server and get response

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

Post data to a server and get response

Postby manish » Mon Jun 01, 2009 8:22 am

Hi,

I want to post some data on the server and get response. Please share some example of this.
manish
Junior Developer
Junior Developer
 
Posts: 21
Joined: Thu Feb 05, 2009 1:06 pm

Top

Re: Post data to a server and get response

Postby manish » Thu Jun 04, 2009 12:28 pm

Hi,

I am able to get data using the following code:
private InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;

URL url = new URL(urlString);
URLConnection conn = url.openConnection();

if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");

try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();

response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}

but I am not able to post data and then get response. I have found a piece to post the data but I am not able to extract the response send by the server.

HttpClient httpClient = new DefaultHttpClient();
HttpPost postMethod = new HttpPost(mUrl);
try {
// prepare parameters
HttpParams params = new BasicHttpParams();
params.setParameter("locationJSON", mLocationJSON.toString());
postMethod.setParams(params);
HttpResponse response =httpClient.execute(postMethod);

Please help me.

manish wrote:Hi,

I want to post some data on the server and get response. Please share some example of this.
manish
Junior Developer
Junior Developer
 
Posts: 21
Joined: Thu Feb 05, 2009 1:06 pm

Top

Return to Networking & Database Problems

Who is online

Users browsing this forum: Google [Bot] and 4 guests