Download zip file using HTTPPOST in Android :

Step 1 :

Create an Asybktask

new save().execute();

Step 2:

In Asynktask

public class save extends AsyncTask<String, Integer, byte[]> {
@Override
protected void onPostExecute(byte[] result) {
super.onPostExecute(result);
}
byte[] data;
@Override
protected byte[] doInBackground(String... arg0) {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(URL);
try {
MultipartEntity entity = new MultipartEntity();

//Add required post data
entity.addPart("id", new StringBody("1"));
entity.addPart("api_key", new StringBody("key"));
entity.addPart("api_password", new StringBody("password"));

//Execute post method
httppost.setEntity(entity);

//Read the response
HttpResponse response = httpclient.execute(httppost);
 InputStream input = response.getEntity().getContent();
data = new byte[input.available()];
input.read(data);
File path = new File(Environment.getExternalStorageDirectory()+"/asdfg.zip");
if (!path.isFile()){
path.createNewFile();
}
OutputStream outputStream =
new FileOutputStream(path);

int read = 0;
byte[] bytes = new byte[1024];

while ((read = input.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
} catch (ClientProtocolException e) {
} catch (IOException e) {
}
return data;
}
}

AlertDialog with EditText Android:

AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);

alert.setTitle("Title");

alert.setMessage("Message");

// Set an EditText view to get user input
EditText input = new EditText(MainActivity.this);

alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {

  String value = input.getText().toString();

  Log.e("edit", value+ " va ");
  // Do something with value!
  
}
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
    // Canceled.
  }
});
alert.show();

Execute SQL file into sqlite for Android:

Step 1 :

Copy and past your sql file into raw folder
src -> res -> raw -> yoursql

Note : Remove the extension e.g use yoursql only instead of yoursql.sql

Step 2 :

In DataBaseHelper class :

import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseHelper extends SQLiteOpenHelper{
 
 //The Android's default system path of your application database.
 private static final String DB_PATH = "/data/data/com.example.name/databases/";
 
 private static final String DB_NAME = "yoursqlite.sqlite";
 
 private SQLiteDatabase myDataBase; 
 
 private final Context myContext;
 
 /**
 * Constructor
 * Takes and keeps a reference of the passed context in order to access to the application assets and resources.
 * @param context
 */
 public DataBaseHelper(Context context) {
 
 super(context, DB_NAME, null, 1);
 this.myContext = context;
 } 
 
 /**
 * Creates a empty database on the system and rewrites it with your own database.
 * */
 public void createDataBase() throws IOException{
 
 boolean dbExist = checkDataBase();
 
 if(dbExist){
 //do nothing - database already exist
 
 }else{
 
 //By calling this method and empty database will be created into the default system path
 //of your application so we are gonna be able to overwrite that database with our database.
 this.getReadableDatabase();
 
 try {
 
 copyDataBase();
 
 } catch (IOException e) {
 
 throw new Error("Error copying database");
 
 }
 }
 
 }
 /**
 * This reads a file from the given Resource-Id and calls every line of it as a SQL-Statement
 * 
 * @param context
 * 
 * @param resourceId
 * e.g. R.raw.food_db
 * 
 * @return Number of SQL-Statements run
 * @throws IOException
 */
 public int insertFromFile(Context context, int resourceId) throws IOException {
 // Reseting Counter
 int result = 0;
// Open the resource
 InputStream insertsStream = context.getResources().openRawResource(resourceId);
 BufferedReader insertReader = new BufferedReader(new InputStreamReader(insertsStream));
// Iterate through lines (assuming each insert has its own line and theres no other stuff)
 while (insertReader.ready()) {
 String insertStmt = insertReader.readLine();
 myDataBase.execSQL(insertStmt);
 result++;
 }
 insertReader.close();
// returning number of inserted rows
 return result;
 }
 /**
 * Check if the database already exist to avoid re-copying the file each time you open the application.
 * @return true if it exists, false if it doesn't
 */
 public boolean checkDataBase(){
 
 SQLiteDatabase checkDB = null;
 
 try{
 String myPath = DB_PATH + DB_NAME;
 checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);
 
 }catch(SQLiteException e){
 
 //database does't exist yet.
 return false;
 }
 
 if(checkDB != null){
 
 checkDB.close();
 
 }
 
 return checkDB != null ? true : false;
 }
 
 /**
 * Copies your database from your local assets-folder to the just created empty database in the
 * system folder, from where it can be accessed and handled.
 * This is done by transfering bytestream.
 * */
 private void copyDataBase() throws IOException{
 
 //Open your local db as the input stream
 InputStream myInput = myContext.getAssets().open(DB_NAME);
 
 // Path to the just created empty db
 String outFileName = DB_PATH + DB_NAME;
 
 //Open the empty db as the output stream
 OutputStream myOutput = new FileOutputStream(outFileName);
 
 //transfer bytes from the inputfile to the outputfile
 byte[] buffer = new byte[1024];
 int length;
 while ((length = myInput.read(buffer))>0){
 myOutput.write(buffer, 0, length);
 }
 
 //Close the streams
 myOutput.flush();
 myOutput.close();
 myInput.close();
 
 }
 
 public SQLiteDatabase openDataBase() throws SQLException{
 
 //Open the database
 String myPath = DB_PATH + DB_NAME;
 try
 {
 myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);
 }
 catch(Exception e)
 {
 //if it reaches here then the db is missing which means this a first run 
 return null;
 }
 return myDataBase;
 }
 
 @Override
 public synchronized void close() {
 
 if(myDataBase != null)
 myDataBase.close();
 
 super.close();
 
 }
 
 @Override
 public void onCreate(SQLiteDatabase db) {
 
 }
 
 @Override
 public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
 
 }
 
 // Add your public helper methods to access and get content from the database.
 // You could return cursors by doing "return myDataBase.query(....)" so it'd be easy
 // to you to create adapters for your views.
 
}

