Code programing problems solving , Ideas discussion , software maintenance , contracting , consulting , helping ... together we can do more

http://www.tutorial4us.com/java/jdk-jvm-jre
12/24/2015

http://www.tutorial4us.com/java/jdk-jvm-jre

JDK JVM JRE - These all the backbone of java language. Each components have separate works. Jdk and Jre physically exists but Jvm are abstract machine it means it not physically exists.

http://www.hub4tech.com/interview/android
12/20/2015

http://www.hub4tech.com/interview/android

Android interview questions and answers -with explanation for various interviews, competitive examination and entrance test. Here you can Practice your first round Job interview.

12/15/2015

Gooooooood morning ♨
12/15/2015

Gooooooood morning ♨

12/13/2015

proceed forward with android programming

What we have today

😁😁👇📱🌍📱
Simple Client server Communication

http://osamashabrez.com/simple-client-server-communication-in-android/



I was working of an android project recently and now I guess .. I am done with my part. Yeah its a team of 4 and the project was divided accordingly. One of the responsibilities I was assigned was the development of Online Leader Board. While I was working, I searched through Internet and found a few examples as well, – I wonder which question Google couldn’t answer?
All of them were either to much complicatedly explained or didn’t cover the complete concepts behind a complete Client Server Communication In Android. Either the writers were assuming that the person who’ll land of this page will be intelligent enough to guess the other part or were simple not interested of posting a complete solution to someone’s problem. And for the same reason I am writing this post, so if someone else lands on this page while searching the same problem he/she could find a complete solution to their needs.
In this tutorial I’ll be assuming that you at least:
Have a basic knowledge of android
Have already developed a few small android application (e.g calculators, alarm, reminder etc.)
If you are new to android development, please leave me a comment and I might start from the beginning.

In spite of using the 3rd party API’s, json classes etc. I’ll be using the default HttpClient from org.apache.http package. For those who want to get the code snippet just and not want me to explain it, the code will be as follows:

Get Data From ServerJava

HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://example.com/script.php?var1=androidprogramming");
try {
HttpResponse response = httpclient.execute(httpget);
if(response != null) {
String line = "";
InputStream inputstream = response.getEntity().getContent();
line = convertStreamToString(inputstream);
Toast.makeText(this, line, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Unable to complete your request", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
Toast.makeText(this, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Caught IOException", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Caught Exception", Toast.LENGTH_SHORT).show();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
HttpClient httpclient = new DefaultHttpClient();
HttpGet httpget = new HttpGet("http://example.com/script.php?var1=androidprogramming");
try {
HttpResponse response = httpclient.execute(httpget);
if(response != null) {
String line = "";
InputStream inputstream = response.getEntity().getContent();
line = convertStreamToString(inputstream);
Toast.makeText(this, line, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Unable to complete your request", Toast.LENGTH_LONG).show();
}
} catch (ClientProtocolException e) {
Toast.makeText(this, "Caught ClientProtocolException", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "Caught IOException", Toast.LENGTH_SHORT).show();
} catch (Exception e) {
Toast.makeText(this, "Caught Exception", Toast.LENGTH_SHORT).show();
}
GET is used to know how actually the code is working.
Default strings from android strings.xml are not used to avoid useless explanation for this tutorial but they are the recommended way too use while designing an app rather then hard coded strings.
Client server communication is this much simple when it comes to android.
Create HttpClient with the default constructor.
Create a HttpGet or HttpPost object depending upon your needs, in this case I made a GET object so that we can know whats going on.
Initialize the object with a GET or POST url.
Execute the GET/POST object through the Http and you’ll get the server’s response in the response object of HttpResponse.


Now the next part is how to fetch data received from the server in HttpResponse object. The server response can be type casted into InputStream object which will then parse the response into simple String. here is the code to do that:

Parse Server Response Into StringJava

private String convertStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();
}
return total.toString();
}
1
2
3
4
5
6
7
8
9
10
11
12
13
private String convertStreamToString(InputStream is) {
String line = "";
StringBuilder total = new StringBuilder();
BufferedReader rd = new BufferedReader(new InputStreamReader(is));
try {
while ((line = rd.readLine()) != null) {
total.append(line);
}
} catch (Exception e) {
Toast.makeText(this, "Stream Exception", Toast.LENGTH_SHORT).show();
}
return total.toString();
}

So now the code is completed for the client side and we will be focusing on the server side script. The example above will show a notification of the string response the server will send back, here is the code snippet I wrote in php for the server side.

Server Side ScriptPHP

$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die('Unable to connect');
$db = mysql_selectdb(DB_NAME,$connection) or die('Database not found');
if(isset($_GET['var'])) :
$query = "SELECT [a few parameters] FROM [some table] WHERE [some conditions]";
$resultset = mysql_query($query);
$row = mysql_fetch_array($resultset)
echo $row['[column name]'];
else:
echo "No get Request Received";
endif;
1
2
3
4
5
6
7
8
9
10
$connection = mysql_connect(DB_HOST,DB_USER,DB_PASSWORD) or die('Unable to connect');
$db = mysql_selectdb(DB_NAME,$connection) or die('Database not found');
if(isset($_GET['var'])) :
$query = "SELECT [a few parameters] FROM [some table] WHERE [some conditions]";
$resultset = mysql_query($query);
$row = mysql_fetch_array($resultset)
echo $row['[column name]'];
else:
echo "No get Request Received";
endif;
Warning:

Implement proper checks for data filtration. The code above was meant for the tutorial and is not enough to use in a market application.
This completes this small tutorial here, now a few of the most asked questions while doing the client server communication:

Why not use the XML approach while performing the client server data transfer operations:
This was my first android application which used internet permissions and fetched data on runtime from server, I found it more interesting to work first manually with writing my own script and then go for the proper XML approach.
Why not use the JSON library for client server application in fact of using an array for GET or POST request:
Answer remains the same for this question as well. I have implemented my own methods and know how HTTP works in android. Next I’ll be using JSON and then switch to XML, JSON tutorial will be the next one in this series.
Some other questions:
Post them into the comments section and I’ll be happy to answer them right away. If you are enough experienced and found this approach not practicable while developing apps for the market, please provide your valuable feedback in that case as well. We warmly welcome a healthy and fruitful discussion here.

To get access to internet at Android, following field must be included to AndroidManifest.xml file of the project:

Internet PermissionsXHTML


1

This domain is established to be used for illustrative examples in documents. You may use this domain in examples without prior coordination or asking for permission.

12/10/2015

hope you are very well mr/mis android's programmer.... What about today 😱😱😱

Yes it's what you looking for


^^ how to get current location ^^

let's go ahead










import android.content.Context;
import android.content.Intent;
import android.provider.Settings;


import android.app.AlertDialog;
import android.content.DialogInterface;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.util.Log;
import android.widget.Toast;

public class GPSTracker implements LocationListener {

private final Context mContext;

// flag for GPS status
public boolean isGPSEnabled = false;

// flag for network status
boolean isNetworkEnabled = false;

// flag for GPS status
boolean canGetLocation = false;

Location location; // location
double latitude; // latitude
double longitude; // longitude

// The minimum distance to change Updates in meters
private static final long MIN_DISTANCE_CHANGE_FOR_UPDATES = 1; // 10 meters

// The minimum time between updates in milliseconds
private static final long MIN_TIME_BW_UPDATES = 1; // 1 minute

// Declaring a Location Manager
protected LocationManager locationManager;

public GPSTracker(Context context) {
this.mContext = context;
getLocation();
}

/**
* Function to get the user's current location
*
*
*/
public Location getLocation() {
try {
locationManager = (LocationManager) mContext
.getSystemService(Context.LOCATION_SERVICE);

// getting GPS status
isGPSEnabled = locationManager
.isProviderEnabled(LocationManager.GPS_PROVIDER);

Log.v("isGPSEnabled", "=" + isGPSEnabled);

// getting network status
isNetworkEnabled = locationManager
.isProviderEnabled(LocationManager.NETWORK_PROVIDER);

Log.v("isNetworkEnabled", "=" + isNetworkEnabled);

// if (isGPSEnabled == false && isNetworkEnabled == false) {
// no network provider is enabled
// } else {
this.canGetLocation = true;
// if GPS Enabled get lat/long using GPS Services
if (isGPSEnabled) {
location=null;
if (location == null) {
locationManager.requestLocationUpdates(
LocationManager.GPS_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("GPS Enabled", "GPS Enabled");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}
}else{
showSettingsAlert();
/*
while(latitude==0.0 && longitude ==0.0){
location=null;
locationManager.requestLocationUpdates(
LocationManager.NETWORK_PROVIDER,
MIN_TIME_BW_UPDATES,
MIN_DISTANCE_CHANGE_FOR_UPDATES, this);
Log.d("Network", "Network");
if (locationManager != null) {
location = locationManager
.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
if (location != null) {
latitude = location.getLatitude();
longitude = location.getLongitude();
}
}
}*/

}

} catch (Exception e) {
e.printStackTrace();
}

return location;
}

/**
* Stop using GPS listener Calling this function will stop using GPS in your
* app
* */
public void stopUsingGPS() {
if (locationManager != null) {
locationManager.removeUpdates(GPSTracker.this);
}
}

/**
* Function to get latitude
* */
public double getLatitude() {
if (location != null) {
latitude = location.getLatitude();
}

// return latitude
return latitude;
}

/**
* Function to get longitude
* */
public double getLongitude() {
if (location != null) {
longitude = location.getLongitude();
}

// return longitude
return longitude;
}

/**
* Function to check GPS/wifi enabled
*
* boolean
* */
public boolean canGetLocation() {
return this.canGetLocation;
}

/**
* Function to show settings alert dialog On pressing Settings button will
* lauch Settings Options
* */
public void showSettingsAlert() {
AlertDialog.Builder alertDialog = new AlertDialog.Builder(mContext);

// Setting Dialog Title
alertDialog.setTitle("GPS is settings");

// Setting Dialog Message
alertDialog
.setMessage("GPS is not enabled. Do you want to go to settings menu?");

// On pressing Settings button
alertDialog.setPositiveButton("Settings",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
mContext.startActivity(intent);
}
});

// on pressing cancel button
alertDialog.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});

// Showing Alert Message
alertDialog.show();
}


