strife's devLog

Android: How to Send Request (POST) to the Server (Full Application)

Hi,

Like I said before I am new to Android and I don’t hide it but I’ve started to like it so far. And now I’d like to show you an application which enables sending data to the servers remotely. Before I give you the code, just look at the screens how it looks.

But if you don’t care how it works just download the application from here:

It should be compatibile with 2.3.3 API.

Preview:

Here you can easly send your data to server, just like that. But first of all you have to change settings by clicking on proper link.

 

That how looks settings page

View after clicking on the button

How it works?

There is too many files to show, for that reason I decided to explain only few features.

How to make a progress bar?

It’s simple, in your Activity class you’ve got to create a class which will extends of AsyncTask it should look like that:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
...
private class postData extends AsyncTask<String, Void, String> {
    private final ProgressDialog dialog = ProgressDialog.show(sendToServerActivity, "",
            "Saving data to server. Please wait...", true);
    @Override
    protected String doInBackground(String... params) {
        // perform long running operation operation
        SharedPreferences settings = context.getSharedPreferences(PREFS_FILE, 0);
        String server = settings.getString("server", "");
        
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost(server);
        try {
            // Add your data
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
            nameValuePairs.add(new BasicNameValuePair("android", editText1.getText().toString()));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
            try {
                httpclient.execute(httppost);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            // Execute HTTP Post Request
            // ResponseHandler<String> responseHandler=new BasicResponseHandler();
            // String responseBody = httpclient.execute(httppost, responseHandler);
            // if (Boolean.parseBoolean(responseBody)) {
            //    dialog.cancel();
            // }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            Log.i("HTTP Failed", e.toString());
        }         
        
        return null;
    }
...

And then if you’ve got already that construction you can simply run it. In my example it looks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public void sendItNow(Button button1) {
    button1.setOnClickListener(new View.OnClickListener() {
            
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // check if every
                boolean isValidate = validate(editText1.getText());
                
                if (isValidate) {
                    // send to server and open progress dialog box
                    new postData().execute(); // most important line
                }
            }
        });
}

As you see I execute this class using .execute method.

PHP Script which saves data

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
if (isset($_POST['android'])) {
file_put_contents("android.txt", $_POST['android']);
echo 1;
}
?>

Note: File android.txt has to have proper chmod (777).

How to manage pages?

The feature which will be able to do that you can find in Manifest file. I’ve noticed it is a very important file, something similar to file which defines how to run application, I guess. In my case I wanted to get some pages (Main and Help/Configuration). So then my Manifest file looks:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sendto.server"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="10" />
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".SendToServerActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="HelpActivity"></activity>
</application>
</manifest>

What exactly I mean by “Pages”, you can find out by clicking link below:

Leave a Reply