step 3 :

Where you want to read the sql file paste the following

DataBaseHelper myDbHelper ;

try{
 int count_of_query = myDbHelper.insertFromFile(getApplicationContext(), R.raw.yoursql);
 Log.e("count_of_query", count_of_query +"");
 }catch(Exception e){
 e.printStackTrace();
 }

POST Data in JSON format – ANDROID:

Step 1:

INternet Permission :

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

Step 2:

Initiate Asynktask.

new Save_task_to_server().execute();

Step 3 :

In Asynktask do the following steps:

public class Save_task_to_server extends
AsyncTask<String, Integer, Integer> {
@Override
protected void onPostExecute(Integer result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
}
@Override
protected Integer doInBackground(String… arg0) {
JSONObject obj = new JSONObject();
String username = “test_android”;
String password = “test”;
try {
//Put user name to JSONObject
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} obj.put(“username”, username);
try {
//Put password to JSONObject
obj.put(“password”, password);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
JSONArray list = new JSONArray();
JSONObject tasks = new JSONObject();
try {
//Pu tasktype to JSONObject
tasks.put(“tasktype”, tasktype);

//Put timestamp to JSONObject
tasks.put(“timestamp”, timestamp);

//Put taskvalue to JSONObject
tasks.put(“taskvalue”, taskvalue);

//Put units consumed to JSONObject
tasks.put(“unitsconsumed”, unitsconsumed);

//Put JSONObject to JSONArray
list.put(tasks);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
//Put JSONArray to JSONObject
obj.put(“tasks”, list);
} catch (JSONException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}

HttpClient httpclient = new DefaultHttpClient();
//Post url
HttpPost httppost = new HttpPost(
“/*Enter your url here*/”);
httppost.setHeader(“Accept”, “application/json”);
httppost.setHeader(“Content-type”, “application/json”);
try {
//Add JSONObject to StringEntity
StringEntity se = new StringEntity(obj.toString());
httppost.setEntity(se);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
HttpResponse response = null;
try {
response = httpclient.execute(httppost);
} catch (Exception e) {
e.printStackTrace();
}
HttpEntity response_entity = response.getEntity();
try {
Response = EntityUtils.toString(response_entity);
} catch (ParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

}

Expected Json POST object :

{“password”:”test”,”username”:”test_android”,”tasks”:[{“unitsconsumed”:”8686868″,”timestamp”:”2014-05-15 11:19:14″,”tasktype”:”inspection”,”taskvalue”:”20″},{“unitsconsumed”:”585″,”timestamp”:”2014-05-15 11:19:18″,”tasktype”:”inspection”,”taskvalue”:”3″},{“unitsconsumed”:”6868″,”timestamp”:”2014-05-15 11:19:41″,”tasktype”:”sequence”,”taskvalue”:”11″},{“unitsconsumed”:”9586″,”timestamp”:”2014-05-15 11:19:53″,”tasktype”:”sequence”,”taskvalue”:”11″}]}

 

Scroll inside an EditText Android:

In xml file :

<EditText
android:id="@+id/DwEdit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minLines="10"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:overScrollMode="always"
android:inputType="textCapSentences"></EditText>

And in Java code Add this piece

EditText dwEdit =(EditText) findViewById(R.id.DwEdit);       
dwEdit.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View view,MotionEvent event){
// TODO Auto-generated method stub
if(view.getId() == R.id.DwEdit){ view.getParent().requestDisallowInterceptTouchEvent(true);
switch(event.getAction()&MotionEvent.ACTION_MASK){
caseMotionEvent.ACTION_UP: view.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
}
return false;
}
}
);

 

 

Android LinearLayout

1. LinearLayout – Horizontal

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="horizontal" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" 
        android:layout_weight="1"/>
 
</LinearLayout>

2. LinearLayout – Vertical

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
 
    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 1" />
 
    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 2" />
 
    <Button
        android:id="@+id/button3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button 3" 
        android:layout_weight="1"/>
 
</LinearLayout>