public void onLocationChanged(Location location) {

}


public void onProviderDisabled(String provider) {
}


public void onProviderEnabled(String provider) {
}


public void onStatusChanged(String provider, int status, Bundle extras) {
}

}

12/09/2015

Hi guys... gooooooood night 🌃 for all


😀😁😁

Today we will show you how to check your >>>> if open 🔓 or no



import java.io.IOException;
import android.content.Context;
import android.content.Intent;
import java.net.HttpURLConnection;
import java.net.URL;

import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.widget.Toast;
public class CheckInternetAccess {
static Context context;
Intent intent ;
public static int status =0;
public CheckInternetAccess(Context con , Intent i){
context = con;
intent = i;
checkNetworkConnectivity();
}

public void checkNetworkConnectivity(){


status =0;
if(intent.getExtras()!=null) {
NetworkInfo ni=(NetworkInfo) intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
if(ni!=null && ni.getState()==NetworkInfo.State.CONNECTED) {
if(hasActiveInternetConnection(context)){
Toast.makeText(context, "app "+"has access to internet",
Toast.LENGTH_LONG).show();
status=1;
}
}
}

}
public static boolean hasActiveInternetConnection(Context context) {

try {
HttpURLConnection urlc = (HttpURLConnection) (new URL("http://www.google.com").openConnection());
urlc.setRequestProperty("User-Agent", "Test");
urlc.setRequestProperty("Connection", "close");
urlc.setConnectTimeout(1500);
urlc.connect();
return (urlc.getResponseCode() == 200);
} catch (IOException e) {
Toast.makeText(context, "Error checking internet connection : "+e,
Toast.LENGTH_LONG).show();
}

return false;
}

}




Finally don't forget to add the following permission to your android mainfist



enjoy 👍👍👍


Buy now at $10.000.000.

11/28/2015

the first android code about sqlite database

yes !!! it simple class that allow you to work with SQLITE DB

^_^

import java.util.ArrayList;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteDatabase.CursorFactory;
import android.database.sqlite.SQLiteOpenHelper;
import android.widget.Toast;

public class DBAdapter{
SQLiteOpenHelper helper;
Context ctx;
SQLiteDatabase db;
public DBAdapter(Context context , String db_name
,CursorFactory factory,
int version){
ctx = context;
helper = new DBAHelper(context, db_name, factory, version);
db=helper.getReadableDatabase();
}

///////////////////////////////////////////////////////////////////////////////
class DBAHelper extends SQLiteOpenHelper {
public DBAHelper(Context context, String db_name, CursorFactory factory,
int version) {
super(context, db_name, factory, version);
// TODO Auto-generated constructor stub
}


public void onCreate(SQLiteDatabase db) {
db.execSQL("create table Mesg (MID text , "
+ "MText text , MsgDate text);");
}


public void onUpgrade(SQLiteDatabase db, int oldvr, int newvr) {
db.execSQL("drop table Mesg");
onCreate(db);
}
//////////////////////////////////////////////////////////////////////////////////
}
public boolean sqlOperation(String sql){
boolean is_ok = true;
try{
db.execSQL(sql);
}catch(Exception ex){
is_ok = false;
}
return is_ok;
}

public void fill_single_form(ArrayList my_list,String sql){
Cursor cr = db.rawQuery(sql,null);
if(cr.moveToFirst()==true){
do
{
my_list.add(("phone : "+cr.getString(0)+" Message : "+
cr.getString(1)+" Date : "+cr.getString(2)).toString());
}while(cr.moveToNext());
}
else Toast.makeText(ctx, "no data", Toast.LENGTH_LONG).show();
}
}

11/28/2015

good night every one here we are sorry for late firstly :)

welcome of you to android section this week

we will publish advanced code shot that may help you to develop your app let us begin :?:

11/15/2015

Address

Sudan, TX
79371

Website

Alerts

Be the first to know and let us send you an email when Code posts news and promotions. Your email address will not be used for any other purpose, and you can unsubscribe at any time.

Share