Archive for the ‘Programming’ Category

Testing for Writing Code

Posted: April 28, 2013 in Android


package com.test.guide;

import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.os.Parcelable;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;

/**
* Android µœ÷◊Û”“ª¨∂Ø÷∏“˝–ßπ˚
* @Description: Android µœ÷◊Û”“ª¨∂Ø÷∏“˝–ßπ˚

* @File: MyGuideViewActivity.java

* @Package com.test.guide

* @Author Hanyonglu

* @Date 2012-4-6 œ¬ŒÁ11:15:18

* @Version V1.0
*/
public class MyGuideViewActivity extends Activity {
private ViewPager viewPager;
private ArrayList pageViews;
private ImageView imageView;
private ImageView[] imageViews;
// ∞¸π¸ª¨∂ØÕº∆¨LinearLayout
private ViewGroup main;
// ∞¸π¸–°‘≤µ„µƒLinearLayout
private ViewGroup group;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// …Ë÷√Œfi±ÍÂ¥∞ø⁄
requestWindowFeature(Window.FEATURE_NO_TITLE);

LayoutInflater inflater = getLayoutInflater();
pageViews = new ArrayList();
pageViews.add(inflater.inflate(R.layout.item05, null));
pageViews.add(inflater.inflate(R.layout.item06, null));
pageViews.add(inflater.inflate(R.layout.item01, null));
pageViews.add(inflater.inflate(R.layout.item02, null));
pageViews.add(inflater.inflate(R.layout.item03, null));
pageViews.add(inflater.inflate(R.layout.item04, null));

imageViews = new ImageView[pageViews.size()];
main = (ViewGroup)inflater.inflate(R.layout.main, null);

group = (ViewGroup)main.findViewById(R.id.viewGroup);
viewPager = (ViewPager)main.findViewById(R.id.guidePages);

for (int i = 0; i < pageViews.size(); i++) {
imageView = new ImageView(MyGuideViewActivity.this);
imageView.setLayoutParams(new LayoutParams(20,20));
imageView.setPadding(20, 0, 20, 0);
imageViews[i] = imageView;

if (i == 0) {
//ƒ¨»œ—°÷–µ⁄“ª’≈Õº∆¨
imageViews[i].setBackgroundResource(R.drawable.page_indicator_focused);
} else {
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}

group.addView(imageViews[i]);
}

setContentView(main);

viewPager.setAdapter(new GuidePageAdapter());
viewPager.setOnPageChangeListener(new GuidePageChangeListener());
}

// ÷∏“˝“≥√Ê ˝æ›  ≈‰∆˜
class GuidePageAdapter extends PagerAdapter {

@Override
public int getCount() {
return pageViews.size();
}

@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}

@Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}

@Override
public void destroyItem(View arg0, int arg1, Object arg2) {
// TODO Auto-generated method stub
((ViewPager) arg0).removeView(pageViews.get(arg1));
}

@Override
public Object instantiateItem(View arg0, int arg1) {
// TODO Auto-generated method stub
((ViewPager) arg0).addView(pageViews.get(arg1));
return pageViews.get(arg1);
}

@Override
public void restoreState(Parcelable arg0, ClassLoader arg1) {
// TODO Auto-generated method stub

}

@Override
public Parcelable saveState() {
// TODO Auto-generated method stub
return null;
}

@Override
public void startUpdate(View arg0) {
// TODO Auto-generated method stub

}

@Override
public void finishUpdate(View arg0) {
// TODO Auto-generated method stub

}
}

// ÷∏“˝“≥√Ê∏¸∏ƒ ¬º˛º‡Ã˝∆˜
class GuidePageChangeListener implements OnPageChangeListener {

@Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub

}

@Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub

}

@Override
public void onPageSelected(int arg0) {
for (int i = 0; i < imageViews.length; i++) {
imageViews[arg0].setBackgroundResource(R.drawable.page_indicator_focused);

if (arg0 != i) {
imageViews[i].setBackgroundResource(R.drawable.page_indicator);
}
}
}
}
}

MD5 in Java

Posted: October 15, 2012 in Java

 

 

Android Mysql

Posted: October 14, 2012 in Android

Android <=> Mysql

Main.java

——————————————

package com.example.databasetest_1;

import java.net.HttpURLConnection;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

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

public class MainActivity extends Activity {

private EditText email;
private EditText pwd;
private Button submit;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

email = (EditText)findViewById(R.id.editTextEmail);
pwd = (EditText)findViewById(R.id.editTextPassword);
submit = (Button)findViewById(R.id.buttonSubmit);

submit.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

try{
Toast.makeText(getApplicationContext(), “test”, Toast.LENGTH_SHORT).show();
Map<String, String> map = new HashMap<String, String>();
String emailString = email.getText().toString();
String pwdString = pwd.getText().toString();
map.put(“email”, emailString);
map.put(“pwd”, pwdString);
boolean result = submitDataByHttpClientDoPost(map, “http://10.0.2.2/android_con.php&#8221;);

if(result) {
Toast.makeText(getApplicationContext(), “Data Inserted.”, Toast.LENGTH_SHORT).show();
}
else {
Toast.makeText(getApplicationContext(), “Failed”, Toast.LENGTH_SHORT).show();
}
}
catch(Exception e){
e.printStackTrace();
System.out.println(e.getMessage());
}
}
});
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}

