Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get data from an internet link in Android

I am making an application which takes a URL with. *.asp extension and we pass it the required parameters and get some string result using POST method.

Any suggestions on how to achieve this?

UPDATED:

Actually I have a .net link which takes some POST Parameters and gives me a Result. How can I do that in Android?

like image 799
Shah Avatar asked Jun 07 '11 07:06

Shah


2 Answers

HTTPResponse should do the trick:

DefaultHttpClient  httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoururl.com");

List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1); <!-- number should be the amount of parameters
nameValuePairs.add(new BasicNameValuePair("nameOfParameter", "parameter"));
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

HttpResponse response = httpclient.execute(httppost);
HttpEntity ht = response.getEntity();

BufferedHttpEntity buf = new BufferedHttpEntity(ht);

InputStream is = buf.getContent();

Now you got a stream to work with, to write the data to a string:

BufferedReader r = new BufferedReader(new InputStreamReader(is));

total = new StringBuilder();
String line;
while ((line = r.readLine()) != null) {
     total.append(line);
}

Good luck!

like image 106
BadSkillz Avatar answered Nov 02 '22 14:11

BadSkillz


try using an AsynTask ( http://developer.android.com/reference/android/os/AsyncTask.html ) and never try to make any time consuming tasks in the onCreate call or any other GUI thread method.

like image 20
Moss Avatar answered Nov 02 '22 14:11

Moss