First Android App | Step 12 | Upload Data to Server from Android App

In this post, we are going to discuss the working of how to upload data from Android App to the server.

The example is that of uploading order information to server from an E-Commerce Shopping Application on android.

The screen looks like this.



The complete demo is available in this video.



Source Code


Checkout.java.

Note: Currently, UserId for user has been hardcoded to 1.

package com.zing.basket;


import java.util.ArrayList;

import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.zing.basket.util.Product;

public class Checkout extends Activity {

 ArrayList cart_list = new ArrayList();
 SQLiteDatabase sqLite;
 int count;
 String address ="";
 String productList="";
 String userId="1";
 String orderNo="";
 String numberOfItems;
 @Override
 public void onCreate(Bundle savedInstanceState)
 {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_checkout);
  
  getCartData();
  
  Typeface type= Typeface.createFromAsset(getAssets(),"fonts/book.TTF");
  
  TextView addressHeader = (TextView) findViewById(R.id.header);
  addressHeader.setTypeface(type);
  
  TextView paymentHeader = (TextView) findViewById(R.id.header1);
  paymentHeader.setTypeface(type);
  
  final EditText name = (EditText) findViewById(R.id.name);
  name.setTypeface(type);
  
  final EditText address1 = (EditText) findViewById(R.id.address1);
  address1.setTypeface(type);
  
  final EditText address2 = (EditText) findViewById(R.id.address2);
  address2.setTypeface(type);
  
  final EditText city = (EditText) findViewById(R.id.city);
  city.setTypeface(type);
  
  final EditText state = (EditText) findViewById(R.id.state);
  state.setTypeface(type);
  
  final EditText pin = (EditText) findViewById(R.id.pin);
  pin.setTypeface(type);
  
  final EditText phone = (EditText) findViewById(R.id.phone);
  phone.setTypeface(type);
  
  Button submit = (Button) findViewById(R.id.submit);
  submit.setTypeface(type);
  
  submit.setOnClickListener(new OnClickListener()
  {
  
   @Override
   public void onClick(View arg0) {
    
    int i=0;
    String prod;
    
    //do validations
    
    //if all validations are ok, then proceed. 
    // construct the address
    address = address.concat(":name:");
    address = address.concat(name.getText().toString());
    address = address.concat(":address1:");
    address = address.concat(address1.getText().toString());
    address = address.concat(":address2:");
    address = address.concat(address2.getText().toString());
    address = address.concat(":city:");
    address = address.concat(city.getText().toString());
    address = address.concat(":state:");
    address = address.concat(state.getText().toString());
    address = address.concat(":pin:");
    address = address.concat(pin.getText().toString());
    address = address.concat(":phone:");
    address = address.concat(phone.getText().toString());
    address = address.replace(" ", "%20");
    //Log.d("arindam",address);
    
    //construct the order summary
    numberOfItems = String.valueOf(cart_list.size());
    //Log.d("arindam",numberOfItems);
    // construct the product details
    
    for (i=0;i {
 JSONParser jParser;
 String url = new String();
 ProgressDialog pd;
 String response = new String();
 SQLiteDatabase sqLite;
 String result;
 
 @Override
 protected void onPreExecute() {
  super.onPreExecute();

  url = "http://lawgo.in/lawgo/order?userid="
    + userId + "&address=" + address + "&itemcount="+numberOfItems +"&orderlist=" + productList;
  //Log.d("arindam","url"+ url);
  
  jParser = new JSONParser();
  pd = new ProgressDialog(Checkout.this);
  pd.setMessage("Please Wait");
  pd.setCancelable(false);
  pd.show();
 }

 @Override
 protected String doInBackground(Void... arg0) {
  try {
  JSONObject json = jParser.getJSONFromUrl(url);
  
   response = json.getString("Status");
   Log.i("arindam",response);
   
   if (response.equalsIgnoreCase("Success")) {
     
     
     orderNo = json.getString("OrderNo");
     Log.i("arindam",orderNo);
     deleteCartData();
     addOrderData();
     result = "success";
    }
  } catch (Exception e) 
  {
   result = "connection error";
  }
  return result;

 }

 @Override
 protected void onPostExecute(String result) {
  if (result.equalsIgnoreCase("success")) 
  {
   Toast.makeText(Checkout.this,"Order No "+orderNo+"successfully created.", Toast.LENGTH_LONG).show();
   pd.dismiss();
   Intent intent = new Intent(getApplicationContext(),HomeScreen.class);
   startActivity(intent);
   finish();
   
  } else {
   pd.dismiss();
   Toast.makeText(getApplicationContext(),
     "Network not aviliable, please try later.", Toast.LENGTH_LONG).show();
   finish();
  }

 }
}
 
}


2 comments:

  1. Please Upload the php file that has been used to upload the data on the server

    ReplyDelete
  2. I am working on this. Will share soon..

    ReplyDelete