Punjan Sudhar
Punjan Sudhar

Reputation: 221

Android : Post data to a website via app and view results in app

I have an application which converts a website to mobile format. There is a spinner in the app. So my doubt is, how do i pass these selected values from my spinner to the website to get results?

Upvotes: 1

Views: 767

Answers (2)

Raghunandan
Raghunandan

Reputation: 133560

You need to make a http post request.

http://developer.android.com/reference/org/apache/http/client/HttpClient.html.

NOTE : If you are making network related operation you should use a AsyncTask other wise you will get a NetworkOnMainThreadException (Honeycomb and later).

public void postData() {
// Create a new HttpClient and Post Header
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://www.yoursite.com/script.php");

try {
    // Add your data
    List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
    nameValuePairs.add(new BasicNameValuePair("id", "12345"));
    nameValuePairs.add(new BasicNameValuePair("stringdata", "AndDev is Cool!"));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

    // Execute HTTP Post Request
    HttpResponse response = httpclient.execute(httppost);

} catch (ClientProtocolException e) {
    // TODO Auto-generated catch block
} catch (IOException e) {
    // TODO Auto-generated catch block
}
} 

Some links with source code available in the links below

http://www.androidhive.info/2011/10/android-making-http-requests/

http://android-er.blogspot.in/2011/09/example-of-httppost-on-android.html

Upvotes: 1

ararog
ararog

Reputation: 1092

Since you are running the a mobile web application inside a WebView container you can use a well known library like jQuery an its ajax stuff.

Upvotes: 0

Related Questions