public Boolean submitDataByHttpClientDoPost(Map<String, String> map, String path) throws Exception {

HttpClient hc = new DefaultHttpClient();

HttpPost request = new HttpPost(path);

List<NameValuePair> parameters = new ArrayList<NameValuePair>();

for(Map.Entry<String, String> entry:map.entrySet()) {
NameValuePair nvp = new BasicNameValuePair(entry.getKey(), entry.getValue());
parameters.add(nvp);
}

HttpEntity entity = new UrlEncodedFormEntity(parameters, “UTF-8”);

request.setEntity(entity);

HttpResponse response = hc.execute(request);

if(response.getStatusLine().getStatusCode() == HttpURLConnection.HTTP_OK) {
return true;
}
else {
return false;
}
}
}

 

main.xml

—————————————–

<LinearLayout xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
xmlns:tools=”http://schemas.android.com/tools&#8221;
android:layout_width=”match_parent”
android:layout_height=”match_parent”
android:orientation=”vertical” >

<EditText
android:id=”@+id/editTextEmail”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Email” >

<requestFocus />
</EditText>

<EditText
android:id=”@+id/editTextPassword”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:ems=”10″
android:hint=”Password”
android:inputType=”textPassword” />

<Button
android:id=”@+id/buttonSubmit”
android:layout_width=”match_parent”
android:layout_height=”wrap_content”
android:text=”Submit” />

</LinearLayout>

 

android_con.php

——————————————

<?php

$con = mysql_connect(“localhost”, “root”, “password”);

if(!$con) {
die(‘Could not connect: ‘.mysql_connect);
}
else {

mysql_select_db(“database”, $con);

//get the android sending data
$str_email = $_REQUEST[“email”];
$str_pwd = $_REQUEST[“pwd”];
$sql = “insert into users(email, password) values(‘”.$str_email.”‘, ‘”.$str_pwd.”‘)”;

//insert data to database
if(!mysql_query($sql,$con)) {
return false;
}
else {
return true;
}

//close connection
mysql_close($con);

}

?>

 

Mainfest.xml

———————————–

<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;
package=”com.example.databasetest_1″
android:versionCode=”1″
android:versionName=”1.0″ >

<uses-sdk
android:minSdkVersion=”8″
android:targetSdkVersion=”15″ />
<uses-permission android:name=”android.permission.INTERNET”/>

<application
android:icon=”@drawable/ic_launcher”
android:label=”@string/app_name”
android:theme=”@style/AppTheme” >
<activity
android:name=”.MainActivity”
android:label=”@string/title_activity_main” >
<intent-filter>
<action android:name=”android.intent.action.MAIN” />

<category android:name=”android.intent.category.LAUNCHER” />
</intent-filter>
</activity>
</application>

</manifest>

 

 

Very Clear [ Android <=> Mysql ], no explain more, type again then u also can find out!

As we know that Codeigniter provides a very good feature that can auto encrypt the session for the php application.


So for the higher security, we can put  random words here to instead of ‘session’ as the above pic showed. There, I will refer from other people’s tutorial, suggest a website that can generate the random key as showed below.

Here, you can copy the keys directly and put to the config at Codeigniter.

 

The website -> Random Key Generator

To remove the ‘index.php’, can config by the follwing steps:

1. create a new file in the root folder, and named -> ‘.htaccess’.

2. copy the following code to the ‘.htaccess’ file.

<IfModule mod_rewrite.c>
#http://codeigniter.com/wiki/mod_rewrite
RewriteEngine On
RewriteBase /

#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#’system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#When your application folder isn’t in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename ‘application’ to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

3. change the ‘RewriteBase /’ if you put the codeigniter in other foler such as ‘/ci/’.
Then need to change as ‘RewriteBase /ci/’. (the sensitive of upper and lower case)

4. change the ‘application/config/config.php’
-> $config[‘index_page’] = ‘index.php’;
-> $config[‘index_page’] = ”;

5. open the Apache ‘httpd.conf’
-> find the line ‘#LoadModule rewrite_module modules/mod_rewrite.so’
-> delete the ‘#’ in front and save, start / restart Apache.

step 1 : install php

First, access http://php.iis.net/ , then download press “install PHP” button.

Then, click “install now”, download the file from the Website and open it.

Later, there will be a window shows your to install PHP.

And also there maybe need to download 3 files for installation.

Then accept, “next”. and after installation. You already build PHP on Windows.

======

step 2 : configure CGI

after that, maybe you want to test whether it can work.

But actually it didn’t finish yet.

You still need to follow 1 step which is :

Open the control panel -> find “Program” ->turn Windows Features on or not” -> “Internet Information Services” -> “World Wide Web Services” -> Application Development Features” -> and Select “CGI” ->Press “OK”

Then wait for a while……

======

step 3 : testing

Until now, you almost finish, so let’s have a test.

follow here.

Go to your “C:\inetpub\wwwroot” folder, and create a php file.

