Clsaa file 1
package com.aabsys.ebdroid;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class BillDisplay extends Activity implements OnClickListener {
TextView showBill, billHdr1, billHdr2, billHdr3, billHdr4, billHdr5,
billHdr6, billHdr7, billHdr8;
Button save, print;
JSONArray cons_detail = null;
JSONParser jParser = new JSONParser();
String mtrno, sBody,presRdng;
private static String bill_url = "http://192.168.234.1/Android/get_consumer_details.php";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bill_display);
mtrno = this.getIntent().getStringExtra("mtrno");
presRdng= this.getIntent().getStringExtra("reading");
showBill = (TextView) findViewById(R.id.tvBill);
// billHdr1= (TextView) findViewById(R.id.tvHeaderConsAcNo);
save = (Button) findViewById(R.id.bSave);
print = (Button) findViewById(R.id.bPrint);
save.setOnClickListener(this);
print.setOnClickListener(this);
showBill.setText("Bill Month: March" + "\n" + "Bill Date: 19/03/2013");
// billHdr1.setText("CONSUMER AC NO.");
new GetBillDetails().execute();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.bill_display, menu);
return true;
}
class GetBillDetails extends AsyncTask<String, String, String> {
ProgressDialog pDialog;
String cons_ac_no, cust_id, oa_no, inst_no, mtr_sl_no, mtr_owner,
cons_name, cons_addrs, bill_unit_1, prev_unit, prev_amt,
pre_mtr_rdng, tax, bill_unit_2, bill_unit_3, bill_basis,
energy_charge, ed_charge, mtr_rent, misc_charge, prev_bill_dt;
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
pDialog = new ProgressDialog(BillDisplay.this);
pDialog.setMessage("Getting details.... Please wait...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// TODO Auto-generated method stub
List<NameValuePair> billParams = new ArrayList<NameValuePair>();
billParams.add(new BasicNameValuePair("connect",
"connectionallowed"));
billParams.add(new BasicNameValuePair("user_id", mtrno));
Log.d("details", billParams.toString());
JSONObject json = jParser.makeHttpRequest(bill_url, "POST",
billParams);
int success = 0;
Log.d("details", json.toString());
try {
success = json.getInt("success");
Log.d("success", "" + success);
} catch (Exception ex) {
ex.toString();
}
if (success == 1) {
try {
cons_detail = json.getJSONArray("consumers");
if (cons_detail.length() > 0) {
JSONObject c = cons_detail.getJSONObject(0);
cons_ac_no = c.getString("cons_ac_no");
cust_id = c.getString("cust_id");
oa_no = c.getString("oa_no");
inst_no = c.getString("inst_no");
mtr_sl_no = c.getString("mtr_sl_no");
mtr_owner = c.getString("mtr_owner");
cons_name = c.getString("cons_name");
cons_addrs = c.getString("cons_addrs");
bill_unit_1 = c.getString("bill_unit_1");
prev_unit = c.getString("prev_unit");
prev_amt = c.getString("prev_amt");
pre_mtr_rdng = c.getString("pre_mtr_rdng");
tax = c.getString("tax");
bill_unit_2 = c.getString("bill_unit_2");
bill_unit_3 = c.getString("bill_unit_3");
bill_basis = c.getString("bill_basis");
energy_charge = c.getString("energy_charge");
ed_charge = c.getString("ed_charge");
mtr_rent = c.getString("mtr_rent");
misc_charge = c.getString("misc_charge");
prev_bill_dt = c.getString("prev_bill_dt");
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPostExecute(String result) {
// TODO Auto-generated method stub
// super.onPostExecute(result);
int prev,curr;
float prUnitCh1,prUnitCh3,prUnitCh2,enrCh,edCh,mtrRnt,mscCh,totalAmt;
prev=Integer.parseInt(pre_mtr_rdng);
curr=Integer.parseInt(presRdng);
prUnitCh1=Float.parseFloat(bill_unit_1);
prUnitCh2=Float.parseFloat(bill_unit_2);
prUnitCh3=Float.parseFloat(bill_unit_3);
enrCh=Float.parseFloat(energy_charge);
edCh=Float.parseFloat(ed_charge);
mtrRnt=Float.parseFloat(mtr_rent);
mscCh=Float.parseFloat(misc_charge);
totalAmt=(((curr - prev)>50?50*prUnitCh1:(curr - prev)*prUnitCh1)+ ((curr - prev-50)>0?((curr - prev-50)*prUnitCh2):0)+enrCh+mtrRnt+mscCh);
pDialog.dismiss();
Calendar c = Calendar.getInstance();
SimpleDateFormat month_date = new SimpleDateFormat("MMMMMMMMM");
String month_name = month_date.format(c.getTime());
sBody = "CESU ELECTRICITY"+"\n"
+ "---------------------------------------"+"\n"
+ "BILL MONTH :"+"\t" + month_name + "-"
+ c.get(Calendar.DAY_OF_MONTH) + "\n" + "BILL DATE :"+"\t"
+ new Date(System.currentTimeMillis()) + "\n"
+ "BILL NO :"+"\t" + 202 + "\n" + "BILLED FOR :"+"\t" + "" + "\n"
+ "DIV :"+"\t" + "" + "\n" + "SUB DIV : " + "" + "\n"
+ "SECTION :"+"\t" + "" + "\n" + "SBM NO : " + "" + "\n"
+"----------------------------------------"+"\n"
+ "CUSONSUMER AC NO :"+"\t" + cons_ac_no + "\n" + "CUST ID :"+"\t"
+ cust_id + "\n" + "INST NO :"+"\t" + inst_no + "\n"
+ "MTR SL NO :"+"\t" + mtr_sl_no + "\n" + "MTR OWNER :"+"\t"
+ mtr_owner + "\n" + "NAME :"+"\t" + cons_name + "\n"
+ "ADDRS :"+"\t" + cons_addrs + "\n"
+"----------------------------------------"+"\n"
+ "PRES RDNG : " + presRdng + " -on "+new Date(System.currentTimeMillis())+ "\n"
+ "PREV RDNG : " + pre_mtr_rdng +" -on "+prev_bill_dt+ "\n"
+ "UNITS ADVANCED : " + (curr - prev)+ "\n"
+ "BILL BASIS : " + "" + "\n"
+"----------------------------------------"+"\n"
+ "MFC/CUST CHRG : " + bill_basis + "\n"
+ "EC : " +((curr - prev)>50?50:(curr - prev))+"*"+prUnitCh1+" = " + ((curr - prev)>50?50*prUnitCh1:(curr - prev)*prUnitCh1) +"\n"
+ "EC : " +((curr - prev-50)>0?((curr - prev-50)):0)+"*"+prUnitCh2+ " = "+((curr - prev-50)>0?((curr - prev-50)*prUnitCh2):0) + "\n"
+ "ENERGY CHRG : " + enrCh + "\n"
+ "ED CHRG : " + edCh + "\n"
+ "METER RENT : " + mtrRnt + "\n"
+ "MISC. CHRG : " + mscCh + "\n"
+"----------------------------------------"+"\n"
+ "TOTAL AMOUNT : " +totalAmt + "\n"
+ "REBATE : " + "0.0" + "\n"
+ "TOTAL BILL BY DUE DT : " + totalAmt + "\n"
+"----------------------------------------"+"\n";
// String.format(sBody,"" );
showBill.setText(sBody);
}
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.bSave) {
File EBdroid = new File(Environment.getExternalStorageDirectory(),
"Bill");
if (!EBdroid.exists())
EBdroid.mkdirs();
File billFile = new File(EBdroid, "eBill.txt");
try {
FileWriter writer = new FileWriter(billFile);
writer.append(sBody);
writer.flush();
writer.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(this, "Saved Suceesfully in /SDcard/Bill/", Toast.LENGTH_LONG).show();
} else {
/*
* File EBdroid = new
* File(Environment.getExternalStorageDirectory(), "Bill"); if
* (!EBdroid.exists()) EBdroid.mkdirs(); File billFile = new
* File(EBdroid, "eBill.txt"); try { FileWriter writer = new
* FileWriter(billFile); writer.append(sBody); writer.flush();
* writer.close();
*
* } catch (IOException e) { // TODO Auto-generated catch block
* e.printStackTrace(); }
*/
Intent intent = new Intent();
intent.setAction(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(
Intent.EXTRA_STREAM,
Uri.parse("file://"
+ Environment.getExternalStorageDirectory()
+ "/Bill/eBill.txt"));
startActivity(intent);
}
}
}
Class File 2
package com.aabsys.ebdroid;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Gravity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class ConsBill extends Activity {
EditText mtrNo, currReading;
Button bSubmit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cons_bill);
mtrNo = (EditText) findViewById(R.id.etMtrNo);
currReading = (EditText) findViewById(R.id.etCurMtrRdng);
bSubmit = (Button) findViewById(R.id.bSubmit);
bSubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (mtrNo.getText().toString().length() > 0
&& currReading.getText().toString().length() > 0) {
if (!isNetworkAvailable()) {
Toast toast = Toast.makeText(ConsBill.this,
"Not Connected To Server ...",
Toast.LENGTH_LONG);
toast.setGravity(Gravity.TOP, 0, 300);
toast.show();
} else {
Intent intent = new Intent(ConsBill.this,
BillDisplay.class);
intent.putExtra("mtrno", mtrNo.getText().toString());
intent.putExtra("reading",currReading
.getText().toString());
startActivity(intent);
}
} else
Toast.makeText(ConsBill.this, "Please Enter Valid Data!",
Toast.LENGTH_SHORT).show();
}
});
}
private boolean isNetworkAvailable() {
// TODO Auto-generated method stub
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;
ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] netInfo = cm.getAllNetworkInfo();
for (NetworkInfo ni : netInfo) {
if (ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;
if (ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;
}
return haveConnectedWifi || haveConnectedMobile;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.cons_bill, menu);
return true;
}
}
Json Parser Classs
package com.aabsys.ebdroid;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.List;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.impl.client.DefaultHttpClient;
import org.json.JSONException;
import org.json.JSONObject;
import android.util.Log;
public class JSONParser {
static InputStream is = null;
static JSONObject jObj = null;
static String json = "";
public JSONParser() {
}
public JSONObject makeHttpRequest(String url, String method,
List<NameValuePair> params) {
try {
if (method == "POST") {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpPost httpPost = new HttpPost(url);
httpPost.setEntity(new UrlEncodedFormEntity(params));
HttpResponse httpResponse = httpClient.execute(httpPost);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
} else if (method == "GET") {
DefaultHttpClient httpClient = new DefaultHttpClient();
String paramString = URLEncodedUtils.format(params, "utf-8");
url += "?" + paramString;
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
HttpEntity httpEntity = httpResponse.getEntity();
is = httpEntity.getContent();
}
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(
is, "iso-8859-1"), 8);
StringBuilder sb = new StringBuilder();
String line = null;
while ((line = reader.readLine()) != null) {
sb.append(line + "\n");
}
is.close();
json = sb.toString();
} catch (Exception e) {
Log.e("Buffer Error", "Error converting result " + e.toString());
}
try {
jObj = new JSONObject(json);
} catch (JSONException e) {
Log.e("JSON Parser", "Error parsing data " + e.toString());
}
return jObj;
}
}