Simplest Android Program to Upload and Download Data from Web Server

0
279
Android Program

A simplest android program (with AndroidManifest file, XML Layout file and Java Activity files) to Upload and Download Data from Web Server is presented here.

Contents:



AndroidManifest.xml [Back]


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="webapp.com.webapp" >

    <uses-permission android:name="android.permission.INTERNET"/>

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".RegisterActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity
            android:name=".LoginActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.Login" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>

register_layout.xml  [Back]


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_margin="30px"
    android:layout_height="match_parent">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etName"
        android:hint="ENTER NAME"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etCity"
        android:hint="ENTER CITY"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etEmail"
        android:hint="ENTER EMAIL"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etPwd"
        android:hint="ENTER PASSWORD"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etPhn"
        android:hint="ENTER PHONE"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Register"
        android:id="@+id/btnOk"
        android:onClick="btnOk_click"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

login_layout.xml [Back]


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="30px">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etEmail_login"
        android:hint="ENTER EMAIL"/>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/etPwd_login"
        android:hint="ENTER PASSWORD"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Login"
        android:id="@+id/btnLogin"
        android:onClick="btnLogin_click"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

RegisterActivity.java  [Back]


package webapp.com.webapp;

import android.app.Activity;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by es on 31/8/16.
 */
public class RegisterActivity extends Activity
{
    EditText etName, etCity, etEmail, etPwd, etPhn;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.register_layout);

        etName = (EditText) findViewById(R.id.etName);
        etCity = (EditText) findViewById(R.id.etCity);
        etEmail = (EditText) findViewById(R.id.etEmail);
        etPwd = (EditText) findViewById(R.id.etPwd);
        etPhn = (EditText) findViewById(R.id.etPhn);

    }

    public void btnOk_click(View v)
    {
        new RegisterDownloader().execute("http://myweb.com/api/add_users.php?city="+etCity.getText().toString()+"&nm="+etName.getText().toString()+"&email="+etEmail.getText().toString()+"&pwd="+etPwd.getText().toString()+"&phn="+etPhn.getText().toString());
    }

    class RegisterDownloader extends AsyncTask<String, Void, String>
    {
        @Override
        protected String doInBackground(String... params)
        {
            return Download.getString(params[0]);
        }

        @Override
        protected void onPostExecute(String s) {
            Toast.makeText(RegisterActivity.this, "Register Done...", Toast.LENGTH_SHORT).show();
            startActivity(new Intent(getApplicationContext(), LoginActivity.class));
        }
    }
}

LoginActivity.java  [Back]


package webapp.com.webapp;

import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by es on 31/8/16.
 */
public class LoginActivity extends Activity
{
    EditText etEmail, etPwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login_layout);

        etEmail = (EditText) findViewById(R.id.etEmail_login);
        etPwd = (EditText) findViewById(R.id.etPwd_login);

    }

    public void btnLogin_click(View v)
    {
        new LoginDownloader().execute("http://myweb.com/api/login.php?email="+etEmail.getText().toString()+"&pwd="+etPwd.getText().toString()); 
    } 
    class LoginDownloader extends AsyncTask<String, Void, String> { 
    
    @Override 
    protected String doInBackground(String... params) { 
         return Download.getString(params[0]); 
    } 
   @Override 
    protected void onPostExecute(String s) { T
           oast.makeText(LoginActivity.this, "Welcome....", Toast.LENGTH_SHORT).show(); 
    } 
   } 
}

 

SHARE
Previous articleProgrammers Should Know : Five Different Stack Implementations
Next articleAn Introduction to Internet of Things
Dr R M Makwana
Dr. Makwana is Ph.D. in Computer Engineering, specialized in Artificial Intelligence from Sardar Patel University, Anand, Gujarat, India. Accelerated career growth from lecturer to professor in short span, having teaching experience of more than 13 years. He is TechSavvy with Research interest in Artificial Intelligence, Image Processing, Computer Vision, and Internet of Things. Actively supporting research community by providing service as a member of technical program committees of national and international conferences and workshops, as well as by reviewing journal and conference papers.

LEAVE A REPLY