You can use Notepad, open Notepad and type:

 

<?php

phpinfor();

?>

 

Then save the file and named as “phpinfo.php” in the folder.

And to open your browser, and type “localhost/phpinfo.php” to address.

Enter….

If you can see the php version information, means the work has done.

—————-

Thanks

 

C++ Tutorial

Posted: April 6, 2011 in C++

//#ifdef GUARDIAN_H
//#define GUARDIAN_H

#include <iostream>
#include <string>
using namespace std;

class Guardian
{
private:
 string GFirstName,GLastName,GId,GPhone,GAddress;

public:
 
 Guardian();//default constructor
 Guardian(string FirstName,string LastName,string Id,string Phone,string Address);
 void setFirstName(string FirstName);
 string getFirstName();

 void setLastName(string LastName);
 string getLastName();

 void setId(string Id);
 string getId();

 void setPhone(string Phone);
 string getPhone();

 void setAddress(string Address);
 string getAddress();

};

Guardian::Guardian(){}

Guardian::Guardian(string FirstName, string LastName, string Id, string Phone, string Address)
{GFirstName=FirstName;GLastName=LastName;GId=Id;GPhone=Phone;GAddress=Address;}

void Guardian::setFirstName(string FirstName){GFirstName=FirstName;}
string Guardian::getFirstName(){return GFirstName;}

void Guardian::setLastName(string LastName){GLastName=LastName;}
string Guardian::getLastName(){return GLastName;}

void Guardian::setId(string Id){GId=Id;}
string Guardian::getId(){return GId;}

void Guardian::setPhone(string Phone){GPhone=Phone;}
string Guardian::getPhone(){return GPhone;}

void Guardian::setAddress(string Address){GAddress=Address;}
string Guardian::getAddress(){return GAddress;}
//#endif

==========================================

//#ifndef STUDENT_H //for avoid redefination for the header
//#define STUDENT_H

/*#include <iostream>
#include <string>
using namespace std;*/

#include “Guardian.h”

class Student
{
private:
 string SFirstName,SLastName,SId,SPhone,SAddress;
 
 Guardian G;//set a relationship from Guardian

public:
 Student();//default constructor
 Student(string Id, string Phone,string Address);//set the variable to the constructor

 void setFirstName(string FirstName);
 string getFirstName();

 void setLastName(string LastName);
 string getLastName();

 void setId(string Id);
 string getId();

 void setPhone(string Phone);
 string getPhone();

 void setAddress(string Address);
 string getAddress();

 /*——————Guardian—————————*/
 void setGuardian(Guardian Gd);
 Guardian getGuardian();
 /*——————Guardian—————————*/

};

Student::Student(){}

Student::Student(string Id,string Phone,string Address)//set initialization for constructor
{SId=Id;SPhone=Phone;SAddress=Address;}

void Student::setFirstName(string FirstName){SFirstName=FirstName;}
string Student::getFirstName(){return SFirstName;}

void Student::setLastName(string LastName){SLastName=LastName;}
string Student::getLastName(){return SLastName;}

void Student::setId(string Id){SId=Id;}
string Student::getId(){return SId;}

void Student::setPhone(string Phone){SPhone=Phone;}
string Student::getPhone(){return SPhone;}

void Student::setAddress(string Address){SAddress=Address;}
string Student::getAddress(){return SAddress;}
/*——————Guardian—————————*/
void Student::setGuardian(Guardian Gd){G=Gd;}
Guardian Student::getGuardian(){return G;}
/*——————Guardian—————————*/

//#endif

=========================================

#include “Student.h”

void main()
{
 Student Student1(“TP000001″,”0108888888″,”Bukit Jalil”);//set initialization

 string tmp;

 cout<<“Enter the firstname: “;
 cin>>tmp;
 Student1.setFirstName(tmp);

 cout<<“Enter the lastname: “;
 cin>>tmp;
 Student1.setLastName(tmp);

 cout<<endl<<“Your personal details:”<<endl;
 
 cout<<“Name: “<<Student1.getLastName()<<” ”
  <<Student1.getFirstName()<<endl;
 
 cout<<“ID: “<<Student1.getId()<<endl
  <<“Phone No: “<<Student1.getPhone()<<endl
  <<“Address: “<<Student1.getAddress()<<endl;

 Guardian Guardian1(“Huang”,”FeiHong”,”TP999999″,”0100000001″,”Bukit Jaili”);
 Student1.setGuardian(Guardian1);
 
 cout<<endl<<“Guardian personal details:”<<endl;
 
 cout<<“Name: “<<Student1.getGuardian().getFirstName()<<” ”
  <<Student1.getGuardian().getLastName()<<endl;
 
 cout<<“ID: “<<Student1.getGuardian().getId()<<endl
  
  <<“Phone No: “<<Student1.getGuardian().getPhone()<<endl
  
  <<“Address: “<<Student1.getGuardian().getAddress()<<endl;

 //Guardian Guardian1;
 //Guardian1.setFirstName(“Huang”);
 //Student1.setGuardian(Guardian1);
 //cout<<Student1.getGuardian().getFirstName()<<endl;
}